Jump to content
GameDev.net Posting Guidelines (please read before posting)
For Beginners Forum FAQs (please read before posting)
Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
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.
Posted 27 October 2012 - 11:55 PM
Posted 28 October 2012 - 02:35 AM
Posted 28 October 2012 - 03:13 AM
Posted 28 October 2012 - 01:39 PM
"I thought of passing a pointer of the room to the object, but that sound's like it can get messy because of the circular dependency."
It's not a 'circular dependency', it's a circular reference. Pointers can but do not necessarily convey memory ownership.
It's fairly typical in these sorts of games that the objects know which room structure they belong to and the room has references to all the objects contained in it. The room comes with nice utility functions such as "iterate over all contents except object X" or "iterate over all contents except the ones in this list" or "iterate over all the contents whose ID exceeds N" so that an object can cause things to happen to other objects more easily. Also you're going to have finder methods; "Find me all the objects which are touching object A". "Iterate over all objects within R of X,Y".
A circular dependency would imply that you're expect the room to delete the objects in it when they're not needed (probably the case) and also that the objects are expecting to delete the room they're in for some reason (which would be nuts. Not least because many objects would delete the room several times).
The directed and necessarily acyclic graph of memory ownership is not the same as the possibly cyclical graph of object references.
As Katie told, be no afraid of circular references.
In your example - sooner or later you want not only to detect collisions but plan actions (pathfinding, discovery etc.). To do such actions your NPC needs the whole room/level, not only parts that are touching.
As of your second question - single linked list is most probably not enough. You need at least one spatially organized container (2D or 3D array, octree, quadtree...) that allows you to make fast spatial lookups (check whether certain grid spot is free, check what is at neighboring slot...). In addition to that you may want to have additional containers - for example all dynamic objects (mobs) can be in linked list, so you can quickly iterate over them.
Having single object referenced from many containers is acceptable - you only have to have clear idea who owns it (i.e. who is responsible of creating/deleting it).
Posted 28 October 2012 - 11:16 PM
But hey, I guess thats what include guards are for ;o
You may also have to use forward declarations. Wikipedia has an example of how to implement circular dependencies in C++.
Posted 29 October 2012 - 04:48 AM
Whew, thats a lot of containers. I'd love to use a 2D array, but my game objects can be in any position (they are not confined to a fixed block). That leaves me with a 2D array of linked lists of objects, or a quadtree of objects, or something else.
Posted 29 October 2012 - 07:14 PM
Is it also true of your tiles? Because in most 2D dungeon-syled games level details (walls) are the most frequent colliders by an order of magnitude. Thus it may make sense to keep level details and movable objects in separate containers if it simplifies collision testing.
Otherwise either quadtree or array of lists seem reasonable. The latter is probably easier to implement. Just make your container interface abstract enough so you can change implementation details later if you find that your initial implementation is not flexible/fast enough.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.