For starters, just to answer the first question, local variable are invented as opposed to your purpose so that they
never carry between scripts. Imagine that local variables declared with "var" were carried between scripts, and suppose that you were writing a general-purpose script that can be used anyone. Now you must think out local variable names very carefully; for example, you could no longer use "i" for your loop counter because it would overwrite the "parent" scripts's i when called from inside a loop. You would end up in choosing some random name, say i_j8314jfs4123, hoping that no one else in the world would use the same name. That's just the second advent of global-variable hell.
If you want to take multiple outputs out of a script, try making the script return those outputs packed into one value. For example:
//Logical Shift Right
//use: lsr(value)
//return value: (C_flag << 8) | (shifted value)
var C;
C = argument0 & 1;
return ((argument0 >> 1) & $FF) | (C << 8);
//Bitwise Rotation Left
//use: rol(value, C_flag)
//return value: (C_flag << 8) | (rotated value)
var C;
C = floor((argument0 << 1)>>8);
argument0 = ((argument0 << 1) & (1 << 8)-1) | (C << 8);
Then unpack it each time you call a script. E.g.:
var C;
C = 0;
stage = vA;
vA = lsr(vA, C);
C = vA >> 8; vA &= $FF; // Unpack the return value
v000 = rol(v000, C);
C = v000 >> 8; v000 &= $FF; // Unpack the return value
...
If you don't like to pack and unpack values each time, you can simulate "pass by reference" method by passing a data structure that contains values, So the child script can modify the value in the data structure. E.g.:
//Logical Shift Right
//use: lsr(value, list)
//where the first element of list is the carry flag.
ds_list_replace(argument1, 0, argument0 & 1); // Set the carry flag
return (argument0 >> 1) & $FF; // Return the shifted value
Call it like this:
var registers;
registers = ds_list_create();
ds_list_add(registers, 0); // The first element is carry flag
vA = lsr(vA, registers);
v000 = rol(V00, registers);
...
// Check the C flag at the end
C_flag = ds_list_find_value(registers, 0);
if (C_flag) vX = vA;
ds_list_destroy(registers);
Edited by torigara, 02 March 2012 - 06:17 AM.