0 votes
by (120 points)
I'm currently using Sugarcube 2.22.0. I'm on my phone so I don't have code to show but my question is ultimately about whether something is possible.

 

In my story I use repeated events that use random sentences to flesh out the characters. The skeleton of the events are the same, but I use widgets to add different sentences based on where the event is and which characters are present. The widget essentially defines an array, pushes sentences and plucks a random one. So if two characters are making small talk as they travel they'll say different things each time. I wanted repetitive events to at least inject some character

 

The widget defines the array at the beginning with <<set $array to []>>, then passes <<if>> checks, which then prompts a <<set $array.push("The restaurants around here are amazing", "I heard there was a robbery around here", etc), then it plucks one and returns it.

 

So my question is: is there a command that allows me to clear or delete the array at the end of the widget? I'm at work and suddenly realized that if it keeps pushing new items into the array eventually a character might say something reserved for someone else.

 

Thanks guys!

2 Answers

0 votes
by (63.3k points)

If the array isn't used outside of the widget, then you should be using a temporary variable, rather than a story variable, in the first place—i.e. _array vs. $array (see: Variables).

Regardless, if you always initialize the variable to an empty array at the beginning of the widget, then you're already starting with an empty array every time.  That said, there is the <<unset>> macro.

0 votes
by (148k points)

Without seeing your widget implementation I have have to assume based on your question that you want the contents of the $array story variable to only exist for the life-time of the widget execution.
I also have to assume (based on your question) that you are initialising the $array to an empty array at the start of the widget's code which means that any array contents generated in one call of the widget shouldn't effect the next call, unless the widget can recursively call itself. 

But to answer your question about clearing out the contents of an array story variable, you can use the that in one of three ways:

1. Re-initialise the $array story variable to an empty array.

The downside of this method is that the story variable still exists at the end of the passage rendering process which means that it will eventually be added to the History (and Save) system, which serves no real purpose if the $array story variable contains temporary data.

/% Initialise the array. %/
<<set $array to []>>

/% Use the array like normal. %/
<<set $array.push("value")>>

/% Re-initialise array to delete all the elements. %/
<<set $array to []>>

2. Unset the $array story variable.

You can use the <<unset>> macro to remove the story variable from the game, which means that it won't end up being added to the History (and Save) system.

/% Initialise the array. %/
<<set $array to []>>

/% Use the array like normal. %/
<<set $array.push("value")>>

/% Remove the story variable for the game. %/
<<unset $array>>

3. Replace the $array story variable with a _array temporary variable and optionally unset it.

The temporary variable is automatically excluded from the History (and Save) system.
optionally: If the widget can recursively call itself then you could use the <<unset>> macro to remove it.

/% Initialise the temporary array, thus automatically excluding it from History. %/
<<set _array to []>>

/% Use the temporary array like normal. %/
<<set _array.push("value")>>

/% [optionally] Remove the temporary variable for the game. %/
<<unset _array>>

WARNING: All of the above code examples were written from memory and have not been tested, they may contain syntax errors.

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...