New to Twine need help with bonuses in my RPG!

I am new to Twine and am having a little trouble with something, hopefully someone here can help.

I am building an RPG that works similarly to the Dungeons & Dragons 5e format. Most of what I want to include I have been able to figure out, but there are still some things I’m struggling with.

First some context, at the beginning of the game a player inputs their characters stats which will then give them bonuses throughout the game. I have used the following set-up to input the stats:

<<set $dex to 10>><<print $dex>>
( <<button “[+]”>><<set $dex++>><<replace “#stats-dex”>><<print $dex>><><>
| <<button “[-]”>><<set $dex–>><<replace “#stats-dex”>><<print $dex>><><> )

and I have calculated the bonuses like so:

<<if $dex == 16 or $dex == 17 >>
<<set $ste to 3>>
<<elseif $dex == 14 or $dex == 15>>
<<set $ste to 2>>
<<elseif $dex == 12 or $dex == 13>>
<<set $ste to 1>>
<>
<<set $ste to 0>>
<>

The problem I am having is actually applying these bonuses in game. I have set-up a way for the player to input a value and that value determines what happens next, but the value should be modified by the above bonus.

This is what I have so far:

What is your Stealth Roll? <<textbox “$stealth” “”>>


<<link “What Happens Next?”>>
<<if $stealth + $ste < 13>>
<<append “#result”>> <<goto [[You Have Been Found!]]>> <>
<>
<<append “#result”>> <<goto [[You Make it Through]]>> <>
<>
<>

The player inputs their value, then if that value + the bonus they gain from their stat is less than 13 they are taken to one passage, else they are taken to a different passage.

Unfortunately, it is not working. Instead of adding the 2 variables it is putting them together and checking if one of them is less than 13.

I think the problem is that my values are in the form of a string and I want integers/floats.

Does anyone know how to do this? Is there a better/different way to do this?

Please use the Preformatted text option in the toolbar when including code examples in your comment, it stops the forum’s software converting standard single & double quotes into invalid curvy (typographical) equivalents as well as persevere macro end tags. It also makes the code examples easier to read, and to copy-n-paste.

I will assume your original code examples looked something like the following…

<<set $dex to 10>><<print $dex>>
( <<button "[+]">><<set $dex++>><<replace "#stats-dex">><<print $dex>><</replace>><</button>>
| <<button "[-]">><<set $dex–>><<replace "#stats-dex">><<print $dex>><</replace>><</button>> )

<<if $dex == 16 or $dex == 17>>
	<<set $ste to 3>>
<<elseif $dex == 14 or $dex == 15>>
	<<set $ste to 2>>
<<elseif $dex == 12 or $dex == 13>>
	<<set $ste to 1>>
<<else>>
	<<set $ste to 0>>
<</if>>


What is your Stealth Roll? <<textbox "$stealth" "">>

<<link "What Happens Next?">>
	<<if $stealth + $ste < 13>>
		<<goto "You Have Been Found!">>
	<<else>>
		<<goto "You Make it Through">>
	<</if>>
<</link>>

note: I corrected the calling of the <<goto>> macros, as there is no point in using an <<append>> macro to inject them into the page’s HTML structure.

This is because the <<textbox>> macro always returns the end-user entered value as a String (text). Thus your <<if $stealth + $ste < 13>> line of code is actually concatenating a String value ($stealth) with a Numerical value ($ste) which due to the rules of data-type coercion results in the product of that concatenation being a String value, which you then compare to a numeric.

eg. If the end-user types 5 into the textbox and the current value of $ste is 3, then the following occurs…

"5" + 3 < 13	=>	"53" < 13

To overcome this issue you will need to convert the value contained within the $stealth variable into a numeric, but here’s where things get a little complex because if the end-user enters a value like “abc” instead of a value like “123” then the result of the conversion will be NaN (which stands for “Not a Number”).

Assuming that you want the value entered by the end-user to be an Integer then you can add JavaScript code like the following to your project’s Story JavaScript area, it creates a new function named setup.parseCleanInt() on the special setup object

setup.parseCleanInt = function (x, base) {
    const parsed = parseInt(x, base);
    if (isNaN(parsed)){
		return 0;
	}
    return parsed;
};

… which you can use like so…

<<set $stealth to setup.parseCleanInt($stealth, 10)>>
<<link "What Happens Next?">>
	<<if $stealth + $ste < 13>>
		<<goto "You Have Been Found!">>
	<<else>>
		<<goto "You Make it Through">>
	<</if>>
<</link>>

note: the above parseCleanInt() was sourced from the JavaScript: Convert String to Number article by Chris Pietschmann.

Also answered, somewhat differently than Greyelf, in my reply in your reddit post.

Thank you! I figured the problem was trying to add 2 Strings, but had no idea how to remedy that. It works now, much appreciated.

Yes, thank you, I did see that. It works now, I appreciate you taking the time to reply twice.