GameMaker Is -6 NOT less than 0?

Heeyy, today I found something very weird.
So I was testing out the "sprite_index" expression, event, whatever it is, and made this
very simple code in order to change the sprite:

if !hsp_x = 0
{
sprite_index = camm
}

So, basically, if the variable hsp_x is NOT 0, then it will do the sprite_index thingy.
The hsp_x in my project can ONLY be 6, 0 or -6.
When it's 0, of course, the code won't work; when it's 6, of course, it works, but if it's -6 it does not work for some reasons.

The weird thing is that, later, I re-wrote the thing as:

if hsp_x < 0
{
sprite_index = camm
}
if hsp_x > 0
{
sprite_index = camm
}

And now it worked as intended, even tho the logic applied is the same.
Am I doing something wrong or is GameMaker that has some funny idea of what the number -6 actually means?
Thanks comrades!
 

kburkhart84

Firehammer Games
The problem is basically that you are "notting" the wrong thing.

Code:
if !hsp_x = 0
That line is doing a NOT operation on the hsp_x variable, which in bit-wise is flipping the bits. Then, it is checking if that is equal to 0. What you want is the following...

Code:
if (hsp_x != 0)
It matters where you put things, as code will take the positioning very literal.

*EDIT Forgot that ~ is bit-wise flipping, though the rest of my point is accurate.
 
Last edited:

chamaeleon

Member
In GML number vales less than or equal to 0.5 is treated as true, including -6.
GML:
for (var n = -1; n < 2; n += 0.25) {
    if (n)
        show_debug_message(string(n) + " -- Yea");
    else
        show_debug_message(string(n) + " -- Nay");
}
Code:
-1 -- Nay
-0.75 -- Nay
-0.50 -- Nay
-0.25 -- Nay
0 -- Nay
0.25 -- Nay
0.50 -- Nay
0.75 -- Yea
1 -- Yea
1.25 -- Yea
1.50 -- Yea
1.75 -- Yea
So your test !hsp_x = 0 will be false for 0 and -6, and true for 6. The simplest solution is to avoid the boolean flag on a speed variable as just do hsp_x != 0.
 
The problem is basically that you are "notting" the wrong thing.

Code:
if !hsp_x = 0
That line is doing a NOT operation on the hsp_x variable, which in bit-wise is flipping the bits. Then, it is checking if that is equal to 0. What you want is the following...

Code:
if (hsp_x != 0)
It matters where you put things, as code will take the positioning very literal.
Yeah I had alot of confusion understanding where the ! of NOT goes to make it work. I'm used to DnD that just "nots" the whole thing. Thanks comrade.

Bit flipping would be ~hsp_x. !hsp_x turns a true/greater-than-0.5 value into false/0 and a false/less-than-or-equal-to-0.5 value into true.
What does ~ means in code? Just curious.
 

kburkhart84

Firehammer Games
Bit flipping would be ~hsp_x. !hsp_x turns a true/greater-than-0.5 value into false/0 and a false/less-than-or-equal-to-0.5 value into true.
I stand corrected...I forgot it is a different operation. My point is still valid though as far as the positioning of the '!' operator.
 

chamaeleon

Member
GML:
enum Bit {
    Water = 1,
    Air = 2,
    Earth = 4,
    Fire = 8
}

var Names = ["Water", "Air", "Earth", "Fire"];

function logBits(names, bits) {
    var result = "< ";
    for (var i = 0; i < 4; i++) {
        if (bits & (1 << i))
            result += names[i] + " ";
    }
    result += ">";
    show_debug_message(result);
}

var bits = Bit.Water | Bit.Earth;
logBits(Names, bits);
var bits = ~Bit.Air;
logBits(Names, bits);

var can_walk_on = Bit.Water | Bit.Earth;
var cant_walk_on = ~can_walk_on;
logBits(Names, can_walk_on);
logBits(Names, cant_walk_on);
Code:
< Water Earth >
< Water Earth Fire >
< Water Earth >
< Air Fire >
 

kburkhart84

Firehammer Games
Cool.
I don't know how it could be useful for a fart head programmer like me, but you never know...
I personally have never found a use for it either. It would have to be some lower level number manipulation kind of thing, or some esoteric trick for doing something. Like that above example, you could really just store the values as separate variables and it would be more readable for most people. The main reason you would do something like that in the past was optimization based, but these days it is much less of an issue. I'm sure people have other reasons for doing it though, but I prefer the easy readability of keeping things separate.
 
