Howdy, Stranger!

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

Trying to make a simple craft system

Hi Twine lovers!

I am struggling at coding a very simple function with Harlowe.
I want item3 to be created once item1 && item2 are clicked.

Here's my code (not sure about the brackets in my first If statement, but doesn't work witout them either) :


[item1]<item1|
[item2]<item2|

(click: ?item1)[(set: $item1_click to true)]
(click: ?item2)[(set: $item2_click to true)]

(if: ($item1_click is true) and ($item2_click is true))[(set:$item3 to true)]
(if: $item3 is true)[item3]

Comments

  • Why not just do it like "If you have 'Item X' and 'Item Y', then show 'Craft Item Z button'"?

    Where are the item links? In an inventory passage? Are you going to have a lot of crafting and recipes?
  • The reason your example does not work is at the time the contents of passage being displayed is processed both of your variables ($item1_click and $item2_click) are not equal to true, there-for $item3 is not set to true and so item3 is not displayed.

    By default once a passage is displayed any conditional macros are not re-evaluated even if the variables they are based on are changed. So changing the value of $item1_click to true does not cause the (if: ($item1_click is true) and ($item2_click is true)) line to be re-evaluated.

    One way to solve this problem is to re-display the item list each time either item1 or item2 is clicked, thus the conditional line with be re-evaluated.

    You should always give your variables a default value before you use them within your code, and Harlow's overview explains how to do this using a passage with a tag of startup.

    a. Assigning default values to your variables: Create a new passage (I named mine Startup), assign it a startup tag and copy the following into it:
    {(set: $item1 to false)
    (set: $item2 to false)
    (set: $item3 to false)}
    
    b. Display your list: Add the following to the passage you want the item list to appear in:
    |items>[(display: "items logic")]
    
    c. Logic to display the items: Create a new passage named items logic and add the following to it:
    (if: $item1 and $item2)[(set: $item3 to true)]
    [item1]<item1|
    [item2]<item2|
    (if: $item3)[item3]
    (click: ?item1)[(set: $item1 to true)(replace: ?items)[(display: "items logic")]]
    (click: ?item2)[(set: $item2 to true)(replace: ?items)[(display: "items logic")]]
    

    Now if you click on both item1 and item2 then item3 will appear.
  • Thank you so much Greyelf! I have a better understanding of the logic now.
    I didn't now about the Startup/Footer/Header tags, it is so useful, especially for games!
Sign In or Register to comment.