Howdy, Stranger!

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

Variable add ?

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

  • edited August 2015
    You have run into an issue that many people new to HTML have and that is that by default anything that is entered into either a prompt or an input field is considered by the web-browser to be a String (Text) value even if the contents of that value looks like a number or a date.

    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:
    <<set $for to prompt(" force ?")>>
    <<set $int to prompt(" intelligence ?")>>
    <<set $agi to prompt(" agility ?")>>
    <<set $endu to prompt(" endurence ?")>>
    
    add them: <<print $for+$int+$agi+$endu>>
    

    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:
    <<set $for to prompt(" force ?")>>
    <<set $int to prompt(" intelligence ?")>>
    <<set $agi to prompt(" agility ?")>>
    <<set $endu to prompt(" endurence ?")>>
    
    <<if not isNaN($for)>><<set $for to parseInt($for)>><</if>>
    <<if not isNaN($int)>><<set $int to parseInt($int)>><</if>>
    <<if not isNaN($agi)>><<set $agi to parseInt($agi)>><</if>>
    <<if not isNaN($endu)>><<set $endu to parseInt($endu)>><</if>>
    
    add them: <<print $for+$int+$agi+$endu>>
    

    note: Instead of using the prompt function you could use SugarCube's <<textbox>> macro.
  • Salutation Greyelf,

    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
  • I included links to the documentation for both isNaN and parseInt functions in my last comment.

    isNaN checks to see if a value within a String (piece of Text) is equal to a number:
    isNaN("ABCDE") returns false because the characters in "ABCDE" are not numeric.
    
    isNaN("12345") returns true because the characters in "12345" are numeric.
    

    parseInt converts a String that contains numeric characters (1234567890) into a number (i.
  • Start programms and technical english at more than 40 is not so easy ;)
    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

Sign In or Register to comment.