Loading variables off an IF statement

Bluevoss
This is strange - no matter what the random number is, I always get the elf character. Anyone know how to set variables inside an IF statement?

[[start]]:

var rnd=Math.ceil(Math.random()*3);
squiffy.set("rnd",rnd);

{if rnd = 1:
@set name = Groo
@set desc = Warrior
@set str = 3
}

{if rnd = 2:
@set name = Blinky
@set desc = Wizzard
@set str = 1
}

{if rnd = 3:
@set name = Pointy
@set desc = elf
@set str = 2
}


[[next]]

[[next]]:

{rnd}<br>
{name}<br>
{desc}<br>
{str}<br>

Dennis Carlyle
Squiffy If/Else is for choosing between strings of text.
==========================
Re: Riddle me this - paths and setting variables
Bluevoss:
"Post-thought - maybe Squiffy is interpreting handling the string printing within the coding modules, but handles setting variables at the end of the section, regardless of their placement in logic structures."

Dennis Carlyle:
"Yes.
If you remove the "@set attack = 3" statement, for example, you'll see that attack does get set to 6 first. Then, (if attack = 3 statement is included), the value is being reset. Both commands are being carried out, regardless of whether guninhand is true or false."
=========================

Bluevoss
I'm sorry to basically ask the same question again. I've been thinking of an idea for a day or so and got my head back into usual code-space, rather than squiffy-space. I think this is driving me as crazy as when I leaned OO code.

Now I gotta think squiffy and see if I can make this work some other way. This engine is very powerful in some ways, but very hobbled in others.

BTW, followup - when people say that you can write java script in the background, what are they referring to? I've looked up some JS tutorials and what I see isn't that close of a match to what I've seen in examples here.

Dennis Carlyle
Bluevoss wrote:
BTW, followup - when people say that you can write java script in the background, what are they referring to? I've looked up some JS tutorials and what I see isn't that close of a match to what I've seen in examples here.


Well . . . JS is JS, but with some things it's flexible in how things can be formatted. (For example, object parameters.)

I don't know about "... write Javascript in the background." Maybe that refers to having JS code that is always "available", regardless of which section Squiffy is 'at' (?). I know you can create your own JS functions in Squiffy, by putting them in the Master section. Following is an if/else function I've been testing lately. The function is named "verbNoun", and it returns the JS string variable "JTxt", which you would then use in your story/game.


@start Intro

[[]]:
squiffy.verbNoun = function(a, b) {
var JTxt = "";

/* Examine */
if (a == "examine") {
if (b == "book") { /* book */
JTxt = "You examine the book.";}
else if (b == "flagstone") {
JTxt = "You examine the flagstone.";}
else { JTxt = "You see nothing unusual about the "+b+"."; }
}
/* Take */
else if (a == "take") {
if (b == "flagstone") {
JTxt = "The stone is firmly set in the hard, dry earth.";}
else { JTxt = "That's not something you can carry with you."; }
}

/* for testing */
else {JTxt = a + b;}

return JTxt; }



So . . . at any point in the game, you can send the function a verb and noun the player has chosen, and it will process that combination.


[calc]:
squiffy.set(("verbNoun"), squiffy.verbNoun("take", "flagstone"));


Now you have a Squiffy attribute, "verbNoun", to display as you wish.

Bluevoss
Thanks very much for the code sample. I work at a corporate-wide position, mostly doing excel and VBA these days. However, the team I'm loosely attached to does javascript. What I can do is try to get your code working in a test program, examining and taking books and flagstones and see how that goes (and ask them specific javascript questions). I figure this is going to be very much like my learning curve with Excel, where to really get anywhere, you needed VBA in the background.

Of course, for a lot of that, I could just record a macros and see what VBA did. Here, I gotta bug you guys, for which I am grateful.

BTW, I did finally get my head around things and get a working harness for the little game I'm putting together. Viewing each section as a single-point set for variables helped (with parallel sections for setting variables to different values). Its kinda like thinking that each section prints text and sets variables, nothing more. Branches are both to new sections of text and new variable settings. Hope that made sense. Still, I'll start looking into what you did and try to duplicate it on this end.

Dennis Carlyle
Bluevoss wrote:
I figure this is going to be very much like my learning curve with Excel, where to really get anywhere, you needed VBA in the background.


I think you probably can use Squiffy for a good CYOA-type game without any 'extras' or scripting needed, but . . . .

Alex, the author, has said somewhere that he didn't intend Squiffy to be for people who don't know Javascript. I believe a spreadsheet or database program, at its core, is just a structure/format for storing and displaying data. But most programs, like Excel or Access, have tons of 'extra' built-in functionality to scan and manipulate the data, even before you look at any scripting language. Compared to these, Squiffy has practically none. (Though I do like the ones it has.) However, with HTML, CSS, and Javascript -- there's not much you can't do, I'm sure, as long as you're willing to take the time to code what you want.

