How do i make this work? (variable times variable)

Hey everyone, I’m relatively new to Twine and I’m working on a project for history and I want to add a store and for this I am trying to make it so after the player selects a number it will times the bacon price with the bacon quantity?

> (b4r:"solid","solid","solid","solid")+(b4r-size:2,2)+(b4r-colour:black,black,black,black)[Items: (live: $Item_cont)[$Item_cont]] 
> 
> (b4r:"solid","solid","solid","solid")+(b4r-size:2,2)+(b4r-colour:black,black,black,black)[Price: (live: $bacon_cont)[(set: $price to $price + $item_price)]]
> (set: $bacon_price to 1.00)
> (set: $bacon_cont to 0) 
> (b4r:"solid","solid","solid","solid")+(b4r-size:2,2)+(b4r-colour:black,black,black,black)[Price: $$bacon_price] 
> [<img src="https://png.pngtree.com/png-vector/20240825/ourmid/pngtree-crispy-bacon-clipart-illustration-digital-watercolor-style-food-png-image_13613539.png" width="150" height="150" style="position: absolute; top: 3; left: 0;">[(dropdown: 2bind $bacon_cont ,"0","1","2","3","4", "5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20")]]
> 
> (set: $item_price to $item_price + $b_command)
> 
> (set: $b_command to $bacon_cont * $bacon_price)```

You should be getting an error that you can’t multiply by a string value.

Drop-down menus only accept strings.

The moment you need to do math with a string value, use the (num:) macro to convert the string to a number.

(set: $price to 1.00)
(set: $amount to "0")

Price = $$price
(dropdown: 2bind $amount ,"0","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20")

(link-rerun:"UPDATE")[(set: $total to $price * (num: $amount)) --> Total = $$total]
1 Like

Thanks

1 Like

@Thehumankraken
If you don’t mind, I will make some suggestions regarding other parts of your code example.

1: Applying CSS Styling to the content of a Passage.

While there is nothing wrong with using Harlowe’s macros to apply styling to content…

(b4r:"solid","solid","solid","solid")+(b4r-size:2,2)+(b4r-colour:black,black,black,black)

…however, doing so can become quite verbose. Which is why I often advise using a known Named Hook to identify where to apply specific styling…

|stat>[Price: $$bacon_price] 

…and using a related CSS Rule in the project’s Story > Stylesheet area to apply styling to that identified area…

tw-hook[name="stat"] {
	border-style: solid;
	border-width: 2px;
	border-color: rgb(0, 0, 0);
	display: inline-block;
}

2: Refreshing the current content of the page after Reader interaction.

The timer family of macros (like (live:) and (after:)) are not a good choice when it comes to refreshing the content of specific areas of the current page after the Reader has made a selection. A better solution is to use a Named Hook to identify the area of the page that needs refreshing, and then using one of the page updating macros like (rerun:) or (replace) to change the content of that area.

eg. the following basic example demonstrates the principle

(set: $count to 0)\
|count>[Count: $count]

(link-repeat: "Increase Count")[(set: $count to it + 1)(rerun: ?count)]
2 Likes