+1 vote
by (300 points)

Hello, I was wondering if there is a way to get the following if statement to work:

<<set $quests to []>>
<<set $hero to {}>>
<<set $hero.hasGrail to true>>
<<set $quests.push({'zero': ["questName","HolyGrail"], 'one': ["startPassage","startGrail"], 'two': ["hero.hasGrail", false]})>>
<<= "$" + $quests[0].two[0]>> // prints $hero.hasGrail
<<if <<= "$" + $quests[0].two[0]>> == $quests[0].two[1]>>
Hero does not have the Grail and can start  the quest.
<<else>>
Hero has the Grail and cannot start  the quest.
<</if>>

The Stupid Print Trick does not work inside an if statement, and I was wondering if you can get variable names inside if statements without knowing the actual name. I was planning on using it inside a for loop so, I don't actually know the name of the variable I will be checking in advance.

There are probably simpler ways to do this.

Thanks in advance anyway.

2 Answers

+1 vote
by (68.6k points)
selected by
 
Best answer

Rather than storing the names of story variables (or entire property chains) so you may obliquely reference them later, why don't you simply store the results there.  It would also likely help to simplify your data structure setup as well, say by making $quests an object.  For example:

/* Initialize $quests as an object. */
<<set $quests to {}>>

/* Add quest objects to $quests. */
<<set $quests["HolyGrail"] to {
	name     : "HolyGrail",
	start    : "startGrail",
	complete : false
}>>

/* In play, when the hero gets the Grail. */
<<set $quests["HolyGrail"].complete to true>>

/* Checking the quest's status at any point (both cases). */
<<if $quests["HolyGrail"].complete>>
	Hero has the Grail and cannot start the quest.
<<else>>
	Hero does not have the Grail and can start the quest.
<</if>>

/* Checking the quest's status at any point (true only). */
<<if $quests["HolyGrail"].complete>>
	Hero has the Grail and cannot start the quest.
<</if>>

/* Checking the quest's status at any point (false only). */
<<if not $quests["HolyGrail"].complete>>
	Hero does not have the Grail and can start the quest.
<</if>>

 


That said, if you want to persist with the mess you have now, then State.getVar() static method can do what you want.  For example:

<<if State.getVar("$" + $quests[0].two[0])>>
	Hero does not have the Grail and can start the quest.
<<else>>
	Hero has the Grail and cannot start the quest.
<</if>>

Since you're using a boolean there, there's no good reason to compare it to another.  Simply initialize $hero.hasGrail to false and set it to true when the player completes the quest.

by (300 points)
Thanks, the second answer is what worked (after updating my story formats). I would not have made this "mess" :) if I didn't need the functionality I can get with this option.
+1 vote
by (23.6k points)

You have to wrap the entire if-statement in a <<print>> to do the stupid print trick. haven't had a chance to test it, but something like this should work:

<<print "<<if $" + $quests[0].two[0] + ">> == $quests[0].two[1]>>
Hero does not have the Grail and can start  the quest.
<<else>>
Hero has the Grail and cannot start  the quest.
<</if>>">> 

 

by (300 points)
Thanks for your answer but this does not seem to work.
...