Hello
I am making a 2d adventure/rpg game right now, and I just got the world/room thing working. Now the player (a white brick) can walk around two rooms made of identical tiles (blue bricks), and collision seem to work fine.
Now it is time to add some npc that can also walk around, do things, etc. The game is made of several rooms, and each room has a linked list of game objects. The game objects however know nothing about the surrounding.
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.
Then I thought I'd use a sensory object that is basically a rectangle and a list, and it would follow the npc object around. The world update function would then update each object normally, and add any objects touching the sensory rectangle to that list.
My questions:
1. What is a good way to give awareness to npc objects?
2. If I want to add path finding to the AI later, is the linked list a good way to store game objects? game objects include tiles, player, npc, and every game object has a position, velocity, collision rectangle.
3. (don't know what to ask yet)
Questions about AI
"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.
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).
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).
"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.
Thanks, I'll try that then.
Also, by circular dependency I meant the need to #include "gameobj.h" in world.h, and #include "world.h" in gameobj.h to allow rooms contain game objects, and at the same time allow game objects to have a pointer to rooms.
But hey, I guess thats what include guards are for ;o
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).
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.
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++.
[quote name='ultramailman' timestamp='1351453177' post='4994820']
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++.
[/quote]
Thanks.
Now I find myself with two more options:
1. pass the objects the linked list instead.
2. pass nothing. The linked list I am using is actually embedded inside the object already, as was required by this particular linked list implementation.
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.
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.
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.
The game right now makes no distinction between tiles and game objects. Tiles are merely game objects whose "movable" flag is set to 0. I did however added two more linked list, one for the movable objects and the other for unmovable objects, like you suggested. I am not seeing any improvements because I only have around 60 objects in one room right now, but I am sure it will be helpful as I add more movable objects.
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.
I think I will go with the quadtree, it feels like even if it is harder to implement, it will be more useful in the future. And I think it will be abstract enough, like using functions to read and modify it, using convenience macros to iterate through it, etc.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement