Hi all,
I noticed something strange in a project I'm working on that uses javascript objects.
I declared multiple (nested) objects at the beginning of a file:
<<set $Alice = {Eliza:{tru:2, adm:-1, aff:3, att:-3}}>>
<<set $Eliza = {Alice:{tru:-1, adm:2, aff:1, att:1}}>>
<<set $matrix = {Alice:$Alice, Eliza:$Eliza}
and then I print out the $matrix object in a later passage:
<<print JSON.stringify($matrix)>>
...I noticed that the first element, Alice, was printing as 0. If I swapped Alice and Eliza, then Eliza was printing as 0. So I declared a "dummy object" before everything (
<<set $foo = {}>>
) and finally everything printed out the way I expected. Seems like a bug! Or is there some sensible explanation for this behavior?
I have a second question as well. It seems that indexing objects by Twine variables, e.g.
<<print JSON.stringify($matrix.$char)>>
where $char can be set to "Alice" or "Eliza", does not work. I get a red Twine error saying that $char is not defined. Is there any way to make this work right?
Thanks,
Chris
Comments
Also, I recommend using JSON.stringify(variableName, null, true) instead of plain JSON.stringify(variableName), because it prints in a more readable fashion.
As for the first error.... would it be OK to show me a HTML file where the bug occurs? I can't seem to replicate it.