Is it possible to display an attribute value inside an If statement? or to change its value?

Hallo. I have a question, it's probably stupid but...here it is.
I tried to display a variable into an IF statement but it woldn't work. I also tried to change an attribute's value in another IF statement but it just showed the command as it was. So, what's the deal with the If statements? How can I conditionally dispaly and / or modify variables? Of course I could just add other sections in which doing it without trouble, but it could make it all a lot simpler to have the option to do it in just one page inside If statements.
thanks for any help!


If statements in Squiffy are very basic. You're best off using JS. See my post:

http://textadventures.co.uk/forum/squiffy/topic/mphphfd1auogiunupjobhg/is-it-possible-to-x-the-variables-and-to-compare-a-variable-with-another


Thanks! I've seen it.


Ok, let's say that I need to change a variable into an IF statement. I just can't see any other way to do what I need to do in my gamebook. I have read that I need to use JS, and in a previous post it was explained how to turn a Squiffy attribute into a JS var and back.
Soooo now, how do I set, increase, decrease, multiply or divide a JS variable outside but mostly inside an IF statement?
Where do I find this information? I searched the forum but couldn't find out. Could you guys point me at the right tread, or just repeat it here?
Thank all of you, this is super important!


You'd just be using standard JS. There's no special markup for Squiffy if you wish to manipulate JS variables.

Basic structure is:

if (time < 10) {
greeting = "Good morning";
} else if (time < 20) {
greeting = "Good day";
} else {
greeting = "Good evening";
}

SUPER THANKS thetruespin! This could be most of the answer I was looking for.
So, if I get it right: this conditionally sets the JS variable "greeting" giving it the values "good morning" etc according to the value of the other js variable "time", right? they are not Squiffy values, right? this could be done also assigning a number value, or doing other maths, I assume.
So with JS you can have multiple "elses" into an IF statement. This means that if time < 10 it's "good morning", if it is >= 10 and < 20 it's "day", and if it's >=20 it's "evening". Right?
By the way, one last question: how do I use JS to do other maths like increase, decrease, sum, subtract, multiply, divide and compare two values? I know it's a lot of questions, but that would close the circle and then I would be done.


Essentially yes.

This is just a very basic example of conditional logic in Javascript. It's a very powerful and commonly used programming language and basically, if you can think it, you can program it in Javascript. For more a simple overview of some basic Javascript calculations using variales, see:

https://www.w3schools.com/js/js_variables.asp

One word of advice, when using JS in Squiffy, you have to execute all your Javascript at the start of a section. You can't mix and match JS and normal content. This isn't ever a problem, but just remember the principle.

For example:

[[mysection]]:
alert("this is a an alert");

I entered a room and there was a [[door]]

Will produce a Javascript pop-up when the section loads.

[[mysection]]:

I entered a room and there was a [[door]]
alert("this is a an alert");

This will not produce the pop-up because the Javascript will not be interpreted as Script.

JS must ALWAYS go at the start of your section.


I helped someone else with some conditional logic and variable manipulation here:

http://textadventures.co.uk/forum/squiffy/topic/_4g7qim4102ix1oahi0_ug/random-value-squiffy

Might be helpful.


Ok guys, I'm going through a lot of trouble here


I tried some of the LS script suggested in the forum. They just don't work in any way (except the alert, that's working properly).
I tried the "greeting" script but it does not work on my Squiffy. It just does not give any output.
I tried to see the JS website, and it makes sense to me, but to copy what they suggest on Squiffy is just a disaser. It would either not show any output or make the whole interface disappear. I tried to copy some script in this forum, either, and the webpage disappeared. What is going on?
Look, I'm not alone in the project I'm working on. I kind of represent the whole italian gamebook community and I'm trying to prove them that Squiffy is great, proving them that it's good to digitalize their paper books with Squiffy. If I can't get over these issues, they won't follow me anymore. So please, can you help me with directions and examples I can understand and just copy on Squiffy interface?
Sorry, I am no coder. Thanks!


 Sorry, I am no coder.

Sadly Squiffy is very much designed for coders, or those willing to learn to code. As mentioned, it provides reasonable out the box functionality, but eventually you'll need some Javascript.

It's also not possible to help you code specific things - I'm happy to point you in right direction, but I sadly don't have enough time to create custom script for everyone that asks.

I'll help people troubleshoot, but I will need code otherwise it's just guesswork.

I can assure you Javascript works perfectly well in Squiffy, so if it's not working it's either a coding error or user error.

