set new variable within IF statement

qroft
Hi there,
i have a section that starts with


{if faq1=1:Dan... Dani... Nope damn. I got it! @inc gameScore 1}
{if faq1=2:Nope! @inc gameScore -1}


So before this section, the player is asked and depending on his choice he gets a point or not.
My problem is that the @inc does not work - it is simply shown as text.
Where is my error?

Big greetings from this side on earth,
Carlos

Alex
You can't set variables within that kind of "if" - those { braces } mean you're using the text processor, and that's just for customising the text that gets displayed.

Instead, you need to use JavaScript. I'm not sure exactly what you're trying to do, so if you could give some examples of the kind of output you're expecting that would help.

Here's an example where there are two answers to a question. One increases the score and other decreases it. Either way the player then progresses to the next section:


@set score=0

Choose an answer:

[The right answer](right)
[The wrong anser](wrong)

[right]:
@inc score
squiffy.story.go ("next")

[wrong]:
@dec score
squiffy.story.go ("next")

[[next]]:
Your score is {score}.


Try it out here: http://docs.textadventures.co.uk/squiff ... le.squiffy

qroft
Thanks a lot Alex. I understand now how it works.
My problem is this: i do have in the INDEX.HTML a DIV with the ID "gamescore".
When i use this var within the squiffy file to show the actual score it works. What i would like to,
is to pass that var to the index so that it can show the whole time the actual score.

This is the beginning of my squiffy file:

@start sec0
@set gameScore = 0
@set huntHealth = 41

[[]]:
var huntHealth = squiffy.get("huntHealth")
var gameScore = squiffy.get("gameScore")
if(gameScore == null){
gameScore = 0;
huntHealth = 41;
}
$('.gameScore').text(gameScore)
$('.huntHealth').text(huntHealth)


So whenever you get to a new section, the health and score showing is updated.

In short: is it possible that the squiffy file can pass the score variable to my index.html ?

qroft
Ok, i just found out one reason why this ain't working!

First the [[]] is fired and then my gameScore increasing.
That is absolutely understandable.

Now my idea was to insert some kind of javascript functions at the beginning of the game,
but i can not launch them. So on top of the .squiffy file i do have a simple JS function with the id refreshScore to
launch an alert.

If i now write this code:

[[theLivingRoom]]:
@inc gameScore
refreshScore();

the game hangs and does neither show the alert nor the next section or whatever.
What i get is: Uncaught ReferenceError: refreshScore is not defined

Any suggestions?

theomstea
Hello,

It's also bugged me that variables cannot be set/unset in {if} sections.
Workarounds can be found most of the time but I think they can pose problem in certain cases.

Here's an example:


@set troll_sleeps
@set wearing_boots
squiffy.story.go("outside");

[[outside]]:
[[enter room.]](room)

[[room]]:
{if troll_sleeps:
Troll: 'zzz' <br>
{if wearing_boots:The troll wakes up.}
}
{else:Troll: 'hello'}


[[exit room]](outside)


So, we have a troll who's sleeping in a room. Upon entering the room, the troll gets woken up if wearing_boots is true.
What's missing in the previous code is the unsetting of troll_sleeps. Where should we put it?

1) The intuitive choice would be inside {if wearing_boots}:

{if wearing_boots:
@unset troll_sleeps
The troll wakes up.}

but as Alex explained that's not possible.

2) A workaround would be to unset troll_sleeps upon exiting the room, i.e.

{if wearing_boots:[[exit room]](outside, not troll_sleeps)}
{else:[[exit room]](outside)}

but that's inelegant for various reasons:
[list=a]
[*]What we really WANT is the troll waking up upon entering the room, not exting.[/*:m]
[*]If there's several exit links we need to replicate the code.[/*:m]
[*]What if stuff happens in the room, that depends on whether the troll is sleeping?[/*:m][/list:o]
In that case we need to find another workaround. The only one I see is

3) Add an intermediate section:

@set troll_sleeps
@set wearing_boots
squiffy.story.go("outside");

[[outside]]:
[[enter room.]](room)

[[room]]:
{if troll_sleeps:
Troll: 'zzz' <br>
{if wearing_boots:The troll wakes up.}
}
{else:Troll: 'hello'}

{if wearing_boots:[[continue]](room2, not troll_sleeps)}
{else:[[continue]](room2)}

[[room2]]:
Stuff happens here that depends on troll_sleeps.

[[exit room]](outside)

but that multiplies the number of sections, i.e. the number of clicks required by the player.
From a player perspective, this is not great (why make me click Continue if that's the only option anyway)

If I'm not wrong, I think there's no other choice than workaround 3), and I think it all comes down to Squiffy
not being able to change the value of an attribute in the middle of a same section:

[[section]]:
@set x=1
{x} should be 1 but is 2
@set x=2
{x} is 2

I am guessing this is due to the "@" statements being parsed and executed before
the text being interpreted. Which is a shame, but I suppose it's voluntary.

I'd like to confirm I'm not missing an obvious/more elegant solution.
Any thoughts on this?

This topic is now closed. Topics are closed after 180 days of inactivity.

Support

Forums