Quantcast
Channel: Twine Forum
Viewing all articles
Browse latest Browse all 3830

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

$
0
0
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.

Viewing all articles
Browse latest Browse all 3830

Trending Articles