Quick disclaimer, I'm a coding novice. I'm trying to be better but I end up kludging and brute forcing everything to get by. So with that out of the way:
I've run into some behavior with assigning variable values in Nexus that I don't understand. I know how to fix my code now but I would like to understand what is happening.
I have some variables that I'm assigning to zero onload by entering the script and calling the function, it looks something like this:
set_variable('val1',0);
set_variable('val2',0);
set_variable('val3',0);
I then use a trigger to assign their values using simplified scripting (the pull-downs and such), so their values might be 1,0,1 respectively.
I then call another function that creates a variable with this script:
set_variable('val4',get_variable('val1')+get_variable('val2')+get_variable('val3'));
The problem is that when I pull the value for val4 it gives me 101... I'm expecting 2. So it appears that it is treating the values as strings when it adds them together. My fix for this is to change my trigger to set their values with script (instead of the modify variable) i.e.
set_variable('val1',1);
and so on.
I can work with this, I'll just change my triggers to use the 'enter script' option, but I was hoping to understand this as it appears that I could be missing something fundamental.
Thanks.
0
Comments
But yeah, I don't think you're really missing anything. I've never ran into that problem before because I've always used the script option and set_variable rather than simplified scripting and val4 has always come out as the proper sum of the other values when you use the enter script option.
I'm more inclined to think you get a concatenation of the variables rather than summation because of something weird going on behind the scenes with the modify variable option in simplified scripting.
Or I could just be horrible at understanding things.
So, it looks like the problem you had occurs because the modify variable option in simplified scripting defaults to setting the variables as strings instead of numbers, making it so your '+' operators performed concatenation instead of addition.
If you don't want to change all of your simplified scripting triggers to execute script options, you can change your
set_variable('val4',get_variable('val1')+get_variable('val2')+get_variable('val3'));
to
so your variables are converted to numbers for that operation.
ETA: In all other instances what you'll get from get_variable('val1') is still going to be a string unless you run it through the Number function or something similar (assuming you're still setting the variables using simplified scripting).
If you're ever in doubt of what your variables types are, you can always do
print(typeof variable, 'orange'); for a quick check
Very helpful, this is exactly what I was looking for. Thanks!