• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Error Message - convert string to bool

H

Halas

Guest
I keep getting this error for this message.
Purpose of the code, not to damage the player while dodging.

Line of code:

Code:
if Obj_Player.state != "backstep" or !Obj_Player.state != "frontstep"   
        {
            other.hp -= damage;
        }

GML:
___________________________________________
############################################################################################
FATAL ERROR in
action number 1
of  Step EventObj_Player
for object Obj_hitbox:

unable to convert string "backstep" to bool
 at gml_Object_Obj_hitbox_Collision_d0aa4282_5d94_470e_b839_a3cb0c2586fb (line 6) -               } else if Obj_Player.state != "backstep" or !Obj_Player.state != "frontstep"   
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_Obj_hitbox_Collision_d0aa4282_5d94_470e_b839_a3cb0c2586fb (line 6)
 

Amon

Member
Is Obj_Player.state a bool 'true/false' or a string variable? The error states that it is a bool as it can't convert the string "backstep" to a true/false bool.
 
H

Halas

Guest
it's a string variable.

So the states the player can be in are:
"move"
"attack"
etc

So, as long as the player is not in its dodge state, it should not take damage.
 

kraifpatrik

(edited)
GameMaker Dev.
It's the ! in !Obj_Player.state. The ! operator works on booleans (negates them), but your variable is a string, so that can't work.
 
H

Halas

Guest
Ah okay, so I had to convert it into a boolean.

But, if I didn't want to convert it to a boolean, was there another way of doing that?
 
H

Halas

Guest
Why not simply
Code:
if (Obj_Player.state != "dodge") {
   // Damage code here
}
?
Is it just the brackets that differentiate it from:

GML:
if Obj_Player.state != "backstep" or !Obj_Player.state != "frontstep"  
        {
            other.hp -= damage;
        }
Reason I have two states is because there are two types of dodging.
 

FrostyCat

Redemption Seeker
On top of getting rid of the extra !, use && (and) instead of || (or). The reason is obvious if you would just take 15 seconds to evaluate your expression with the state at various values. variable != a || variable != b is always true, and it's a classic noob trap that punishes people who disobey De Morgan's Laws.
 

Nidoking

Member
That, or OP intended to write a condition equivalent to Obj_player.state == "frontstep" and just iterated over "this appears to get me closer to my goal, so I'll set it in stone and tack on more stuff until I get what I want" until it no longer made sense.
 
H

Halas

Guest
On top of getting rid of the extra !, use && (and) instead of || (or). The reason is obvious if you would just take 15 seconds to evaluate your expression with the state at various values. variable != a || variable != b is always true, and it's a classic noob trap that punishes people who disobey De Morgan's Laws.
Yes, the extra ! is a typo, alright.

Reason I have two states is because there are two types of dodging.
If the player has two types of dodging. How can it be an && statement?
Using the && would mean the player is enacting both dodges at the same time, no?
So no, the reason isn't as obvious as you say.

But, alas, I have added a new invulnerable variable for the player that changes to true/false when in one of the dodge states.
 

Nidoking

Member
If the player has two types of dodging. How can it be an && statement?
Using the && would mean the player is enacting both dodges at the same time, no?
Let's say that the state is "backstep".

Is it the case that "backstep" is not equal to "backstep"?

Is it the case that "backstep" is not equal to "frontstep"?

Did you answer yes to either of the above questions?

Then your or condition says that the player should take damage.

Did you answer yes to both of the questions?

Then the and condition says that the player should not take damage.
 

FrostyCat

Redemption Seeker
If the player has two types of dodging. How can it be an && statement?
Consider these 3 scenarios:
  • The state is "frontstep"
  • The state is "backstep"
  • The state is something else
Now evaluate state != "frontstep" || state != "backstep":
  • When the state is "frontstep", the second half of the || is true, so the entire expression is true.
  • When the state is "backstep", the first half of the || is true, so the entire expression is true.
  • When the state is something else, both halves of the || are true, so the entire expression is true.
Notice how the expression is NEVER false. This is why you keep taking damage. This is why variable != a || variable != b is a noob trap.

Now evaluate state != "frontstep" && state != "backstep":
  • When the state is "frontstep", only the second half of the && is true, so the entire expression is false.
  • When the state is "backstep", only the first half of the && is true, so the entire expression is false.
  • When the state is something else, both halves of the && are true, so the entire expression is true.
This is the behaviour you're looking for. This is exactly why I said to use &&.

Every single topic I've seen you make thus far comes from you putting hope on code that has not been intentfully written nor double-checked. Start evaluating expressions to the word and checking the results against what's right. Start using truth tables and value tables. Start thinking like a computer when you're dealing with a computer.
 
H

Halas

Guest
While I have since solved the initial issue, this thread is helping me, as you said, to think like a computer. I am about six months into learning coding and I admit that is one of the areas that I have issues with, it is like remolding my thought process. A price, unfortunately, you all are paying as well haha.

Now evaluate state != "frontstep" && state != "backstep":
  • When the state is "frontstep", only the second half of the && is true, so the entire expression is false.
  • When the state is "backstep", only the first half of the && is true, so the entire expression is false.
  • When the state is something else, both halves of the && are true, so the entire expression is true
I now understand this.

And to all fairness, Nido here is correct.
That, or OP intended to write a condition equivalent to Obj_player.state == "frontstep" and just iterated over "this appears to get me closer to my goal, so I'll set it in stone and tack on more stuff until I get what I want" until it no longer made sense.
I have been using this same sequence, and I iterated it out of making sense. Normally I would use it as Obj_player.state == "frontstep" - But since I had changed it to a not statement, it no longer made sense.
 
Top