Bluevoss
I'll say it's simple. Even this doesn't work. I'm having to come up with a lot of tricks to get around these issues...

@set low = 2
@set high = 4

{if low < high: low less then high}
{else: high less than low}

Done


(of course, now someone will show me where I'm wrong and I'll feel like and idiot. Though I will have working code :) )

I recently wrote a CYOA using a generator I created in Excel. While it was fun and pretty flexible, there were several problems. First off, a lot of people couldn't run it (they didn't have Excel). But worst, Excel has no real security. Someone could quite easily float a version with deathly deadly malware attached and distribute it as mine. That being the case, I started looking for an alternative (and here I am!).

Dennis Carlyle
Bluevoss wrote:I'll say it's simple. Even this doesn't work. I'm having to come up with a lot of tricks to get around these issues...

@set low = 2
@set high = 4

{if low < high: low less then high}
{else: high less than low}

Done


(of course, now someone will show me where I'm wrong and I'll feel like and idiot. Though I will have working code :) )



It's like we've seen in other threads -- if/else just seems to be quite inconsistent. I'll have to test more, but with a few simple examples, it does seem like this kind of comparison will work:

{if fuel> 25: There's enough fuel to make the trip.}

That is . . . comparing two set attributes against each other is inconsistent at best. But comparing a set attribute against a number will work.

Notice no space between "fuel" and ">". And . . . it may be different when comparing an attribute to a string. {If gender=male:...} works (as in the docs), but add a space anywhere between "gender" and "male" . . . and it doesn't work.

Bluevoss
And here's how to do it. This is just an easy section of code, but you could string a bunch of these (including < and > to cover ranges) and get things done.

This is important since it means you don't have to go to different sections to set variables - you can do it right in one section.


@set var1 = 1
@set var2 = 2

if (squiffy.get("var2") == 2) {
squiffy.set("var2", 1);}

try

var1={var1}
var2={var2}

Dennis Carlyle
Bluevoss wrote:And here's how to do it. This is just an easy section of code, but you could string a bunch of these (including < and > to cover ranges) and get things done.

This is important since it means you don't have to go to different sections to set variables - you can do it right in one section.


@set var1 = 1
@set var2 = 2

if (squiffy.get("var2") == 2) {
squiffy.set("var2", 1);}

try

var1={var1}
var2={var2}


That's great. So it looks like the "rule" about Javascript needing to be the first thing in a section does not apply where only Squiffy commands like @set or @clear come before the JS.

TyCamden
I fixed OP's original post per the advice given in this thread.

Can anyone simplify it ?

@start start

[[start]]:

var rnd=Math.ceil(Math.random()*3);
squiffy.set("rnd",rnd);

if (squiffy.get("rnd") == 1) {
squiffy.set("name", "Groo");}

if (squiffy.get("rnd") == 1) {
squiffy.set("desc", "Warrior");}

if (squiffy.get("rnd") == 1) {
squiffy.set("str", 3);}

if (squiffy.get("rnd") == 2) {
squiffy.set("name", "Blinky");}

if (squiffy.get("rnd") == 2) {
squiffy.set("desc", "Wizard");}

if (squiffy.get("rnd") == 2) {
squiffy.set("str", 1);}

if (squiffy.get("rnd") == 3) {
squiffy.set("name", "Pointy");}

if (squiffy.get("rnd") == 3) {
squiffy.set("desc", "Elf");}

if (squiffy.get("rnd") == 3) {
squiffy.set("str", 2);}

[[next]]

[[next]]:

{rnd}<br>
{name}<br>
{desc}<br>
{str}<br>

Dennis Carlyle
TyCamden wrote:I fixed OP's original post per the advice given in this thread.

Can anyone simplify it ?



You can make the code a little more compact by combining things with the same rnd value. And a little more by just using the Javascript variable, instead of "get"ting the Squiffy attribute.

if (rnd == 1) {
squiffy.set("name", "Groo");
squiffy.set("desc", "Warrior");
squiffy.set("str", 3);
}

TyCamden
Thank you. Here it is now:

@start start

[[start]]:

var rnd=Math.ceil(Math.random()*3);
squiffy.set("rnd",rnd);

if (rnd == 1) {
squiffy.set("name", "Groo");
squiffy.set("desc", "Warrior");
squiffy.set("str", 3);
}

if (rnd == 2) {
squiffy.set("name", "Blinky");
squiffy.set("desc", "Wizard");
squiffy.set("str", 1);
}

if (rnd == 3) {
squiffy.set("name", "Pointy");
squiffy.set("desc", "Elf");
squiffy.set("str", 2);
}

[[next]]

[[next]]:

{rnd}<br>
{name}<br>
{desc}<br>
{str}<br>

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

Support

Forums