A naming engine within an engine.

Started by
6 comments, last by Estauns 22 years, 5 months ago
Hey all, I''m a little confused about naming entities within an engine. Basically, lets say I have like 50 ogres I need to generate on the fly. You''re walking through a map, you hit a button on the floor and POW! 50 ogres appear. According to my current knowledge of C++, to create an object of a class, you''d do something like COgre ogre1. But how would you go about creating something dynamically, as in the example above? Do game engines use some sort of ticker to keep track of the name of the ogres that have already been created, or how does it work? If you need to create 50 ogres, you can''t call them all ogre can you? You couldn''t do COgre ogre for all 50 could you? So how do they do it? If this is confusing, I''ll clean it up later. Not much time now. "Where genius ends, madness begins."
Estauns
"Where genius ends, madness begins."Estauns
Advertisement
COgre *ogres = new COgre[num_of_ogres_needed];// initialize the ogres, using ogres[j]->Init() or something// use the ogres// when you''re don with the ogres,delete [] ogres; 

The [] is critical because it causes delete to iterate through every element and call the destructor for it (which combats memory leaks).
Okay, so that creates a pointer called ogres which points to an array which holds all the ogre information (atleast number of ogres needed, etc.) But how would you add more ogres? Like say you need 3, then you need 6? Is this where a linked-list would come into play (I haven''t learned about them yet, so...)...


"Where genius ends, madness begins."
Estauns
"Where genius ends, madness begins."Estauns
Okay, so that creates a pointer called ogres which points to an array which holds all the ogre information (atleast number of ogres needed, etc.) But how would you add more ogres? Like say you need 3, then you need 6? Is this where a linked-list would come into play (I haven''t learned about them yet, so...)...


"Where genius ends, madness begins."
Estauns
"Where genius ends, madness begins."Estauns
Yep, that''s when you need to do some sort of dynamic data structure, like a linked list, or binary tree, etc.

If you do this, you should look at using the STL . It''s a linked list template, and all the work has been done for you.

Don''t worry, it''s not that complicated to use. I was able to learn /use it in about two days in a recent project. Just declare a template of your data type, which would be Cogre. Now you can add() new ogres at any time during the game, dynamically, like so:

COgre GenerateOgre;
GenerateOgre = new COgre("Ogre
MainOgreList.add(GenerateOgre);

Now, you can simply iterate through the list and you have access to all the Ogres in the game.


-nt20


"nikolatesla20"
"nikolatesla20"
quote:Original post by nikolatesla20
Now, you can simply iterate through the list and you have access to all the Ogres in the game.

Iteration through a list isn''t that fast, though.

When you want to decide which (STL) container to use, think of this:
  • Linked lists have fast (constant) insertion time, but slow (linear) access time. Of course, there are modifications that can be made to combat this

  • Vectors have relatively slow insertion time, but constant time access.

If you know ahead of time the maximum number of Ogres that will be used at any point in time, you can reserve that many pointers (if it isn''t too wasteful) in your vector:
typedef vector<Ogre *> vectOgre;vectOgre ogres;ogres.reserve(max_needed); 

Of course, if the maximum is 200 but you use 4 for most of the game, then that''s a significant waste.
quote:Original post by Oluseyi
When you want to decide which (STL) container to use, think of this:
  • Linked lists have fast (constant) insertion time, but slow (linear) access time. Of course, there are modifications that can be made to combat this

  • Vectors have relatively slow insertion time, but constant time access.



Also remember that "slow access time" means "slow random access time". A lot of the time in games you''re looping through all the elements in your list anyway (like calling the Think() method for each object in turn) and so using a list is probably not a bad idea. A vector adds a fair amount of overhead (since it stores two pointers per node instead of just one) and accessing a vector sequentially is basically just a depth-first search.

codeka.com - Just click it.
quote:Original post by Dean Harding
Also remember that "slow access time" means "slow random access time". A lot of the time in games you''re looping through all the elements in your list anyway (like calling the Think() method for each object in turn) and so using a list is probably not a bad idea.

Of course. However, if you cannot guarantee the linearity of access (for example that all Ogres, say, in a room are successive elements in the list) then virtually every instance of access becomes random access.

But he''s right anyway.

This topic is closed to new replies.

Advertisement