keeping variables and their values across frames
Why is this useful:
- you could want to have a variable that remembers if something particular happened in the previous frame
- it's not always possible to quickly calculate the content of a variable as a function of the state of one frame only
- with arrays, one might want to accumulate values that cannot be calculated quickly within one frame only
- simple state machines
So the new behaviour would be that by default all top-level variables are kept across frames. This could be done in a couple of ways:
CoffeeScript.compile and CoffeeScript.eval accept a "sandbox option", which might help in creating a scope that maintains state across executions? I didn't look much into that but it's likely to work that way. See https://www.google.co.uk/search?q=CoffeeScript.compile+sandbox and http://coffeescript.org/documentation/docs/coffee-script.html
- create a scopeAcrossFrames object that holds all the variables that will be preserved across frames
- in the code manipulation step, find all variables declared in the first var block. Coffescript automatically aggregates all "top-most" variables in a block of var declarations at the top.
- for each variable declaration in the block of vars of above, first check whether thisAndThat already exists in the global scope or in the scopeAcrossFrames object. If no, then the variable is created and initialised in the scopeAcrossFrames object. If yes (it does exist in the global state already), then skip it. So for example: var thisAndThat; at program translation time if the thisAndThat variable is not defined in the global state already then a) var thisAndThat is removed and b) scopeAcrossFrames.thisAndThat is created and initialised with null
- If the thisAndThat exists in the scopeAcrossFrames object (because it's just been created just now or before), then replace all the occurrences of thisAndThat with scopeAcrossFrames.thisAndThat (or with this.thisAndThat if you choose the way of calling call(scope object) passing a global scope object)
Note that while the user types his intended variable name, variables for all the substrings will also be created. This is probably not a problem for
Note that something simple can already be done:
✓doOnce ->
window.a = 0
window.a++
ball window.a /1000
in fact, a more minimal example is
✓doOnce ->
window.a = 0
a++
ball a /100
which, again, only works as soon as "a" is initialised in the global scope (by deleting the tick mark).
Once "a" is initialised in the global scope, it can be referenced anywhere in the user sketch without problem.
Once "a" is initialised in the global scope, it can be referenced anywhere in the user sketch without problem.
This is actually totally not a bad way to do things.