Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 egoplant   Members   -  Reputation: 117

Like
0Likes
Like

Posted 29 May 2013 - 05:22 PM

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?



Sponsor:

#2 Khatharr   Crossbones+   -  Reputation: 4032

Like
0Likes
Like

Posted 29 May 2013 - 06:28 PM

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.

#3 0r0d   Members   -  Reputation: 831

Like
0Likes
Like

Posted 29 May 2013 - 07:36 PM

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?



#4 Pieisgood   Members   -  Reputation: 202

Like
0Likes
Like

Posted 29 May 2013 - 07:48 PM

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.



#5 Quantumcat   Members   -  Reputation: 103

Like
0Likes
Like

Posted 29 May 2013 - 07:49 PM

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.



#6 Gorge Express   Members   -  Reputation: 179

Like
0Likes
Like

Posted 29 May 2013 - 07:52 PM

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. 



#7 Ludus   Members   -  Reputation: 970

Like
0Likes
Like

Posted 30 May 2013 - 02:54 AM

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.



#8 kunos   Crossbones+   -  Reputation: 2224

Like
0Likes
Like

Posted 30 May 2013 - 03:59 AM

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.



#9 egoplant   Members   -  Reputation: 117

Like
0Likes
Like

Posted 30 May 2013 - 10:06 AM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS