Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Setter Links - Affecting Lists

Hey all,

Been playing around with Twine 1.4.2 / Sugarcane for a day or two, and read a few tutorials. I'm now trying to work out how to change a list when I click a link. e.g. to update a party list.

I've played with setter links to set the party leader's stats:
Do you like [[fighting|Intro][$leader = $warrior]] monsters, [[stealing|Intro][$leader = $rogue]] from the rich or [[zapping|Intro][$leader = $mage]] evil-doers?

and then started a party using
<<set $party = [$leader.name]>>

and later on add characters with something like
<<set $newchar = $warrior>>

<<set $newchar.name to either($names)>>

You meet <<$newchar.name>>the<<$newchar.class>>

<<set $party.push($newchar.name)>>

[[Location2]]

I then added a party stats passage accessible on the menu, and want to be able to remove a character by clicking a link next to their name on the party. Unfortunately I have no idea how to run a macro from a link... the following is my best guess but it doesn't work. The links appear correctly, but clicking them throws up an error.
Your Party has <<$party.length>> <<if $party.length is 1>>person<<else>>people<<endif>> in it:
<<$party[0]>>
<<if $party.length gte 2>><<$party[1]>> [[remove|Party Stats][$party.splice($party.[1],1)]]<<endif>>
<<if $party.length gte 3>><<$party[2]>> [[remove|Party Stats][$party.splice($party.[2],1)]]<<endif>>
<<if $party.length gte 4>><<$party[3]>> [[remove|Party Stats][$party.splice($party.[3],1)]]<<endif>>

<<$party>>

[[Back to Game!|previous()]]
Is there a way to do this?

Comments

  • You've cocked up the start parameters in the calls to the Array object's splice() method.

    Instead of this:
    $party.splice($party.[x],1)

    It should look like this:
    $party.splice(x,1)
    For example:

    <<if $party.length gte 2>><<$party[1]>> [[remove|Party Stats][$party.splice(1, 1)]]<<endif>>
    <<if $party.length gte 3>><<$party[2]>> [[remove|Party Stats][$party.splice(2, 1)]]<<endif>>
    <<if $party.length gte 4>><<$party[3]>> [[remove|Party Stats][$party.splice(3, 1)]]<<endif>>

    EDIT: Brain fart.  Disregard this bit.
    Additionally, consider this bit:
    [[Back to Game!|previous()]]

    That will not do what you expect if the player removes any party members.  The reason being that previous() yields the title of the last passage which is not the same as the current one.  What you'll need to do there, or for any menu that uses recursion or is otherwise comprised of multiple passages, you'll have to set a $variable with the title of the passage you actually want to return to and use that.  For example:
    [[Back to Game!|$return]]

    There are several ways in which you could set the return variable, but doing it automatically via tags is probably easiest.
  • Thanks again - that first bit works perfectly...

    Now an odd one, after testing the "remove" links worked, I hit the "back to game" button and it seemed to work fine without any of the changes you mentioned - i.e. even after hitting remove two or three times, it still took me back to the passage I was in prior to going to the Party passage. Has the functionality of it changed recently?
  • Aimless wrote:

    Now an odd one, after testing the "remove" links worked, I hit the "back to game" button and it seemed to work fine without any of the changes you mentioned - i.e. even after hitting remove two or three times, it still took me back to the passage I was in prior to going to the Party passage. Has the functionality of it changed recently?


    Ah.  Yes, sorry, I suppose if you're simply recursing into the same passage over and over, then previous() will work fine.  The more complicated solution is only required if your menu spans different passages.
  • Thanks for the clarification - to be honest I'll probably be adding a "character stats" link to each party member at some point soon so the better version will come in really handy.
Sign In or Register to comment.