variable variable

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
M.Irr
Posts: 11
Joined: Thu Dec 20, 2007 6:54 pm

variable variable

Post by M.Irr »

Hi,
I know that it is simple to make that but how do i make that.
i want a Variable with a counter ex:

Code: Select all

int count=0
while(count<100){
int "varname"+count=1
count++
}
How could I declar the name like that?
:D
CuteAlien
Admin
Posts: 9687
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Not sure if I understand that right. You want the variable name itself to change? Sorry, you can't do that and I doubt it's what you need. If you tell us what you want to achieve by this we can maybe help you to do that in another way. My guess would be that you want an array.
like:

Code: Select all

int variable[100];
You can access each element of the array like:

Code: Select all

for ( int i=0; i <100; ++i )
 variable[i] = 1;
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
FlyingIsFun1217
Posts: 219
Joined: Fri Apr 13, 2007 8:29 pm
Location: Illinois
Contact:

Post by FlyingIsFun1217 »

Are you basically trying to increment the number each time? I think nobody is quite sure what you mean, because you don't clearly state what you want it to do.

If language is a barrier, just post the same in your native language, and see if anybody can translate.

FlyingIsFun1217
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

You want an array and you want to read a book about C/C++.
M.Irr
Posts: 11
Joined: Thu Dec 20, 2007 6:54 pm

Post by M.Irr »

no a Array don't work I think. My problem is that i must make SceneNodes with the nam like Node and than a Counter so that i have:
Node1
Node2
...
...

But how can i make this?
LosNir
Posts: 43
Joined: Wed Dec 19, 2007 6:38 pm
Location: Israel
Contact:

Post by LosNir »

I understand him,
he wants to make a variable that the name of that variable is taken from another variable + a number.
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

Um, I don't understand your problem. If you want to give your SceneNodes names, you can do that by:

Code: Select all

core::stringc name("node"); name += counter;
node->setName(name);
But I'm still not sure how that will help you. Are you really, really sure you don't want an array. Did you read any C/C++-tutorials? A book?
LosNir
Posts: 43
Joined: Wed Dec 19, 2007 6:38 pm
Location: Israel
Contact:

Post by LosNir »

I'll explain him

We got 2 vars, X and Y:

Code: Select all

char* X = "Scene";
int Y = 0;
What he wants to do is creating a third var, but the name of that third var will be combination of X and Z.

So creating that var with the name that combines X and Y, would be:

Code: Select all

int Scene0 = 0;
and if you increment Y, is should be:

Code: Select all

int Scene1 = 0;
another example is that if you change X to lets say "IrrLicht", the var would be (after the increment of Y):

Code: Select all

int IrrLicht1 = 0;
Got it? :)
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

That would be an array.

M.Irr, wenn dir die Worte im Englischen fehlen, probier's mal deutsch.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

I know these variable variables from PHP. They are very handy.
However C++ does not have anything you could compare with it.

Having an array is the best way to achieve this in C++
You still have one varible that can be "prefixed" with numbers.
e.g. Var[1]

so if you want multiple SceneNodes in one variable you would do something like this:

Code: Select all

core::array<ISceneNode*> Nodes;

// you can fill the array with this
Nodes.push_back(MyNode);
// where MyNode is a ISceneNode*

// Now you can access the nodes with
Nodes[i]

// so if you for example want to get the id of one of the nodes you would do this
Nodes[0]->getId();
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
kornwaretm
Competition winner
Posts: 189
Joined: Tue Oct 16, 2007 3:53 am
Location: Indonesia
Contact:

Post by kornwaretm »

a ha, this guy must be the flash action script guy. ?...

what u want is imposible. i guess this is the different between a compiler and an interpreter, interpreter can acces source codes (may be?) and read some string operation as a variable name. flash player is an interpreter, and c++ is compiler. u triying something that i have try man.

by the way u've got u're answers on the previous reply.
:wink:
shogun
Posts: 162
Joined: Wed Sep 05, 2007 11:02 am
Location: inside

Post by shogun »

Even Actionscript has the Array()-class ...
M.Irr
Posts: 11
Joined: Thu Dec 20, 2007 6:54 pm

Post by M.Irr »

Oh thanks to all for the help i will test it!
Danke auch shogun für das Angebot wenns jetzt trotzdem net geht werde ich es so machen!
Post Reply