Attract-Mode Support Forum

Attract-Mode Support => Scripting => Topic started by: Luke_Nukem on June 07, 2014, 08:39:59 PM

Title: Passing a "global name" to a function?
Post by: Luke_Nukem on June 07, 2014, 08:39:59 PM
I've got a tricky one...

Lets say I'm trying to pass a global variable name to a function, not the value of that variable itself.

Code: [Select]
testName <- 0;
function test (x,y,varName){
      local x = x;
      local y = y;
      varName = 1;
    return x+y;
}
local testFun = test(3,5,testName);
print ("testFun = "+testFun+"\n");
print ("testName = "+testName+"\n");

I would expect this to print
Code: [Select]
8
1

Except varName is local to the function, and gets passed a 0.
It then changes the 0 to a 1 in the function, but because it was done locally to that function, testName remains as 0.

I could of course use testName in the function itself, but I'm trying to pass the name of that var to the function. A bit like a pointer.

Anybody have any ideas?
Title: Re: Passing a "global name" to a function?
Post by: liquid8d on June 08, 2014, 08:31:49 AM
Arguments passed to the function will be local to it, you are just passing the value of testName to the function. As you said, if you want to modify the value of the global variable you can just use it or in a different scope you can preface it with ::, but I'm not sure if that is helpful for what you are trying to do.

Can you explain what you mean by 'trying to pass the name of that var to the function'? Perhaps you mean something like a key/value where the key you are changing the value of is dynamic. A table can do that:

Code: [Select]
myValues <- {
   testName: 0
}

function modifyValues(key, val) {
   myValues[key] = val;
}

Hope that helps!
Title: Re: Passing a "global name" to a function?
Post by: Luke_Nukem on June 08, 2014, 12:47:32 PM
Thanks liquid8d.
I figured I could perhaps use an array to do it, and while looking through the squirrel ref, stumbled across that. The lexical style is damn confusing at best though.
Thanks for the example, it clears it up in my head quite nicely.
Title: Re: Passing a "global name" to a function?
Post by: Luke_Nukem on June 08, 2014, 05:40:04 PM
I came up with this, I've been trying to refine some parts of my layout, and wanted my increment over time code, to be in a single function that could be used for any purpose, rather than copy/paste style.

Code: [Select]
switchVals <-{
"swTEST" : 0,
"tTEST" : 0,
"rTEST" : 0
}

function increment( ttime, counter, switcher, returnVal ) {
if (switchVals[counter] == 0)
switchVals[counter] = ttime;
print("counter = "+switchVals[counter]+"\n");
////////////// 1000 = 1 second //////////////
if (ttime - switchVals[counter] > 100){
print("In to switch block\n");
if (switchVals[switcher]==0){
switchVals[returnVal] += 1;
if (switchVals[returnVal] >= 25){
switchVals[switcher] = 1;
}
}
else
if (switchVals[switcher]==1){
switchVals[returnVal]  -= 1;
if (switchVals[returnVal] <= 10)
switchVals[switcher] = 0;
}
switchVals[counter] = 0;
}
return switchVals[returnVal];
}

fe.add_ticks_callback( "textTickles" );

function textTickles( ttime ) {

switchVals["rTEST"] = increment(ttime,"tTEST","swTEST","rTEST");
print(switchVals["rTEST"]+"\n");
}

I'll probably refine it a bit more after work.
Title: Re: Passing a "global name" to a function?
Post by: Luke_Nukem on June 09, 2014, 02:46:34 AM
Hey dude. I've refined my example above quite a bit, works an absolute charm!

See here; http://forum.attractmode.org/index.php?topic=8.msg28#msg28 (http://forum.attractmode.org/index.php?topic=8.msg28#msg28)