Howdy, Stranger!

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

Making a 'shop' menu, or dynamically populating a list and reading what the player clicked back

Hello all.

So, I am having a good time with Twine 2. Using the default Harlowe 2.0.1 format.

Here is what I have run up against. I want to make a clickable menu of choices for the player to purchase from a shop. I have a passage where I populate a dm with weapons, in the format "weapon name", "damage" for a bunch of weapons.

While I have no issue putting them onto the screen, all linked up and clickable using a for loop and the (click:) command, I cannot figure out how to read back what the player chose later. Obviously if I try to read back temp variable, it doesn't work as the variable went out of scope long before the player clicked.

It would be fine if I could read back the text of the clicked link, but I don't know how.

Anyway, to make it short: I want to populate a shop with links to buy items, find out what item the player chose, and then move on with that info (deducting gold from their inventory, etc.), and for whatever reason this has me stumped.

Comments

  • No go, anyone?

    I have managed to figure out so many parts of the system, making an RPG system from scratch fairly quickly. But I still can't figure out how to dynamically populate a list of links and later determine the link clicked. I mean, I *could* simply hardcode the links, avoiding dynamic creation completely, but this will really limit just how many items I would include in the shop.
  • edited June 2017
    i dont know the harlowe system but basically you could do (with sugarcube)
    // say you have this nice array of items:
    
    vendorItems = [
         {"name":   "Sword", "damage": 30},
         {"name":   "Axe", "damage": 30},
         {"name":   "Bow", "damage": 30},
         {"name":   "Whatever", "damage": 30}
    ];
    
    
    <<for _i = 0; _i < vendorItems.length; _i++>>
        
    <<capture _i>>
    
        [["Buy " + vendorItems.name|passage_bought][$bought = _i]]
    
    <</capture>>
    
    <</for>>
    

    and after player goes to passage_bought, you can get the bought item's index in vendorItems array with $bought variable, for ex:
    // on passage, passage_bought:
    
    <<print `"You have bought a " + vendorItems[$bought].name`>>
    
    
  • Use code like the following to create your list:
    {
    (set: $stock to (array: "Sword", "Axe", "Bow"))
    (set: $selected to "")
    
    (for: each _item, ...$stock)[
    	(print: '(link: _item)[(set: $selected to "' + _item + '")(go-to: "Next")]')<br>
    ]
    }
    [[Next]]
    
    The above uses a (print:) macro to dynamically create the (link:) to get around the issue that variables referenced within the associated hook of things like a link are not evaluated until after the link has been selected. Also note the usage of two different quote types in the parameter being passed to the (print:) macro, in this case I used single quotes to delimit the overall String value and double quotes to delimit the inner String values.

    You can test the above example by creating a passage named Next and adding the following code to it.
    You selected (if: $selected is not "")[$selected](else:)[nothing]
    
  • I see! Thanks for the answers. I never thought of that approach, and the way I was trying to do this wasn't giving me the desired results.

    Thanks again!
Sign In or Register to comment.