+1 vote
by (170 points)

Hi. Sorry for the probably dumb question but English is not my first language and I'm not a programmer at all.

 I'm trying to use the delete Array method described here: https://www.motoslave.net/sugarcube/2/docs/object-methods.html#array-arrayprotodelete. I have a Twine2, Sugarcube 2.25 setup.

What I want is setting up a random explore system that allows the player to travel in random places, but also to remember where the player has been so that he can visit a place he knows wherever he wants, and the random explore system only brings the player to unexplored places.  So, in my Storyinit file I set up an array like this:

<<set $places = ["springs", "village", "library", "mountaintop", "swamp"]>>

Then in a paragraph named "randomexplore" I put the following code:  

<<if visited("VILLAGE")>>$places.delete("village") <</if>>
<<if visited("LIBRARY")>>$places.delete("library")<</if>>
<<if visited("MOUNTAINTOP")>>$places.delete("mountaintop") <</if>>
<<if visited("SWAMP")>>$places.delete("swamp") <</if>>
<<if visited("SPRINGS")>>$places.delete("springs") <</if>>
<<set _randomPlaces = $places.random()>>

<<if _randomPlaces == "swamp">> You found a new place.[[SWAMP]]
<<elseif _randomPlaces == "library">>

... and so on.

The system is able to take me to one of the five random places, but it can't delete the places I actually visited. Instead it only shows the code as text, like $places.delete("library") in the middle of the paragraph without executing it. What am I doing wrong? Also, supposing it will work, after the last place has been visited, what code should I use to identify that the array is void and to let the player know he visited all of the places? Thank you in advance.

1 Answer

+2 votes
by (68.4k points)
selected by
Best answer

You need to use the <<run>> macro (or <<set>>).  For example:

<<run $places.delete("something")>>


Using your village example:

<<if hasVisited("VILLAGE")>><<run $places.delete("village")>><</if>>

NOTE: I switched visited() for hasVisited(), since you only need to know if the player has been there, rather than how many times.

by (170 points)
It works now, thanks!
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.