Hello,
i try to make a caracter sheet ( twine 2 sugarcube)
The player have 30 points
<<set $for to prompt(" force ?")>>
<<set $int to prompt(" intelligence ?")>>
<<set $agi to prompt(" agility ?")>>
<<set $endu to prompt(" endurence ?")>>
after i will like to do something like that :
<<if $for+$int+$agi+$endu>30>>
Too much points
<<else>>
page 3
<<endif>>
<<if $for+$int+$agi+$endu>30>> is not correct.
how to write it correctly ?
Thank s
Comments
So lets assume the Reader of your story entered a 1 for force, a 2 for intelligence, a 3 for agility and a 4 for endurence.
If the web-browser accepted these values as numbers then your $for+$int+$agi+$endu would be 1 + 2 + 3 + 4 which would equal 10 but this is not what happened.
Instead the web-browser accepted then as String (Text) so your $for+$int+$agi+$endu would be "1" + "2" + "3" + "4" which would equal "1234", as the following test shows:
What you need to do is to first convert each of those Strings into numbers which you can do this using the Javascript parseInt fuction but this leads into a second issue, what happens if the Reader enters a value like "ABC" for force instead of a number.
You will first need to test the values that the Reader has entered to check that they are actually numerical before you can convert them. The following is a basic example of how to use Javascript's isNaN function to do this, although it does not test for all the things that could go wrong like the Reader entering a number with a decimal point instead of an integer:
note: Instead of using the prompt function you could use SugarCube's <<textbox>> macro.
i ve change for textbox, more beautifull
i use <<if not isNaN($for)>><<set $for to parseInt($for)>><</if>> ...
even i don't know how it work...that ligne do the job... magic of the code
thank's for answer. Fast and accurate !
unfortunatly ... ll be back
Thierry
isNaN checks to see if a value within a String (piece of Text) is equal to a number:
parseInt converts a String that contains numeric characters (1234567890) into a number (i.
it s more clear, with your explanation. Thank you.
I ve still a lot of questions ... don't care to each one or you will lost too much time
best regards
Thierry