tmedwards / sugarcube-2

SugarCube is a free (gratis and libre) story format for Twine/Twee.
https://www.motoslave.net/sugarcube/2/
BSD 2-Clause "Simplified" License
177 stars 41 forks source link

After setting a variable to undefined, refreshing the page sets it to null #90

Closed BawdyInkSlinger closed 3 years ago

BawdyInkSlinger commented 3 years ago
:: StoryData
{
        "ifid": "5CA2DDDE-6270-44F3-BEE4-ADEE564275CB"
}

:: StoryTitle
Sandbox

:: Start
<<set $my_var to 5>>
$$my_var = $my_var

<<button "Continue" "Test Test2">><</button>>

:: Test Test2
Click the button, then refresh the page. You will see that it is set to null, not undefined.
<<run alert($my_var)>>

<<button "Set to undefined" `passage()`>>
  <<set $my_var to undefined>>
<</button>>

using the unset macro does not reproduce this issue.

Expected behavior: refreshing the page should alert undefined.

ChapelR commented 3 years ago

This is likely because undefined can't be serialized as it's not a valid JSON type, and is therefore not really supported by SugarCube. Still, it would probably be better if the value were deleted instead of being massaged into a null.

As a sort of stylistic rule though, null is what is generally recommended for when you want to set a value to "nothing" as undefined is used when the value has never been set in the first place. If you want to make something undefined, usually the delete operator (or <<unset>>) is used.

BawdyInkSlinger commented 3 years ago

Still, it would probably be better if the value were deleted instead of being massaged into a null

Yep, that's the behavior I would want.

greyelf commented 3 years ago

Yep, that's the behavior I would want.

If that is the case then why do you do a <<set $my_var to undefined>> instead of a <<unset $my_var>> ?

BawdyInkSlinger commented 3 years ago

I'm saying this as fallback behavior. I'd prefer it to remain set to undefined, but I'm accepting that JSON doesn't allow that.

tmedwards commented 3 years ago

Resolved by b64afa6.