What is the best way to keep track of game objects?

Started by
7 comments, last by egoplant 10 years, 10 months ago

My game is a sort of grid type of game. My problem is, once I initially load one area, if I leave that area and come back all of the objects in that area reset, because my way of loading an area is just a list of all objects to generate in that area. I'm wondering what the best way to keep track of objects would be? Like for example, if someone moved an object one square to the left, how would I know that next time he reenters the area?

Advertisement

Change the way you store and load the area states.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

My game is a sort of grid type of game. My problem is, once I initially load one area, if I leave that area and come back all of the objects in that area reset, because my way of loading an area is just a list of all objects to generate in that area. I'm wondering what the best way to keep track of objects would be? Like for example, if someone moved an object one square to the left, how would I know that next time he reenters the area?

So... you have objects in a list, but you dont like the list and want to put them in something else. Is that right?

You delete the objects from the list, and then dont like that you have to create new ones to put into the list. Why not just leave them in the list?

I dont understand what your problem is. Sounds like the issue isnt the code, but that you dont know what you want to do. If you dont know what you want to do, how do you expect us to know?

I'd suggest creating a factory for your game objects and a destructor function. So when your player leaves an area and you don't want to keep those objects in memory you call your objects destructor function which will then write data about it's private members (location, speed, yada yada) into a text file in a nice format (json, xml, your own). Then when your player re-enters that area, you create a new object with your factory and have it instantiate the private members with the data that was saved to the text file. This has an added benefit of easily extending to other types of files... like a save game file.

Hope that helps.

You can make every object be a class, with positions as member variables, and have a vector of these objects. Then when you draw them, loop over them and use their positions. When they move, change their positions. Every object will keep track of its own position.

Well, you could use flags I guess. Lots and lots of them.

Or you could save the state of the world, instead of reloading it anew. Serializing the entire world is one way to do that, though it's inefficient.

Another option is to reference information from both a "base world" and your save data to construct each object at the necessary location. Your game would keep track of only things about the object that can change, like position or if a button is activated or not or whatever. It would also have some kind of unique id. The id would reference an object in the base world, which contains the rest of the info necessary to construct the object.

The problem is exactly because you're loading the objects of each area as soon as the player enters an area. Rather than doing that, you could load every area as soon as the program starts. Depending on how much information each area contains, this could take up quite a bit of memory, however. Another solution is to use a system of file saving/loading when the player leaves/enters an area, storing the information of each area onto the HDD. Considering this is a grid-based game and you're just storing the position of each object, it would probably suffice to go with the former solution of just loading each area to the memory and keeping it there as long as the game is running.

pretty much what Ludus said. What's the reason for having these "area"? Are you memory limited? I really doubt you are if you are asking this question.

So, if you are not, just load everything up front and you are done. If you are trying to speed up loading times, then just have a map<string,Area*> areas, if areas["theAreaINeeedNow"]==nullptr then load the area from disk.

If you really are memory bound.. then you'll have to be much clever than this and get into streaming and serializing areas and their current state.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni

Thanks for all the replies, you've given me a lot to think about. I will try and implement one of them.

This topic is closed to new replies.

Advertisement