I personally have never found a use for it either.
The only thing I can really think of off the top of my head would not be of use in GMS, unless you're REALLLLLY old-school.
For example, in some binary formatting, you get the negative value by flipping all the bits and adding one, so basically ((~ value)+1).
But as I said, the use cases are limited and you can achieve the same with much more readable solutions with GML.
 

TheouAegis

Member
Code:
//Snap to 16x16 grid
x &= ~15;
y &= ~15;

//Snap to 32x32 grid
x &= ~31;
y &= ~31;

//Remove Nth element from a binary set
set &= ~n;
Rather than typing out x&=$fffffff0 or x&=$fffffffffffffff0 (I forgot which one GMS would use). That's probably it's most typical use.
 
The problem is basically that you are "notting" the wrong thing.

Code:
if !hsp_x = 0
That line is doing a NOT operation on the hsp_x variable, which in bit-wise is flipping the bits. Then, it is checking if that is equal to 0. What you want is the following...

Code:
if (hsp_x != 0)
It matters where you put things, as code will take the positioning very literal.

*EDIT Forgot that ~ is bit-wise flipping, though the rest of my point is accurate.
Got 2 more problems:
First, I tried "notting" a "less than" by doing
Code:
! <
and it does not work. How can I "not" the "less than" properly?

Second thing, this code here confuses me:
Code:
if anim.vsp != 0
    {
        if place_meeting(x+anim.hsp_x,y,bar3)
        {
            while !place_meeting(x+sign(anim.hsp_x),y,bar3)
            {
                x = x + sign(anim.hsp_x)
                with (anim2)
                {
                    x = x + sign(anim.hsp_x)
                }
                with (corp)
                {
                    x = x + sign(anim.hsp_x)
                }
            }
            anim.hsp_x = 0
        }
        if place_meeting(x,y+anim.hsp_y,bar3)
        {
            while !place_meeting(x,y+sign(anim.hsp_y),bar3)
            {
                y = y + sign(anim.hsp_y)
                with (anim2)
                {
                    y = y + sign(anim.hsp_y)
                }
                with (corp)
                {
                    y = y + sign(anim.hsp_y)
                }
            }
            anim.hsp_y = 0
        }
    }
The
Code:
if anim.vsp != 0
works backwards for unknown reasons to me.
Like, vsp is always 0 for the entire time, so (normally) this code would not work.
But not only it works even if vsp = 0, but it does not work if I change the question to
Code:
if anim.vsp = 0
.
Is like, inverted or something.

To put it simple:
vsp is 0
If the question asks "if vsp ISN'T equal to zero": It works.
If the question asks "if vsp IS equal to zero": It does not work.
 

Roderick

Member
How can I "not" the "less than" properly?
!(x<y) Literally "NOT x is less than y"

or

x >= y "x is greater than or equal to y"

With regards to the second issue, right before the if statement, add show_debug_message(anim.vsp)
Most likely, the value of your variable isn't what you think it should be. If that's the case, go over your other code and figure out what's giving it the wrong value.
 
!(x<y) Literally "NOT x is less than y"

or

x >= y "x is greater than or equal to y"
So basically !(vsp<0) then? Oke I'll try it out.

With regards to the second issue, right before the if statement, add show_debug_message(anim.vsp)
Most likely, the value of your variable isn't what you think it should be. If that's the case, go over your other code and figure out what's giving it the wrong value.
I use a draw_text expression in a debug object that shows me the variable's value, and it's ALWAYS 0.
 

Nidoking

Member
I use a draw_text expression in a debug object that shows me the variable's value, and it's ALWAYS 0.
You forgot to finish your sentence. It's ALWAYS 0 during the Draw event in which you put the draw_text call. How are you proving that it doesn't change value between Draw events?
 
You forgot to finish your sentence. It's ALWAYS 0 during the Draw event in which you put the draw_text call. How are you proving that it doesn't change value between Draw events?
I don't think I understood comrade.
But I programmed that when I press Space the vsp changes to -30, and it does display it. So I think it works.
 
Top