trouble with maths in squiffy

TyCamden
Why does this not work, and how do I get it to work ?

@set coffee=100

set ("coffee", get("coffee") / 2 );

coffee = {coffee}

set ("coffee", get("coffee")-20 );

coffee = {coffee}

set ("coffee", get("coffee")-10 );

coffee = {coffee}

Dennis Carlyle
TyCamden wrote:Why does this not work, and how do I get it to work ?

@set coffee=100

set ("coffee", get("coffee") / 2 );

coffee = {coffee}

set ("coffee", get("coffee")-20 );

coffee = {coffee}

set ("coffee", get("coffee")-10 );

coffee = {coffee}


First, I think it's a good idea to always set your code in sections and/or passages. So when you use "@start" at the beginning, it kicks off the parsing of code in ways you control.

Second -- any Javascript code in a section/passage must be the first thing in it. You can have Squiffy stuff *after* the JS in a passage, but if you do, then any JS after that Squiffy stuff will not work. So, the rule I use is:
- JS first, if needed.
- Squiffy stuff after that, if needed.
- No more JS is possible after Squiffy stuff in a section/passage.

Here's one way to do the stuff you're trying above:


@start start

[[start]]:
@set coffee = 100

'Coffee' = {coffee}

[divide by 2](div)

[div]:
squiffy.set("coffee", (squiffy.get("coffee")/2));

The value of 'coffee' is now: {coffee}

[subtract 20](sub)

[sub]:
squiffy.set("coffee", (squiffy.get("coffee")-20));

The value of 'coffee' is now {coffee}.

TyCamden
Is there a way that you could have it go from one section to another without the user clicking on things?

Not just in this example program, but in general as well.

Dennis Carlyle
TyCamden wrote:Is there a way that you could have it go from one section to another without the user clicking on things?

Not just in this example program, but in general as well.


This Javascript will do it.

squiffy.story.go("section_name");


It doesn't, apparently, work with passage names -- only sections.

So you could, for example, revise/add to the code above with this:



[sub]:
squiffy.set("coffee", (squiffy.get("coffee")-20));

var coff = squiffy.get("coffee");
if (coff < 40) { squiffy.story.go("cheap"); }

The value of 'coffee' is now {coffee}.

[[cheap]]:

Wow. Coffee has really dived!

Bluevoss
I'm a newbie here - first post. Been putzing with this for a day or so now. But in answer to your question, it looks like you can jump to another section using a label in {}. For example.
===
[[start]]:

Run down the street to the door.

{door}

[[door]]

You are at a door.
===
(I think this works...)

Dennis Carlyle
@Bluevoss -

I think there's an oversight here. The example needs to add ":" after "[[door]] to make it work. Without the colon, it's a link, not a section header.

From what I can gather, looking at the code example in your other new thread, it may be 'misleading' to say you can "jump" to a section by using {door}. Under "Embedding Text", the docs say ...


You can embed text from another section, or from a passage in the current section, by surrounding its name with curly brackets.



Often, this will be all you need. But you are not actually re-routing the script to a new section, the way a hyperlink does.

Bluevoss
:) That's what I get for typing code without running it through the tool. Oops. Thanks for catching that.

Still, it seems that {section} will work to move you to a section without a user click.

TyCamden
Getting close, but there is still a problem.

There are still no hyperlinks (which is good), but it is not performing the second two maths (bad)...

Revised code:

@set coffee=100

set ("coffee", get("coffee") / 2 );

coffee = {coffee}

{nextSection1}

[[nextSection1]]:

set ("coffee", get("coffee")-20 );

coffee = {coffee}

{nextSection2}

[[nextSection2]]:

set ("coffee", get("coffee")-10 );

coffee = {coffee}


Output:

coffee = 50

coffee = 50

coffee = 50

Dennis Carlyle
TyCamden wrote:Getting close, but there is still a problem.

There are still no hyperlinks (which is good), but it is not performing the second two maths (bad)...

Revised code:

@set coffee=100

set ("coffee", get("coffee") / 2 );

coffee = {coffee}

{nextSection1}

[[nextSection1]]:

set ("coffee", get("coffee")-20 );

coffee = {coffee}

{nextSection2}

[[nextSection2]]:

set ("coffee", get("coffee")-10 );

coffee = {coffee}


Output:

coffee = 50

coffee = 50

coffee = 50



It's not performing the second two Javascript commands because {nextSection1} does not actually "jump" to [[nextSection1]], the way a hyperlink would. ( I disagree with Bluevoss on this. ) It will insert ("embed") text content from the section ( including links and more { }'s ), but will not perform Squiffy or Javascript commands within the section.

It's not clear to me what you're trying to accomplish within a game setting. If you want to do math without user clicks, why not just do the math all in one go, then display the final result?

Bluevoss
I don't know enough to hold a position on this. I put the "iffy" into Squiffy.

I think he's just trying to find out how it works in case he's got to thread through conditional logic.

Still, there might be better ways to do this.

TyCamden
Still a problem.

First, thanks for your replies. I got part of my problem solved. Here is the new working example code:

@start Refill coffee

[[Refill coffee]]:

set ("coffee", 100 );

coffee = {coffee}

[[Spill 2/3 coffee]]

[[Spill 1/2 coffee]]

[[Spill 2/3 coffee]]:

set ("coffee", get("coffee") / 3 );

coffee = {coffee}

[[Spill 2/3 coffee]]

