@Fergus: OK, so it is what I thought. But it only contains values for pieces that are present; labels of pieces that are not on the board do not get value 0 associated. This makes it tricky to use.
What is the best way to test whether an associative array contains a given key? In JavaScript I would just copy array[key] to a variable, and then test if that variable is undefined.
But testing for an undefined value seems to be difficult in GAME code. In seems that variables to which you assign an undefined value disappear, and that #name for nonexisting variables evaluates to the string literal "#name". Is this guaranteed behavior? I.e., would it make sense to write
if != #name "#name":
to only process a variable when it does exist / was not assigned an undefined value?
Or can the problem be avoided by reading the value of the variable using var rather than #? Is there any guaranteed behavior for using var on a non-existing / undefined variable?
If not, it seems to be useful to have an operator (let's call it number) that would evaluate to the numerical value of its operand, but to zero if the operand was not numeric, or undefined. So that I could write something like number elem #key piececount, which would give me the expected 0 if #key was not a valid key in the piececount table.
@Fergus: OK, so it is what I thought. But it only contains values for pieces that are present; labels of pieces that are not on the board do not get value 0 associated. This makes it tricky to use.
What is the best way to test whether an associative array contains a given key? In JavaScript I would just copy array[key] to a variable, and then test if that variable is undefined.
But testing for an undefined value seems to be difficult in GAME code. In seems that variables to which you assign an undefined value disappear, and that #name for nonexisting variables evaluates to the string literal "#name". Is this guaranteed behavior? I.e., would it make sense to write
to only process a variable when it does exist / was not assigned an undefined value?
Or can the problem be avoided by reading the value of the variable using var rather than #? Is there any guaranteed behavior for using var on a non-existing / undefined variable?
If not, it seems to be useful to have an operator (let's call it number) that would evaluate to the numerical value of its operand, but to zero if the operand was not numeric, or undefined. So that I could write something like number elem #key piececount, which would give me the expected 0 if #key was not a valid key in the piececount table.