Howdy, Stranger!

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

Dynamic list of links

Hi,

I'm using Sugarcube 2, and I'm display a list of clothing that the user can choose to wear using a <<for>> loop and accessing an pre-configured array variable

eg. (formatted to aid readability)
<<for ...>>
  <<set $csobj=...>>
  $csobj.name 
  <<click "Wear">>
    <<wearClothing $csobj.name>>
    <<replace "#clothing_selector">>
      <<display "Clothing Selector">>
    <</replace>>
  <</click>>
<</for>>

The list displays ok, but clicking any "Wear" link acts as if the last item was clicked. This is because the reference to $csobj.name in the <<wearClothing>> macro tag is being stored as a reference, rather than the current value.

I need to somehow have <<wearClothing>> execute passing the name of the clothing item it corresponds to.

Help please?

Comments

  • The code within the <<click>> macro is not process until after the Reader clicks on the link, so the value of $csobj (and the related $csobj.name property) will be equal to whatever it was at the end of the for loop, assuming you have not changes it's value manually in the time between the end of the for loop and the Reader's click.

    You need to use a <<print>> macro to dynamically generate each <<click>> macro and its contents. Note the usage of single quotes to build the String being passed to the <<print>> macro and that the value of $csobj.name is being wrapped in double quotes.
    <<print '<<click "Wear">><<wearClothing "' + $csobj.name + '">><<replace "#clothing_selector">><<display "Clothing Selector">><</replace>><</click>>'>>
    

    note: The above code was not tested because I don't know what the <<wearClothing>> macro does.
  • @greyelf: That worked great. Thank you so much!
Sign In or Register to comment.