[[Spill 1/2 coffee]]

[[Refill coffee]]

[[Spill 1/2 coffee]]:

set ("coffee", get("coffee") / 2 );

coffee = {coffee}

[[Spill 2/3 coffee]]

[[Spill 1/2 coffee]]

[[Refill coffee]]


In my actual program (a work in progress), I still have a problem. It is hard to explain. The code is below. Run it and choose:
1.) The man goes to work ;
2.) The man falls on the icy sidewalk ;
3.) Head to work * ;
4.) The man stops at 7-11...
You will see that instead of running the IF code, it just shows it on screen and stops the gamebook.
I want it so that if you saw this one earlier choice it will go to one section of code, and if you did not see the choice it will go to another.

problem code:
@start intro

[[intro]]:
@set coffee=100

@title The Coffee Cup

A man takes his plastic coffee cup from the cupboard. His favorite one. It is insulated to keep his coffee warmer, longer. It is black, and without design, serious and businesslike. And a handle with a comfortable grip.

He fills his coffee cup. It is pleased.

"Man, do I feel good. But, where am I? What am I? I appear to be a coffee cup. But... I have no memory before this one. Well... what happens now?"

[[The man heads to work]]

[[The man likes sugar in his coffee]]

[[The man decides to drink tea]]

[[The man likes sugar in his coffee]]:

The man likes sugar in his coffee. He opens a pack, and dumps it in.

"What is this powder that is being poured into me? It is so sweet. But... how am I tasting it? Or seeing anything? I don't have facial features. Not even a mouth. So, I cannot he heard or understood. HEY! MAN WITH THE POWDER! Nothing."

[[The man heads to work]]

[[The man likes cream in his coffee]]

[[The man decides to drink tea]]:

The man says aloud, "I'm tired of coffee today. I think I'll have some tea."

His wife replies, "I just made some. It's still on the stove."

The coffee cup screams: NO ! I AM A COFFEE CUP ! NOT A TEA CUP ! DON'T DO IT !

But noone can hear him.

[[Tea it is]]

[[The man changes his mind again and has coffee]]

[[Tea it is]]:

He pours the coffee down the sink and rinses out the cup. He pours in some hot tea instead.

What does the coffee cup think about this? It does not. It no longer thinks at all. It is back to being just... a coffee cup.

The End.

[[The man changes his mind again and has coffee]]:

The man says aloud, "Nevermind. I'm running late."

"Whew, that was a close one. I don't think it be right, carrying tea. It's just... not in my nature."

[[The man heads to work]]

[[The man likes cream in his coffee]]:

The man likes cream in his coffee, so he pours some in.

"I'm being diluted by a white liquid!? It's not bad though."

The man stirs the drink.

The coffee cup laughs with delight. Oh. He is ticklish !

[[The man heads to work]]

[[The man heads to work]]:

The man grabs his keys, celphone, and coffee cup, and heads out the door to work.

The coffee cup is momentarily blinded by the sun. It is so bright outside, much brighter than indoors. And it is relecting off of the snow covering the man's yard. But it knows it is well insulated for his trip, and his contents well protected from the frigid winds.

[[The man falls on the icy sidewalk]]

[[The man gets into his car and heads to work]]

[[The man falls on the icy sidewalk]]:
set ("coffee", get("coffee") / 2 );
{if coffee<1: [[Finish]](YouLose)}

The man's shoes lose grip on the ice and he topples down. His coffee cup goes flying, and the lid pops off when it hits the sidewalk. Half of it is spilled, forever lost.

The coffee cup cries in pain, and feels dizzy, but it survives. It begins to question it's own mortality.

[[Head to work]](The man gets into his car and heads to work)

[[The man gets into his car and heads to work]]:

The man gets to his car, and into it as well. He places the coffee cup in the cup-holder of the car.

"I feel very safe here. Very secure indeed. Looks like we are going a bit on an adventure."

[[The man arrives at work]]

[[The man stops at 7-11]]

[[The man has a car accident]]

[[The man stops at 7-11]]:

The man stops at the 7-11.

if (squiffy.story.seen("The man falls on the icy sidewalk")) {
[[711hefellearlier]]
} else {
[[711nofallearlier]]
}

[[711hefellearlier]]:

@set coffee=100

He tops-off his coffee.

" I feel rejuvinated ! "

[[The man heads to work]](The man arrives at work)

[[711nofallearlier]]:

He grabs a donut to eat at work.

[[The man heads to work]](The man arrives at work)

[[The man arrives at work]]:

coffee = {coffee}

{if seen The man stops at 7-11:{manLate}}{else:{manOnTime}}

[[manLate]]:

set ("coffee", get("coffee")-20 );

The man arrives at work. He is late.

His boss warns him not to be late again.

He walks over to his desk and sits down. As he eats his donut, he drinks the coffee.


{if coffee<1: [[Finish]](YouLose)}

[[He has to use the bathroom]]

[[manOnTime]]:

set ("coffee", get("coffee")-10 );

The man arrives at work. He is almost late, but made it just in time.

He walks over to his desk and sits down. He drinks some of the coffee.

{if coffee<1: [[Finish]](YouLose)}

[[He has to use the bathroom]]

[[The man has a car accident]]:

x

[[He has to use the bathroom]]:

x

TyCamden
EDIT. The latest problem is fixed. For details, see: viewtopic.php?f=24&t=6356

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

Support

Forums