Page 1 of 1

variable variable

Posted: Sat Dec 29, 2007 1:40 pm
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

Posted: Sat Dec 29, 2007 2:57 pm
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;

Posted: Sat Dec 29, 2007 4:40 pm
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

Posted: Sat Dec 29, 2007 5:01 pm
by shogun
You want an array and you want to read a book about C/C++.

Posted: Sun Dec 30, 2007 12:54 pm
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?

Posted: Sun Dec 30, 2007 1:39 pm
by LosNir
I understand him,
he wants to make a variable that the name of that variable is taken from another variable + a number.

Posted: Sun Dec 30, 2007 1:40 pm
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?

Posted: Mon Dec 31, 2007 2:11 am
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? :)

Posted: Mon Dec 31, 2007 2:19 am
by shogun
That would be an array.

M.Irr, wenn dir die Worte im Englischen fehlen, probier's mal deutsch.

Posted: Mon Dec 31, 2007 2:30 am
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();

Posted: Mon Dec 31, 2007 10:15 am
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:

Posted: Mon Dec 31, 2007 10:58 am
by shogun
Even Actionscript has the Array()-class ...

Posted: Mon Dec 31, 2007 11:50 am
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!