all groups > flash actionscript > october 2007 >
You're in the

flash actionscript

group:

change string to variable (sort of)


change string to variable (sort of) trigger2160
10/3/2007 10:43:58 PM
flash actionscript:
Hi. I am making a game in flash which requires lots of similar functions, such
as

function getSomeNumber():Number{
return number;
}

I was wondering if i could generalize this by doing the following. take a
parameter of string defining the name of the variable. i then want to change
this string to the name of the variable so that i can output the value of the
variable. a bit like this

function getSomeNumber(nameOfVar:String):Number{
//somehow use string to find out variable name and return value of variable
return unknown;
}

hope this makes sense.

trigger2160
Re: change string to variable (sort of) AScracker
10/4/2007 5:53:53 AM
FIrst you need to convert the String to Number
Here the Code is
var str:String = "123";
getSomeNumber(str);
function getSomeNumber(nameOfVar:String):Number {
// here you need to convert the string to number bcoz ur return type is Number
var num:Number = Number(nameOfVar);
return num;
}

~~~SK

Re: change string to variable (sort of) trigger2160
10/5/2007 12:00:00 AM
Im sorry but thats not what i am after. i do not want the string changed to a
number. i want it changed to a variable name.

var someThing:Number = 20;

I have a variable as above. i want the string changed to the name of the
variable (someThing) so that the variable can output its number. Hope that is
clear.
Re: change string to variable (sort of) clbeech
10/5/2007 4:03:48 PM
OK, you should be able to do this, if the variable resides in the root
timeline, else point to the mc containing the var:


var someThing:Number = 20;

function getSomeNumber(nameOfVar:String) : Number {
var num = this[nameOfVar];
return num;
}

getSomeNumber('someThing');
Re: change string to variable (sort of) kglad
10/5/2007 5:04:22 PM
actually cl, you're creating two variable references with that code:

this[nameOfVar] and getSomeNumber().

it's probably clearer if you do something like:



var s:String="someThing";

function convertStringToVar(varName:String, varValue:*):* {
this[varName] = varValue;
return this[varName];
}
var anotherVariableReference:* = convertStringToVar(s,33);
trace(anotherVariableReference,this[s]);

// to remedy, don't use a function with a return value to define
this[varName]. just use it directly:

var varName:String = "someThing";
this[varName] = 20;
Re: change string to variable (sort of) trigger2160
10/5/2007 5:50:02 PM
Re: change string to variable (sort of) clbeech
10/5/2007 6:26:11 PM
@kglad: yeah, I see what you're saying there kg, but I think he's just wanting
a return from a previously created list of variable references. I'm not sure
why he wouldn't call them directly, but I think it has something to do with the
user input from his system being in the form of a String, so he wasn't able to
access the value of the var named similarly ... but I follow you, and I guess I
should have even simplified the return as ...

return this[nameOfVar];

... rather than setting up the extra reference in the function, but your
method is the way to go for constructing a new variable from an input String
and value, and I love the wildcard use here, didn't know that would work, cool.

OH, but hey (@trigger also) if the variable already exists, one should be able
to reference it directly by calling ...

this['something'];

... and if you've got a function which is gathering the input you could say ...

value = this[input];

... if 'input' is in the form of a string, and it should return the value of
the variable, without having to call to a function, right? that's what you're
saying by 'just use it directly', I got it now :)
AddThis Social Bookmark Button