Please do try and post the code again and I'll take a look at it.


I'm going through the JS tutorial, and I can say I understand most of it. It works when I try it on the "try it" page of 3shools.
Most of it works on Squiffy, except the conditional functions.

<body>

Display the greeting
<p id="demo"></p>

<script>
function myFunction() {
    var greeting;
    var time = 20
    if (time < 10) {
        greeting = "Good morning";
    } else if (time < 20) {
        greeting = "Good day";
    } else {
        greeting = "Good evening";
    }
document.getElementById("demo").innerHTML = greeting;
}
</script>
<body onload="myFunction()">
</body>

I modified it form the examples of 3school. I canceled the function that took the local time and just declared the variable called time. I canceled the "try it" button and inserted the function evocation "onload" which should made it start when you load the page.
This code works perfectly on 3school but not on Squiffy, as much as I tried. I assume that the indentation after the beginning of the function is mandatory, but I tried also to cancel the indentation, with no use.
Please, try it out on your Squiffy; does it work?


https://www.w3schools.com/code/tryit.asp?filename=FD6RFL1RHXPI
Here you can see it works on the site.


The problem you've got is that you are mixing structural elements with Javascript. As I mentioned in another post, in Squiffy Javascript MUST be kept separate from HTML and content. The main problems are as follows:

  • you are specifying a body tag, when the outputted file already has one (you actually include a further body tag later on)
  • you have added script tags. These are technically HTML elements and will prevent code from triggering in Squiffy. They are not needed.
  • you've added content and HTML at the start, before any Javascript, so even without the script tags the code wont run.
  • you've included your Javascript as a function that you attempt to trigger using a body onload tag - because of the aforementioned problems this won't work, plus there is no need to do this anyway.
  • The way you are trying to output the variable is very convoluted.

When using JS in Squiffy, always try and make use of both Javascript itself and the options Squiffy gives you to output content. The code you need is:

var greeting;
var time = 20
if (time < 10) {
    greeting = "Good morning";
} else if (time < 20) {
    greeting = "Good day";
} else {
    greeting = "Good evening";
}
squiffy.set("squiffygreeting", greeting);


The output is: {squiffygreeting}

If you paste that into Squiffy and click run, it should work. You can then modify your time variable and see the different results.


YUPPI! It works, if I indent everything 4 spaces. This is awesome! This time I feel I'm moving in the right direction. Thank you so much!
Now, this allows me to change a var according to the value of another var; and giving it a string, it allows me to display a conditioned text. Great!
As next step I added three different section links, and they worked. Yuppi!
Then I tried to replicate with another conditional using the variables "message", to display a message sounding like this:

The value of time is X: in fact it is lesser than 10 / between 10 and 19 / equal or greater than 20.

I declared the var message in the first section (but aslo tried it in the fourth section) and then tried to go, but instead for some reason the software interpreted the code as "not code" and displayed it inside the pink rectangle. If I cancel the indentation the code is displayed as plain text. In both cases it is not run as code, as happened in the first section - even if they are written similarly.
I guess I'm again doing something wrong...what is it?
Thank you!


[[start]]:
    var greeting;
    var time = 1
    var message;
    if (time < 10) {
    greeting = "Good morning";
    } else if (time < 20) {
    greeting = "Good day";
    } else {
    greeting = "Good evening";
    }

    squiffy.set("squiffygreeting", greeting);

The output is: {squiffygreeting}<br>
{if squiffygreeting=Good morning:Good Morning indeed.<br> [[Exit 1]](uno).<br>}
{if squiffygreeting=Good day:Good day indeed.<br> [[Exit 2]](due).<br>}
{if squiffygreeting=Good evening:Good Evening indeed.<br> [[Exit 3]](tre).<br>}

[[uno]]:
Good Morning, I said.<br>
[[Ok]](quattro).

[[due]]:
Good Day, I said.<br>
[[Ok]](quattro).

[[tre]]:
Good Evening, I said.<br>
[[Ok]](quattro).

[[quattro]]:
So, let's move on.<br>

if (time < 10) {
message = "lesser than 10";
} else if (time < 20) {
message = "between 10 and 19";
} else {
message = "greater or equal to 20";
    }

squiffy.set("sqmessage", message);
squiffy.set("sqtime", time);

The value of time is {sqmessage}: in fact it is {sqtime}.

Try it out, it does not work on my squiffy.


Anyone?


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

Support

Forums