Howdy, Stranger!

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

Sugarcube - Passage Link that just changes variable

Imagine you're programming a store. You have a passage named Store and maybe five items for the player to choose from to buy.

So you have options to buy Lamp or Table or Microwave but you have to create a passage for each item you buy, when really all you need to do is <<set $itemBought to "Table">>. And you don't need to have the "You've bought the BLANK" in multiple passages, so the literal buying passage would just be

<<set $itemBought to "Table">>
<<goto YouHavePurchasedSomething>>

Now, two lines of good doesn't need a passage, and if your store has a lot more than just five items, making a passage for every single one is beyond tedious. Is there a possible way that when you have a multiple links for Buy the BLANK, clicking it just changes a variable?

I would imagine it to be something like <<set $itembought to "Table">>, but all that does is create a passage titled <<set $itemBought to "Table">>, and I don't know where the passage destination would fit into that. A different macro might be needed.

Any help is greatly appreciated.

Comments

  • SugarCube has excellent documentation for both the 2.x and the 1.x versions of the story format, and includes many examples on how to use it's features.

    If you read the documentation related to links (v2.x or v1.x) you will see two link types listed as Link w/ Setter and Link w/ Text & Setter, both of which do exactly what your are asking for.
  • greyelf wrote: »
    SugarCube has excellent documentation for both the 2.x and the 1.x versions of the story format, and includes many examples on how to use it's features.

    If you read the documentation related to links (v2.x or v1.x) you will see two link types listed as Link w/ Setter and Link w/ Text & Setter, both of which do exactly what your are asking for.

    Thank you very much, I didn't know such a resource was available. This will greatly help my stories.

    However, it does leave a "broken link warning" over the passage this macro is put into. Is there a way to amend that?
  • However, it does leave a "broken link warning" over the passage this macro is put into. Is there a way to amend that?
    The Twine 2 application's Auto Create Missing Passage, Passage Map Linking and Broken Link Warnings only support the markup link types common to all three story formats.

    There are some hacky tricks to try and get around this issue but the simple answer is no.
  • If it doesn't trigger any unwanted code, you can just set the link to return back to the store-passage:
    [[Buy|store][$item.bought = true, $money -= $item.cost]]
    

    The full setup would look something like:
    <<set $money = 30>>
    
    <<set $table to {
    "name": "Table",
    "bought": false,
    "cost": 25
    }>>
    
    <<set $lamp to {
    "name": "Lamp",
    "bought": false,
    "cost": 10
    }>>
    
    <<set $items to [$table, $lamp]>>
    
    
    
    <<for _i to 0; _i lt $items.length; _i++>>
    	<<if $items[_i].bought === false and $money >= $items[_i].cost>>
    		<<capture _i>>
    			<<print $items[_i].name>>: Cost:<<print $items[_i].cost>> - [[Buy|store][$items[_i].bought = true, $money -= $items[_i].cost]]
    		<</capture>>
    	<<elseif $items[_i].bought === false>>
    		<<print $items[_i].name>>: Cost:<<print $items[_i].cost>> - Too expensive.
    	<</if>>	
    <</for>>
    

Sign In or Register to comment.