🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

How do i manage objects?

Started by
7 comments, last by Oak 24 years, 5 months ago
In my game there are basically two types of objects, players and bullets. Each object (player or bullet) is a ''class Object''. Currently, I have a pointer to an array of pointers to Objects (Object **objects) which i grow with realloc() and then add object pointers to with new Object. My problem is that when adding the multiplayer-code I need to be able to identify and locate an object in many different computer systems, and as the arrays are not fixed, I cannot simply state ''delete object no 34''... I can''t be sure that the objects will appear in the same order on all machines... Do I have to use a fixed-size array or is there some other good way of managing objects? ... I''m thinking about using a linked list to store all the objects, but won''t that make finding a specific object even harder?? ... The objects are deleted when either a player disconnects or when a bullet hits a player or a wall, will that make a linke list a much faster alternative, so I won''t have to realloc() the list all the time?? ... Please share your ideas on the subhject! Åke Wallebom
Advertisement
Please tell me you're not using malloc (realloc) and delete together. PLEASE!!!

I think a better way to do it would be to use a map of integers to object pointers. You need to make this integer a consistent tag on all computer systems. This is pretty fast, since searching a map is O(logN), whereas a list search is O(N). Check out &ltmap>, one of my favorite containers.

BTW, you should use EITHER malloc/realloc/free OR new/delete. Never mix the two groups. I'd suggest the latter, since you're using C++ (as evidenced by you class Object).

Edited by - stoffel on 1/26/00 6:39:40 PM
Try maybe an associative array (hash table) or a binary search tree. Either should give you good search time and proper dynamicism.
Thanks for the replies,

(I''m afraid I was using realloc and free on the pointer list, and new and delete on the pointers. This shouldn''t be a problem, or maybe I''m just ignorant =) ... )

I don''t understand the concept of a ''map of integers'' ? And what is the '' container'' ? ...

If I don''t want a fixed sized array, do I always have to look through all Objects for a specific value in one variable?

I''m not very familiar with binary search trees though, maybe you could explain this further?

How would i add an object to the list and then how would i identify it using some id number?

Åke Wallebom
Sounds like you could use a healthy dose of algorithms. At any book store you will be able to find an algorithms book in C/C++. For many of the games I have written it pays to have multiple lists for the same information. Usually for objects in my MUD I have a linked list that is global to the game. However it is usually not efficient to search for an object in a global nature everytime I use it, so I created binary search trees agains the same node data to globally search for items. You can take this even further and create binary search trees for each zone, or have a linked list anchored in the zone to your object list. In the end all you have to do is balance the number of index updates you must do in-order to add/delete nodes, versus what it will take to traverse the tree in search of a particular node. Most of the time for me the latter occured far more often so it made sense to maintain multiple indexes on my objects. The same approach can be taken to players.

Kressilac
ps This is by no means exhaustive. Doubly linked lists, priority trees, n-trees, hash-tables, and various other sorting methods can be used to sort and search your data. Again I recommend a good book on algorithms for more on this.
Derek Licciardi (Kressilac)Elysian Productions Inc.
If you''re going to do a lot of searches, stay away from linked lists.

A BST is like a linked list, except every node has two pointers. Values greater than the node value get sent to one pointer, values lesser than the node value get sent down the other pointer. This way you can zoom in on a specific value in O(lg n) time. There are other variations of BSTs that are self-balancing like AVL trees or Red-Black trees.

A good book on algorithms and data structures is: "Introduction to Algorithms" by Cormen, Leiserson and Rivest.
You really should use a data structure that can remove specified objects, like List.remove(Object) then your objects can remove themselves, when you are find that a bullet hit an object, you can just do List.remove(bulletObject);
I agree with the poster who said you need to look into algorithms. If you're going to get into serious programming, you need to understand abstract data types: arrays, linked lists (single- and double-), queues, stacks, heaps, trees, hash tables, and anything else I'm forgetting. These are also called "containers" in the Standard Template Library context, because they contain objects.

The reason I suggested using a std::map instead of a binary search tree is that the map is already in the standard C++ library, and it's easy to use (if you understand STL). In the Microsoft version of STL, the map and set containers are implemented as red/black trees.

If you wanted to use an STL map with your Object class to help you look for the important value, this would be the way to do it:

#include &ltmap>
using namespace std;

//...in your function or class or whatever

typedef map&ltint, Object*> ObjectMap;

ObjectMap m_objMap; // declare object map variable

// now when you want to use the map:

Object* pObj = new Object; // make a new object
Object->important_value = 3;
Object->other_values = whatever;

// insert the object into the map, using its important_value as the key
m_objMap.insert (ObjectMap::value_type (pObj->important_value, pObj));

// insert more objects...

// get the object using the important value, happens in lgN time:
ObjectMap::iterator obIt = m_objMap.find (3);
ASSERT (obIt != m_objMap.end ()); // make sure it found your object
pObj = *obIt; // you get the object pointer back

There you have it. I have a feeling this is over your head, but I like to give code when I can. If this doesn't make any sense, you should get a book on algorithms, then one on STL (because it is, honestly, difficult to learn). Good luck. And STOP using malloc/realloc!! =)

Edited by - stoffel on 1/27/00 4:11:07 PM
A good STL book:

STL Tutorial and Reference Guide, by Musser and Saini. Blue and white Addison-Wesley hardback.

Good stuff.

Mason McCuskey
Spin Studios - home of Quaternion, 2000 GDC Indie Games Fest Finalist!
www.spin-studios.com
Founder, Cuttlefish Industries
The Cuttlefish Engine lets anyone develop great games for iPad, iPhone, Android, WP7, the web, and more!

This topic is closed to new replies.

Advertisement