Howdy, Stranger!

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

Trying to set a variable at the same time as a link is clicked (Harlowe)

So my approach to changing equipment in my RPG is going to look something like this:


You are using $Weapon.

You have these weapons:
(set: $iterator = 1)
(live: 2ms)[
(link-goto: $OwnedWeapons's $iterator, "Change Equipment")
(if: $iterator + 1 > $OwnedWeapons's length)[(stop:)]
(set: $iterator to $iterator + 1)
]

The thing that is troubling me is how I can get the game to know which link they click on. I don't want to develop a unique passage for equipping every piece of gear in the game. Anyone have any ideas about a way to implement this?

Comments

  • 1. There are a couple of issues with your current example:

    a. All the links your example creates will overwrite each other, one way to get around this issue is to use a named hook and (append:) macro combination.

    b. It does not handle the case when there are no items in the $OwnedWeapons array.

    c. It increments the $iterator variable after it has finished processing the array.

    A better version of your example, I added line-breaks and indents to make it a little more readable but those can be safely removed.
    You are using $Weapon.
    
    You have these weapons:
    |list>[]
    
    (set: $iterator to 1)
    (live: 2ms)[
    	(if: $iterator > $OwnedWeapons's length)[(stop:)]
    	(else:)[
    		(append: ?list)[(link-goto: $OwnedWeapons's $iterator, "Change Equipment")
    		]
    		(set: $iterator to $iterator + 1)
    	]
    ]
    

    2. You can replace your (link-goto:) macro with a (link:)/(set:)/(goto:) combination which will allow you to use a variable (eg. $item) to store which item was selected.
    You are using $Weapon.
    
    You have these weapons:
    |list>[]
    
    (set: $iterator to 1)
    (live: 2ms)[
    	(if: $iterator > $OwnedWeapons's length)[(stop:)]
    	(else:)[
    		(append: ?list)[{
    			(link: $OwnedWeapons's $iterator)[
    				(set: $item to $OwnedWeapons's $iterator)
    				(go-to: "Change Equipment")
    			]}
    		]
    		(set: $iterator to $iterator + 1)
    	]
    ]
    

    3. But the first thing you will notice is that $item will always equal the same value and this is because the value of $OwnedWeapons's $iterator within each link's (set:) macro is determined at the time the link is selected, not at the time the link was created.

    You can use a (print:) macro to dynamically create String that represents the (link:) (and its contents) which will allow you to insert the current value of the $iterator variable at the time the link is created. You will need to convert the $iterator variable from a Number to a String so that it can be appended to the (link:) String, you will also need to replace the double quotes delimiting the "Change Equipment" with single quotes.
    You are using $Weapon.
    
    You have these weapons:
    |list>[]
    
    (set: $iterator to 1)
    (live: 2ms)[
    	(if: $iterator > $OwnedWeapons's length)[(stop:)]
    	(else:)[
    		(set: $ndx to (text: $iterator))
    		(append: ?list)[{
    			(print: "(link: $OwnedWeapons's " + $ndx + ")[
    				(set: $item to $OwnedWeapons's " + $ndx + ")
    				(go-to: 'Change Equipment')
    			]")}
    		]
    		(set: $iterator to $iterator + 1)
    	]
    ]
    

    Now if you check the value of $item within the "Change Equipment" passage it should contain the correct value.
  • Thank you so much! I'm going to read this over for a while to get a solid understanding of it, but I appreciate you taking your time to explain it.
Sign In or Register to comment.