Jump to content

Map data


aiaf
 Share

Recommended Posts

bat = {}
bat["x"] = 0.1
bat["y"] = 0.1
bat["z"] = 0.1
bat["hitpoints"] = 100
bat["damage"] = 5 
bat["armor"] = 6

acidtrap = {}
acidtrap ["x"] = 0.1
acidtrap ["y"] = 0.1
acidtrap ["z"] = 0.1
acidtrap ["hitpoints"] = 100
acidtrap ["damage"] = 5 

portal = {}
portal["x"] = 0.1
portal["y"] = 0.1
portal["z"] = 0.1
portal["open"] = 1


mapdata = {}
mapdata["monsters"] = {bat, bat, bat}
mapdata["traps"] = {acidtrap, acidtrap, acidtrap}
mapdata["islandportal"] = portal

print(mapdata["monsters"][1]["x"])

I was thinking how to store island information on disk.

Dont think we need anything else this is simple enough.

When game starts first time we serialize and write to disk such tables for each map.

Then we load data into the game, and save when necessary.

I made this with Leadwerks/UAK:

Structura Stacky Desktop Edition

Website:

Binary Station

Link to comment
Share on other sites

On the workshop I did have a Save/Load script. The idea is that you add 2 script functions for a script attached to an entity you want to save/load data for. SaveData() and LoadData(). In SaveData() you return a table structure that will be saved for that entity (unique names of entities acting as it's identifier). LoadData() has an argument which is that entities table that was returned from SaveData() so the entity itself determines it's own data structure to be saved and loaded. This helps remove any sort of global way of needing to save information.

 

Here is a post I replied to recently about someone using it:

 

Link to comment
Share on other sites

So you are suggesting having all data set per entity with SetKeyValue.

Guess you could save serialized lua tables with SetKeyValue if you want something more complicated.

 

Im not entirely convinced, but we can go with this.

Sometimes you need more efficient data structures but this should work for what we do.

I made this with Leadwerks/UAK:

Structura Stacky Desktop Edition

Website:

Binary Station

Link to comment
Share on other sites

I don't think that process uses SetKeyValue. It uses variables that are in the entity scripts. I think you mentioned that you were more on the C++ side than lua. In lua you can attach a script to an entity, and in that script you can define variables that are specific to that instance of that entity. Imagine if you created an instance of a C++ class and attached it to an LE entity. That class instance has variables and values for that specific entity. Then imagine that class had 2 virtual functions called SaveData() and LoadData(data). When you decide to save your game, every entity in the world at that time gets iterated over and it's SaveData() function called and inside there you return from it a structure of the data you want to save for that entity. When you load a saved file it loops through everything that you saved and if it was a dynamic object it'll create it from a prefab or if it already exists in the loaded map (via unqiue name) it'll call that entity scripts LoadData(data) passing in the exact same structure that was saved. That way that script knows exactly what it saves and so it knows exactly what to load.

So think of entity scripts just like C++ classes that you can attach to LE entities. Same idea. The main difference is the structure you'd return in the SaveData() function because in C++ it has to either be string related or a common structure but you couldn't really have a common structure because each class could have different properties that you want to save. However in lua because it's a scripting language you can just return a table that is a different structure for each entity because it doesn't matter. It's just a table and that exact same table will be passed to that entity's LoadData(data) function. The entity already knows the table structure because it's the one that created it via SaveData() so it knows how to read it and what variables in it to set to it's entity variables.

The reason this is nice is that the creator of the scripts themselves determine what values should be saved off instead of deciding that at some global level for every entity that exists.

 

Example:

So to use your example above. Let's say we have a bat in one of our levels. That bat model/entity is put in at design time in that map and a script is attached to it that gives it it's functionality. We give the entity a name like Bat1 in the map. We set a script variable that indicates it should have data saved and loaded in it's Start() function. So when we loop over all the entities on a save it'll check all their scripts for this value to decide if it should call it's SaveData() function. Let's say the bat has a health variable. So we hit the bat once and reduce it's value by 10. Let's say the health variable is now 90. Then we instantly press the save button. What happens when we do that?

1. Loop over every entity in the world

2. Check if it has a script attached to it and if it does check if it has a shouldSave variable and if it does and it's value is true...

3. Call it's SaveData() function and take it's return value (which will be a table with whatever structure, we don't care at this point but it would have something like a health variable in it that we set from the scripts self.health variable) and save that to a bigger overall table where it's key is the entities unique name.

4. Once all entities are finished that bigger overall table is saved to disk because it now has all the entities data that needed to be saved.

 

Then we want to reload the map.

1. The map is loaded.

2. We read in the saved overall table from disk that has all entities saved information in it and loop over every entity in it

3. For each entity name we find that entity that is loaded (because we loaded the map in step 1) and we call it's LoadData(data) passing in the same table it gave us via it's SaveData()

4. Inside LoadData(data) we take data.health and set it to self.health (this bats health variable)

5. Now all the entities have their script variables set to what they were when they were saved off.

  • Upvote 1
Link to comment
Share on other sites

All that being said about how the process works for saving at an entity level vs a global level, if each map is kind of in isolation and shouldn't take long to do then are we really needing to save any map data like in your example of a bat or acid trap? I would think each map is just a fresh load everytime.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...