Go to new section without linking

Dennis Carlyle
Here's one way of sending the game to a new section when an attribute reaches a certain value. The advantage of using a function is that it you don't have to force the player to click on a link to get to the desired section. (Although this example only has one option each time, you could add other options at every point.)

It does, however, require the author to specifically 'call' the function with a line of Javascript at any section where the attribute might have reached the conditional value.

I did try to include more 'auto' behaviour in the Master section, but so far I'm only having any luck with JS functions in the Master section. To my mind, this isn't really a disadvantage. It gives the author control over when / if to use the function.


@start start

[[]]:
/* This 'Master' section loads automatically on every section change. */
/* So the 'drinkCoffee' function is always 'available', and can be */
/* 'called' (with Javascript) from any point in the game. */

squiffy.drinkCoffee = function() {
if (squiffy.get("coffee") < 1) {
squiffy.story.go("End"); }
}

<!--
NOTE: The "drinkUp" section below is just an example of 'calling' on textual content
that's repeated several times in the game. Things like "@set" or Javascript
statements in the section will *not* be picked up and "embedded" or run in
the 'calling' section. But some things that refer to "in-line" text will -
such as the {if coffee> 1: . . . } part.
End Note -->

[[drinkUp]]:

You have {coffee} {if coffee> 1:gulps}{else:gulp} of coffee left.

[[drink some coffee]](Sec3)

[[start]]:
<!-- Note: "start" section initializes things, and does *not* -->
<!-- call on the Master section function. -->

@set coffee = 10

<b><u>Cafeteria</u></b>

Your break is 15 minutes.

You have about {coffee} gulps of coffee left in your cup.

[[drink some coffee]](Sec2)


[[Sec2]]:
/* We know that "coffee" is > 1 here. But there's */
/* no harm in calling the function anyway. */

squiffy.drinkCoffee();

@clear
<b><u>Cafeteria</u></b>
<br>

Someone stumbles and bangs into your table, almost knocking your coffee over.<br>

@dec coffee 3
{drinkUp}

[[Sec3]]:
squiffy.drinkCoffee();

@clear
<b><u>Cafeteria</u></b>
<br>

{if coffee> 4:<i>Break is almost over!</i>

You hurry to finish up, and spill some coffee down the front of your shirt.<br>}

@dec coffee 2
{drinkUp}

[[End]]:
@clear
<b><u>Office</u></b>
<br>

You down the last bit of your coffee, and head back to your office.


TyCamden
Did you run it? Did it work?

I tried making a short program (below) using your code, but could not get it to work.

If you can see why it does not work, let me know please:

@start start

[[start]]:

@set coffee=1

You have {coffee} {if coffee> 1:gulps}{else:gulp} of coffee left.

[[drink]]

[[drink]]:

@dec coffee

You have {coffee} {if coffee> 1:gulps}{else:gulp} of coffee left.

[[drink again]](drink)

[[End]]:

The End.

[[]]:
squiffy.drinkCoffee = function() {
if (squiffy.get("coffee") < 1) {
squiffy.story.go("End"); }
}

Dennis Carlyle
Yes, the code I posted works for me (and I've made some cosmetic changes, added some comments to the posted code.)

In your example, you have the "drinkCoffee" function created in a Master section -- but you never use it. Check my example, and see where the "drinkCoffee" function is actually 'called' upon to check the value of 'coffee'.

From my original post:
"It does, however, require the author to specifically 'call' the function with a line of Javascript at any section where the attribute might have reached the conditional value."

TyCamden
I understand now after going through it, and your post, again. Thank you.

The following is an example that I saved in my notes that I created, based on your example...

@start start

[[]]:

squiffy.CheckHealth = function() {
if (squiffy.get("health") < 1) {
squiffy.story.go("End"); }
}

[[defendAgain]]:

You have {health} health left.

[[Throw A Punch]]

[[start]]:

@set health = 100

You are in a fist fight. Defend yourself !

[[Throw A Punch]]

[[Throw A Punch]]:

squiffy.CheckHealth();

You take a punch to the face.

@dec health 25
{defendAgain}

[[End]]:

You cannot take the punishment any further.

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

Support

Forums