Wait Scope Difference? [Resolved]

Just noticed that while

str = "okay"
wait {
  msg (str)
}

is fine,

str = "okay"
SetTimeout (1) {
  msg (str)
}

produces an error message: Error running script: Error compiling expression 'str': Unknown object or variable 'str'?

Anyone have an explanation?


wait is special; it defines a block of code, and is implemented in C# as a special case.

SetTimeout is a function (defined in CoreTimers.aslx) whose second parameter is a script. It creates a timer, and saves the script you pass to it as a script attribute of that timer. And script attributes don't have access to the scope they were created in (unlike Javascript's scope-frame model, where they do)


Thanks mrangel, good to know. I can pass values in through game attributes:

game.str = "okay"
SetTimeout (1) {
  msg (game.str)
}

which works fine!


You could also do what I did, and create a version of SetTimeout that takes parameters:

  <function name="SetTimeoutParams" parameters="interval, params, script">
    timername = GetUniqueElementName("timeout")
    create timer (timername)
    timer = GetTimer(timername)
    SetTimerInterval(timer, interval)
    timer.timeoutscript = script
    timer.parameters = params
    SetTimerScript(timer) {
      this.enabled = false
      invoke (this.timeoutscript, this.parameters)
      JS.scrollToEnd ()
      destroy (this.name)
    }
    EnableTimer(timer)
  </function>

and then use it like:

SetTimeoutParams (1, QuickParams("str", "okay")) {
  msg (str)
}

(I've also done this with SetTurnTimeout as well, because it's useful to pass parameters to a callback)


agariogames

I really wanted to send a small word to say thanks to you for the fantastic points you are writing on this site. i returned to sports head basketball


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums