Script variables, why not?


  • retired

    Pollution's suggestion of a dice variable got me thinking about implementing variables in scripts. This would be a fairly decent step forward, and I believe would help a lot. The idea is to have something (at first, we'll keep this to integers although strings can also be added) that you can manipulate and check at your will. A very very rudimentary example of a mob prog with a variable:
    [code] declare hitpower $hitpower = dice(2,5); if level $n > 50 $hitpower = $hitpower * 2 if level $n > 100 $hitpower = $hitpower * 3 endif mob echoat $n $I whaps you in the jaw! mob echoaround $n $I whaps $N in the jaw! mob damage $n $hitpower $hitpower lethal [/code] This could be a fight script, a kill script, or a random script for some thug that likes to go around picking fights.

    The variables will be a data structure allocated upon script execution, and all of a script's variables will be cleared when it ends. Thus no memory management to deal with for our nice imms.

    Once variables are implemented, it will be an easy transition to add loops, although this might pose to be a bit more problematic due to the possibility of builders creating infinite loops (yuck!). We might be able to add a safeguard in, though.

    Any thoughts, suggestions are welcome. This is something I'm going to implement along with script expansions and scripts on tokens.

  • registered

    A way to fix loops so they can't go infinite is to make three types of loops… counting loops, list loops and conditional loops. Counting loops would have a format as such ```` for LABEL VAR START STOP STEP ... [exitfor LABEL] ... endfor LABEL ```` where… * LABEL = the name of the for loop to allow for nested loops * VAR = the loop variable name, references as $VAR in the loop * START = initial value (can be a numerical variable or ifcheck) * STOP = final value (similar to START) * STEP = a non zero step (similar to START, a ZERO value should do the loop for START only then exit) * exitfor LABEL = exits the loop execution for that loop (basically, skips to the ENDFOR LABEL) A list loop would iterate through a list expression (such as mob contents, room objects, online players, etc) ```` list LABEL VAR LIST ... [exitlist LABEL] ... endlist LABEL ```` where… * LABEL = the name of the list loop to allow for nested loops * VAR = the loop variable name, references as $VAR in the loop * LIST = the list entity that provides the element to search. (this would utility the $( ) expression entity resolver, only accepting LISTs). * exitlist LABEL = exits the loop execution for that loop (basically, skips to the ENDLIST LABEL) This would take care to CHECK for infinite loops, by using a SEEN STAMP that is generated uniquely for that loop. When the current item looked at has a SEEN STAMP greater than or equal to the loop's generated stamp, it exits the loop. The reason for greater than or equal to is to account for the possibility that a nested loop within the current one manipulates said entity. Also, should the viewed entity be INVALID (such as a mob that was destructed), that forces a loop exit. The last loop has the main potential for becoming infinite. ```` while LABEL IF-CHECK [and IF-CHECK] [or IF-CHECK] ... [exitwhile LABEL] ... endwhile LABEL ```` where… * LABEL = the name of the while loop to allow for nested loops * IF-CHECK = ordinary if-checks * exitwhile LABEL = exits the loop execution for that loop (basically, skips to the ENDWHILE LABEL) The test portion of the loop operates just like an IF statement, only a false value would look for the ENDWHILE LABEL line instead of ELSE or ENDIF.

Log in to reply