Deleting, modifying and re-adding element to a list

Twine Version: SugarCube 2.30.0

So I have this code, which works to cycle through things I like and display them.

:: StoryTitle
Simplified test

:: StoryData
{
  "ifid": "E5D6D657-EB7B-41E1-BB94-A1F539B702C5",
  "format": "SugarCube",
  "format-version": "2.30.0",
  "start": "The Start"
}

:: StoryInit

<<set $likes = [
  "I like pizza.",
  "I like ice cream.",
  "I like pop.",
  "I like soda.",
  "I like cola.",
  "I like cookies.",
  "I like pretzels.",
  "I like candy.",
  "I like cereal.",
  "I like lemonade.",
  "I like licorice.",
  "I like chocolate.",
  "I like dark chocolate.",
  "I like white chocolate."
  ]
>>

:: StoryMenu [StoryMenu]

:: Widgets [widget nobr]

<<widget picklike>><<nobr>>
<<set $idx = random(0,$likes.length/2)>>
<<set _thislike = $likes[$idx]>>
<<run $likes.delete(_thislike)>>
<<print _thislike>>
<<run $likes.push(_thislike)>>
||||$likes.length
<</nobr>>
<</widget>>

:: The Start

<<picklike>>

[[Like more stuff.|The Start]]

:: PassageFooter

:: PassageHeader

:: Stylesheet [stylesheet nobr]

The problem comes when I want to say “Again, I like…”

Adding in this code

<<if not _thislike.contains("Again, ")>><<set _thislike = "Again, " + (_thislike)>><</if>

Creates a situation where <<run $likes.push(_thislike)>> doesn’t work! But if I put a <<run likes.push(“!”)>> before it, everything works as intended.

I’m glad I got stuff to work, but I’m wondering why doesn’t the first run $likes fire with the modified string, and how would I fix that?

Thanks!

When you do this, is _thislike defined before the <<run $likes.push(_thislike)>>?

(unrelated, but if you tag a passage with nobr, you don’t need to also use the <<nobr>> macro)

EDIT : after messing around with the code, I’ve made a second widget with the Again, element (though in a way that isn’t added to back to the array).

<<widget picklikedos>>
	<<set _index to random(0, $likes.length/2)>>
   	<<set _thislike to $likes[$idx]>>
    <<run $likes.delete(_thislike)>>
    <<run $likes.push(_thislike)>>
    <<print "Again, " + _thislike>>
	||||$likes.length
<</widget>>

If you want the Again, included inside the array:

<<widget picklikedos>>
	<<set _index to random(0, $likes.length/2)>>
   	<<set _thislike to $likes[$idx]>>
    <<run $likes.delete(_thislike)>>
    <<print _thislike>>
    <<if not _thislike.includes("Again, ")>>
          <<set _thislike to "Again, " + _thislike>>
    <</if>>
    <<run $likes.push(_thislike)>>
	||||$likes.length
<</widget>>
1 Like

Thanks!

I compared my code to yours, and … it looks like the main difference is, I had < and you had <>.

It looks like a bug in tweego that it didn’t detect this. I should probably report a bug there or check for a newer version.

So I appreciate the poke and verification I was at least on the right track.

Tweego hasn’t updated since 2020, but you may want to use the last version of SugarCube (which is at 2.37.3 since last July) :slight_smile:

1 Like

TweeGo doesn’t execute the contents of a (standard) Passage. It only assembles the contents of the Passages together, along with a Story Format Template, to generate a Story HTML file.

A (standard) Passage can contain pure nonsense and TweeGo wouldn’t care, the runtime engine of the selected Story Format on the other hand will. :slight_smile:

1 Like