No announcement yet.

dynamically creating new variables

  • Filter
  • Time
  • Show
Clear All
new posts

  • [GUIDE] dynamically creating new variables

    To dynamically create a variable you need to understand 3 commands call/spawn, compile, and format .

    The call command executes a proceeding block (code inside of curly braces { }) with preceding arguments passed in _this.
    Call will execute the code and continue the script after it finishes executing. The spawn command has the same function except it will run the code parallel to the parent script, allowing you to run two loops simultaneously in the same script. The syntax is:
    _scriptHandle = arguments call {code block};
    Compile is straightforward. It takes a string and converts it to a curly bracket code block.
    _code = compile " Yink = 'example';";
    Would become:
    _code = {Yink='example';};
    And finally format. Format allows you to change characters in a string via tokens. Syntax Is as follows:
    _string = format ["this uses %1 to \n%2", value, value];
    So the proceeding argument to format is an array with the first index being a string and proceeding indexes are corresponding values to respective tokens. The \n is a structured text command that starts a new line.

    So combining these three commands we can create variables on the fly.
    Here's the final solution:
    [ ] call compile format ["var%1 = _number;", 6];

    the only use I found for this method is creating temporary global variables where local variable (in a single script) cant be accessed (im pretty sure in event handlers you cant pass in local variables/sleep counters) at least it doesnt work for me

    here's an example:
    tempNumber1 = round(random 4);
    tempNumber2 = round(random 99);
    letterArray = ["a","b","c","d","e"];
    _value = _this select 0; //lets say this is 5
    _handle = [_value] call compile format ["%1%2 = %3;",letterArray select tempNumber1, tempNumber2,_this select 0]; // returns for example a56 = 5
    tempnumber1 and tempnumber2 are global variables even though i could of used them as local variables (by passing them into the arguments of call.
    but _value is a local variable, which can be passed in by means of arguments from wherever this script was called E.G. nul = [this] execVM "variable_spawn.sqf"
    using this method we can pass in local variables into a global namespace without worrying about overlapping variables.

    you dont have to worry about your variable being written over because they change everytime the script fires.
    I dont see this being widely used but could come in handy sometimes, like if you might execute the script more than once at a time and sleep counters cause it to overlap.
    Last edited by Yink; 05-04-2014, 10:27 PM.
    Yink | Vanilla Pilot | Killer of Stirling | CS GOD | Projects Team

  • #2
    Re: dynamically creating new variables

    Whoa....gonna have to re-read this in the am. Thanks Yink!
    |TG-189th| Unkl
    TG PrimerArmA Game Officer189th Infantry Brigade Former Irregular
    Submit a Ribbon Nomination!
    "this is on par with groups you have to join to get the quality of gameplay and i really enjoyed it" - random dude
    "Remember when the threat has spotted you, smoke first then bring the explosive rounds to bear. When the threat has not spotted you its bang first and smoke after to extract." - Wicks




    TeamSpeak 3 Server




    Twitter Feed