Jump to content
  • Advertisement
Sign in to follow this  
Crashman

Instancing an Object with a Dynamically created name?

This topic is 4621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you instance an Object named by a variable and then call functions from it? ie. int i = 0; while(till doomsday) { Object i; ++i; } 3.Set(); 15.Set(); Something along the lines of that.

Share this post


Link to post
Share on other sites
Advertisement
What are you trying to accomplish? There are ways of approximating what you want to do, but 99% of the time they are not the best solution to the problem.

Enigma

Share this post


Link to post
Share on other sites
In most languages, the way you're talking about, you can't/don't.

The closest you generally can come [in C++] is something like this:


map <int, variable_type> variable_list;
map <int, variable_type>::iterator variable_picker;

int i;

for(i=0; i<100000; ++i){
variable_list=variable_type();
}

variable_picker=variable_list.find(3);
if (variable_picker!=variable_list.end()){
variable_picker.Set();
}
variable_picker=variable_list.find(15);
...



Or if the numbers are all known at compile time you could probably do some template trickery that would lead to something akin to:


variable<3>.Set();
variable<15>.Set();



but you couldn't use variables in the angle-brakets, which is likely not what you need.

Share this post


Link to post
Share on other sites
All I need to do is generate Instances that I can call functions from, but I don't know how many I will need in all because the circumstances change everytime the program runs. The "naming/calling an instance with a variable" was just something i thought of..
Is there a more efficient way of doing it?

-I went over my notes and found out the max number of Instances is going to be somewhere around 8000, and they're going to be created/destroyed all the time, so I need a way to track which one is supposed to be destroyed.

Share this post


Link to post
Share on other sites
Basicly its a 3 Dimensional puzzle game, i need to have the cubes destroyed once they are in a certain position and I was trying to think which would be the most efficient way of tracking their position, their "neighbors", their color, and destroying them if put into a certain pattern.

Share this post


Link to post
Share on other sites
Ok, I found a way to track their position and check their nieghbors status, but I still need a way of instancing objects on the fly..

[Edited by - Crashman on October 22, 2005 9:34:26 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Crashman
Ok, I found a way to track their position and check their nieghbors status, but I still need a way of creating objects on the fly..

For a real pain you could make a sextuply-linked list:

class object {

private:
object * left_object; // [x-1][y][z]
object * right_object; // [x+1][y][z]
object * up_object; // [x][y-1][z]
object * down_object; // [x][y+1][z]
object * back_object; // [x][y][z-1]
object * forward_object; // [x][y][z+1]

public:
object & left() { return *left_object; }
object & right) { return *right_object; }
object & up() { return *up_object; }
object & down() { return *down_object; }
object & back() { return *back_object; }
object & forward() { return *forward_object; }
...
};

But that's probably more trouble than it's worth. [totally] You could see what Boost::multi_array can do for you.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crashman
How do you instance an Object named by a variable and then call functions from it?

ie.

int i = 0;
while(till doomsday)
{
Object i;
++i;
}

3.Set();
15.Set();

Something along the lines of that.


I'd use a std::map

string strName = "1";
std::map<Object*> objList;
objList[strName] = new Object;
objList[strName]->Set();

That's just a quick example of the dirty way to do it. What you'd want to do is utilize all the functions with the map class properly. If this method interests you and you want to see it better, let me know.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!