Jump to content
  • Advertisement
Sign in to follow this  
egoplant

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

This topic is 1937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!