Howdy, Stranger!

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

Twine 1.4.2 SugarCube 2.17 correct syntax in set macros and if else

edited April 2017 in Help! with 1.x
I have read the Twine and SugarCube docs, but JavaScript is fairly new to me and I am still trying to get the hang of both using that and the Twine/SugarCube macros.

My understanding is that I can:
<<set $myvar to 0>>
in the StoryInit passage and then testing for it would be
<<if $myvar is 0>>
    do this stuff
<<elseif $myvar is 1>>
    do this other stuff
<<elseif $myvar is 2>>
    do this other stuff
<<endif>>

This works, but I have a couple of questions.

In other scripting I have done elsewhere, there is always an "else" condition, as well.
Is
<<else>>
required in SugarCube, or is it ok to skip it, if not matching the conditions, where
<<else>>
would be simply to "do nothing", as in:
<<if $myvar is 0>>
    do this stuff
<<elseif $myvar is 1>>
    do this other stuff
<<elseif $myvar is 2>>
    do this other stuff
<<else>>
<<endif>>

Also, I have found bits of code, here and there, that people have used in Twine and have incorporated a lot into my story/game.

One of the things is a "player" object from an rpg example by Sharpe. In the StoryInit, he has:
<<set $player = {
	name: "Player",
	maxHP: 50,
	HP: 50,
	maxMP: 10,
	MP: 10,
	AC: 6,
	attack: 1,
	damage: 1,
	speed: 10,
	gold: 10,
	XP: 0,
	levelUp: 100,
	level: 1
}>>

can "to" be substituted for "=", or is "=" required for objects?

Also, when he tests for these, he has:
<<if $poisoned eq 1>>

<<set $poisonDamage = Math.floor($player.maxHP * $monsterPoison) + Math.floor((Math.random()*3)+1)>>
<<set $player.HP = $player.HP - $poisonDamage>>

<<if $player.HP lte 0>>
<<set $player.HP = 1>>
<<endif>>

You suffer  <<print $poisonDamage>> damage from poison!<br><br>

<<endif>>

[[Attack!|Attack][$attackPower=1]]<br>

I would like to know if there is a difference between that and:
<<if $poisoned is 1>>

<<set $poisonDamage = Math.floor($player.maxHP * $monsterPoison) + Math.floor((Math.random()*3)+1)>>
<<set $player.HP to $player.HP - $poisonDamage>>

<<if $player.HP <= 0>>
<<set $player.HP to 1>>
<<endif>>

You suffer $poisonDamage damage from poison!<br><br>

<<endif>>

[[Attack!|Attack][$attackPower to 1]]<br>


Is there a time when "=" should be used rather than "is" or "to"?
With so many parts of my game being derivatives of code from other Twiners, I would like my code to be as consistent, as possible, as well as accurate. I also don't want to break something and have to start all over (I do back up, often).

Thanks, in advance, for any clarification.

Comments

  • EarthMagic wrote: »
    ...or is it ok to skip it, if not matching the conditions...
    The <<else>> is not required unless you have values outside the range caught by other conditions.
    EarthMagic wrote: »
    ...can "to" be substituted for "=", or is "=" required for objects?
    As explained by the <<set>> macro documentation, the to operator is the equivalent of the equals-sign (=) operator, and both work for complex object values as well as standard values like String and integer.
    EarthMagic wrote: »
    I would like to know if there is a difference between that and...
    Nothing, one is used word-based operators (eg. to and lte) and the other is using the equivalent Javascript maths-symbol-based operators (eg. = and <=).
    EarthMagic wrote: »
    Is there a time when "=" should be used rather than "is" or "to"?
    The is is not the same as the equal-sign (=) or to operators, it is used for comparison where the other two are used for assignment.

    One reason to use the word-based operators instead of maths-symbol-based ones is save yourself from the dreaded "single equals-sign vs double (or triple) equals-sign" problem that plagues both novice and experienced coders alike.

    eg. you accidentally use an assignment operator when you wanted a comparison operator, and vise-versa.
  • Thank you, very much, greyelf!
    EarthMagic wrote: »
    Is there a time when "=" should be used rather than "is" or "to"?

    The is is not the same as the equal-sign (=) or to operators, it is used for comparison where the other two are used for assignment.

    If I understand you, where Sharpe had
    <<if $poisoned eq 1>>
    

    eq is the same as is, but
    <<if $poisoned = 1>>
    
    would not be accurate as
    the = would be used for assignment, only.
    as in
    <<set $poisoned = 1>>
    
    would be the same as
    <<set $poisoned to 1>>
    

    where
    <<if $poisoned === 1>>
    
    is also the same as
    <<if $poisoned is 1>>
    

    Let me know if I got it twisted up somewhere. Thanks, again!

  • Close, but not quite. While both are equality comparison operators, eq and is are not exactly the same, as SugarCube's <<if>> documentation is clear about.

    The TwineScript eq operator is equivalent to the JavaScript == operator and is lazy equality. The neq operator has similar equivalence with the != operator.

    The TwineScript is operator is equivalent to the JavaScript === operator and is strict equality. The isnot operator has similar equivalence with the !== operator.

    The difference between lazy and strict equality is that lazy equality attempts type coercion when comparing values of dissimilar types while strict equality does not.

    For example, say you're attempting to compare the values 5 (number) and 5 (number):
    5 eq 5  → true
    5 is 5  → true
    

    Now, let's say you're attempting to compare the values 5 (number) and '5' (string):
    5 eq '5'  → true
    5 is '5'  → false
    


    PS: I generally recommend the is/isnot operators.
  • Thank you, very much, TheMadExile. It will all sink in and the more I use it, the easier it will get.
    Close, but not quite. While both are equality comparison operators, eq and is are not exactly the same, as SugarCube's <<if>> documentation is clear about.

    You know, I must visit that page on my hard drive 15 times a day and I guess I missed it somehow. Now, I know it is all in the macros section, it will be easy to check.
    PS: I generally recommend the is/isnot operators.
    That does look as if it would be really easy to remember.

    Thank you, both, for taking time to answer my questions and explain so clearly. I already have over 70 passages and don't want to have to go back and fix stuff when there are 150.

Sign In or Register to comment.