Help with the Loading/Revisiting a Map in game

Started by
4 comments, last by habeel 17 years, 6 months ago
hi, I have been working on a RPG game from time to time for fun. And while programming I kind of ran into a problem. The problem is ex. when you re-visit a map. Lets say there was a health bottle in a map called "House1" the players picks up/uses that health bottle and goes to another map, now when the player comes back to the map "House1" all the entities(health items,potions,monsters etc etc) are loaded from the entity file of House1 map that means that Health Items comes back. How do you guys go about it? Do u guys make some seprate class to keep track of all the changes that occur to each map. I mean this can be with stuff like, the player talked to the NPC once so next time when he visits the same map the NPC should remember that the player already talked to it. My game design is kind of like CS well there is a BaseObject class and all the objects like Player, Items, Monsters, Portals, and other type of Triggers are drieved from it. Maps are consist of 2 files: 1. Pretty much tile data, and where to place them with layers. 2. Entity Data (this pretty much what tells that, what stuff is gonna be on the map and where and with what property and a unique ID) so a Entity file would look like this. Unique ID is pretty much makes it easy for the game script to refer to diffrent objects in the script. // start entity player{ uid=1001; pos=100,40; script="playersrpt001.os"; } entity portal{ uid=1023; pos=201,10; Misc1="House2"; // pretty much name of the next map } so for now when we change map everytime it just loads the default stuff with there properties. So lets say ex. the player moved a certain thing a bit when the map gets reloaded it goes back to its standard place. So if someone can give me some idea's on how to counter this please thanks
Advertisement
The 'saved game' (which includes the current game, which you can consider as a temporary save) should maintain the transient status of all changable entities that are still 'game-active' (i.e. in the current area or any area which it is possible to revisit).

Transient status is any property of the entity that can change, typically position and existence (in the case of deletable entities, like things you can pick up), but possibly also things like mental state, whether a switch is triggered, etc.

If you want a level to be 'frozen' when you leave it, the simple answer is to save a file for the level you just left, and load the current state file for the level you're entering, if one exists. You need to add to your basic object:
class BaseObject { ... // No transient data by default public virtual void SaveTransientData(OutputStream stream){} public virtual void LoadTransientData(InputStream stream){}}

... and add suitable save functions to the subclasses for which you want to preserve state.

The transient data is likely to be a subset of the entity data from your map file, so if you already have level readers/writers you can adapt them to do transient data instead.

Then, when you re-enter an area and the state file for that area exists, there's now a two step process. First, load the area as normal (to populate the 'current area' entity store). Then load the state file, updating each entity with the properties specified in that file (the transient data you saved earlier); if the entity is flagged as non-existent in the state file, remove it. (Note: removing it may just mean flagging it as deleted, clearing its expensive resources, then not including it in any game logic, as that makes it easier to remember in the new state file.)
I used a similar approach to the above method, but a little different. Basically, I had one entity format that could be loaded, this stored all the state data for each entity (including custom data per entity class). When leaving a level, I literally save the entire scene as a new file (I have the data directory that the original is loaded from, and a save directory with the same scene directory structure). When loading a scene, I try to load the save scene first (if it exists), else load the original scene. This means that all entities in their current form are saved when you exit the room (each class has certain flags, like don't save and such, including the player, autospawned child entities, etc, to make sure it reloads properly). This way I only need to load a single entity scene file, and I can keep a running record of what happened in each scene.
If you wanted, you could do a slightly different approach to the method described above. Everytime the player leaves the map, save any important information to a file in the same format as your level files. Stuff like switches, items, NPCs, whatever. When you load a level, first load the original level file, then load the saved state file and change any variables as neccessary. This lets you have greater control on what actually gets saved (so if you update a level, for instance, the save won't be an old version).
I have a basic system for this. The abstract is that I just make a save game of all the maps I've visited so far.

When entering a new map, check to see if the altered world package contains any information about this map. If not, generate the map (either from disk or using my random level generator). If so, load the map and data from the world package.

When leaving a map, save the map and world status, using the same "saveWorld" function that I have in the editor. This isn't very space-efficient, since it just saves a copy of the entire world with a few changes, but disk is cheap. If you're targeting a console you might want to correct this and set up some sort of "dirty flag".

It's pretty simple as long as you make sure not to save transient things in the map (such as temporary items, the player, etc), and watch out for references/pointers/etc to other items.
thank you all for the replies, they really helped alot, I should be able to fix it now :)

This topic is closed to new replies.

Advertisement