- Home /
storing a line of code as a reference/varible
hi I've looked everywhere but i can't seem to find out if i can do this, basically i won't to store something like
var switchMenu = menuSwitch.animation["areaToLevelSwitch"];
outside of a function so that other functions inside my script can use it, i can store it in a function fine, but would like to know if it is possible to store it outside a function.
Answer by Doireth · Jul 22, 2012 at 11:27 PM
I have never heard of storing code/expression as a variable and don't see how it would even make sense. Remember a variable declaration is the designation of memory based on the variable type. A line of code doesn't fit into any type be it primitive or reference.
Even if you were to store a line of code as a string you will need to parse the code, do syntactic and semantic checks and then have some compiler turn the code into machine instruction.
There is really no reason I can think of where this idea would be the solution or have I just misunderstood the question?
If it's just the reuse of code then:
function Example() {
// the code you'll want to reuse
}
function Update() {
Example(); // use the stored code
}
....should suffice.
it was mainly just to store a reference which is used in about 6 different functions in one script, i didn't think it was possible, but you never know. i guess storing it in a function wouldn't make much difference, as to just writhing it at the top of each function... cheers
Answer by ScroodgeM · Jul 22, 2012 at 07:20 AM
simply write your line outside function.
var switchMenu = ...;
function f1() { ...use switchMenu; }
function f2(some params) { ...use switchMenu; }
or i didn't understand your question???