Page 1 of 1

Eos IF Statements

Posted: Tue Apr 21, 2020 9:46 pm
by bakak17
So I made a variable called "cbt" which is set to 0 at the beginning of the tease, and the user is asked if they like cbt. If they answer yes, the variable is changed to be 1. Now, when I get to this
Screen Shot 2020-04-21 at 2.42.53 PM.png
Screen Shot 2020-04-21 at 2.42.53 PM.png (103.65 KiB) Viewed 1800 times
action while previewing the tease, it always displays the actions for if cbt=1 even when cbt=0! How do I fix this?

Re: Eos IF Statements

Posted: Tue Apr 21, 2020 10:04 pm
by undeniable_denial
That is because a single equal-sign is an assignment. You are telling EOS to make cbt equal to 1 inside the if. The result of the assignment then is also 1, which gets converted to true.

To fix it, make it two equal signs: cbt==1
That is a comparison which can result in either true or false.

I have explained a similar case in my tutorial in part IV chapter 2, if you want to take a look.

Re: Eos IF Statements

Posted: Tue Apr 21, 2020 10:37 pm
by bakak17
undeniable_denial wrote: Tue Apr 21, 2020 10:04 pm
To fix it, make it two equal signs: cbt==1
This worked, thank you!

Re: Eos IF Statements

Posted: Tue Apr 21, 2020 11:47 pm
by kerkersklave
I would however set cbt to true and false.
And then use cbt directly as a predicate, no need for the == 1.

Re: Eos IF Statements

Posted: Thu Apr 23, 2020 3:13 pm
by fapnip
While not exactly an an answer to the original question, just clarify all this if stuff:

Code: Select all

var x = 1
(x == 1) // evaluates as true
Is equivalent to:

Code: Select all

var x = 1
(x)  // evaluates as truthy
Is also equivalent to:

Code: Select all

var x = 1
(x == true)  // evaluates as true
All the above are 'truthy' evaluations using Abstract Equality via implicit evaluation or double equals.

For example:

Code: Select all

('66' == 66) // evaluates as true.
Javascript also has Strict Equality comparisons using triple equals:

Code: Select all

(66 === 66) // evaluates as true
('66' === 66) // evaluates as false
(true === true) // evaluates true
(1 === true) // evaluates false
But, back to double equals:

Code: Select all

(1 == true) // evaluates true
Generally, you should try to use strict comparisons with triple equals, else you could end up getting a true result when you were after false.

(There's also the inverse of == (abstract equals), != (abstract not equals), and the inverse of === (strict equals), !== (strict not equals))

Re: Eos IF Statements

Posted: Thu Apr 23, 2020 3:21 pm
by undeniable_denial
fapnip wrote: Thu Apr 23, 2020 3:13 pm

Code: Select all

var x = 1
(x == true)  // evaluates as true
All the above are 'Falsy' evaluations[/code]
I think you mean 'truthy', but whatever: Honestly, the both of you are just scaring away beginners with this perfectionism and I would rather have them tell their stories with unoptimized/questionable code than not at all.
"No master has yet fallen from the sky!"...

Re: Eos IF Statements

Posted: Thu Apr 23, 2020 3:49 pm
by fapnip
I'm a firm believer that information doesn't scare away beginners, but instead gives them the tools to figure out why something isn't working when they eventually hit a wall.

JavaScript's abstract evaluations, while useful in many cases, are at the core of countless bugs. I'm not sure how knowing what they are is scary, or "perfectionism", but my sincere apologies to anyone experiencing nightmares while in pursuit of perfect variable equality!

(Yes, meant truthy.)