quick question about scripts

S

spoonsinbunnies

Guest
so I know I can use argument_count to alter how many arguments a script uses, And I know that the actual arguments aren't used, instead they are copied. So how can I change the directly stated argument instead of the copy. Example of what I mean below because my wording is terrible here.
Code:
if u.recordingtimeline=true && u.timer/10 = round(u.timer/10)
for(i=0 i<argument_count i++)
{
    timeline[u.timer/10,i]=argument[i]//saving
}
if u.recordingtimeline=false
for(i=0 i<argument_count i++)
{
    argument[i]=timeline[u.timer,i]//reloading
}
now obviously if I call this function with x y and image_angle the saving part will work fine, but im fairly sure that the recalling part will alter the copy of the argument the script creates, is there a way around this, does adding it into a string work to reference the variable? Im sure I could toy with this until I get it working but Im sure someone who has more knowledge on scripts could give me more knowledge so I understand why the answer is how it is, than idk this works just because.
 

Greenwitch

Member
No, you can't reference a variable in GM nor can you pass by reference. There are workarounds, like you can still point to an array or a data structure (ds_) to access them directly somewhere else. When using arrays to do this, make sure to use the [@ accessor as it tells GM that you want to edit the original array instead of the copied one.

Code:
var a, b;
a[0] = 5;
b = a;
b[0] = 3; // a[0] is still 5
b[@ 0] = 3 // now a[0] is 3
 

chamaeleon

Member
so I know I can use argument_count to alter how many arguments a script uses, And I know that the actual arguments aren't used, instead they are copied. So how can I change the directly stated argument instead of the copy. Example of what I mean below because my wording is terrible here.
Code:
if u.recordingtimeline=true && u.timer/10 = round(u.timer/10)
for(i=0 i<argument_count i++)
{
    timeline[u.timer/10,i]=argument[i]//saving
}
if u.recordingtimeline=false
for(i=0 i<argument_count i++)
{
    argument[i]=timeline[u.timer,i]//reloading
}
now obviously if I call this function with x y and image_angle the saving part will work fine, but im fairly sure that the recalling part will alter the copy of the argument the script creates, is there a way around this, does adding it into a string work to reference the variable? Im sure I could toy with this until I get it working but Im sure someone who has more knowledge on scripts could give me more knowledge so I understand why the answer is how it is, than idk this works just because.
Everything is passed by value in GMS, changing things inside a script won't affect any variable just because it was an argument in the call. Changing the content of various data structures (including arrays using the @ accessor) is possible because you work with references to them which is the value being passed in, but you cannot change which data structure a variable contains, etc.

If you absolutely must/need to do something along the lines of knowing which variable is being used you have some limited support in the form of being able to use strings to get and set instance variables and global variables (but not local variables) using the variable functions. But rather than going down that route, I'd strongly consider looking into alternatives. For example, you're talking about wanting to set x, y, and image_angle from within the script and you wonder how to do that if they were passed by argument.Rather than doing that, I'd ask myself whether they need to be passed by argument in the first place, given that instance variables are implicitly available for the instance whose event code is executing, and you could just not pass them, and just refer to them by name instead inside the script.
 
Top