I am using ChapelR’s simple inventory (v2.3.0) but want to have my own inventory display passage so I can customise how the inventory is displayed. Rather than the default list of text strings I want a list of links which when clicked add a description of the clicked item in a paragraph below. Clicking a different item would then overwrite the description. I have the following code which displays the list of text strings as links but then gives an error when I click on one:
With a quick glance the first trouble I think: when you’re clicking the link, $i has changed to the last value the loop allows for.
You might use the <<capture>> macro http://www.motoslave.net/sugarcube/2/docs/#macros-macro-capture to avoid the problem.
I’ve not tested it for lack of time right now, but you can try something like this in your loop:
Whenever you need to read the value of a variable within an asynchronous block of code that will have been changed by the time the code is run, you need to capture the variable and thus its value.
Asynchronous here meaning code that runs out-of-sync with the normal sequence of events. For example, the code within a <<link>> macro, since it runs only whenever the link is activated by the player.
Here’s a corrected example using <<capture>> with a temporary loop variable: