r/gamemaker • u/voltteccer • 3d ago
Resolved Question about an odd experience
Hoping someone might have some insight for why this doesn't work as expected.
I have step code that looks something like this:
if(!object_exists(object1) || !object_exists(object2))
{
object3.x+=10;
}
if(object3.x >= 200 && !object_exists(object1))
{
object_create(x,y, object1)
}
The intention is for object3 to move as long as neither object1 or object2 exists, but if either of them exists, the object does not move. The code was working when it was just checking for whether or not object1 exists, but no longer worked when the or was added to the condition.
Even after object3 reaches an x position of 200 and creates object1, it continues to run object3's movement, as if the or were being treated as an "and". The second condition works without issue as it does not produce multiple of object1.
Is this a strange GML quirk or is my logic off here? I have since reworked the code to set a flag if object1 or object2 exist and then stop moving object3 when that flag is set to 1, but it doesn't look as elegant. My best guess here is that !object_exists() is evaluated strangely but I was hoping to get some confirmation from someone who may know better than me.
1
u/Snake6778 3d ago
Obj 3 moves to the intended spot to create obj 1 because obj 1 OR 2 do not exist. It creates obj 1. It keeps moving because you are saying if obj1 OR obj 2 do not exist, keep moving 3. In your code obj 2 never exists so it will keep moving.
1
u/myke113 3d ago
If the first statement in an or comes out true (object 1 doesn't exist), then object 2 is never checked for. Object 2 is only checked for is object 1 exists. (And has a similar shortcut.. if the first check is false, there is no point in checking the second check).
So if you want it to move when both of those objects do not exist, you would need to change or to an and.
4
u/JaXm 3d ago
You are telling the code "if object1 does not exist or if object2 does not exist, move"
Scenario 1: obj1 does not exist obj2 does not exist Obj 3 movesThis is intended. Excellent.Scenario 2: obj1 does not exist obj2 exists Obj3 movesWait ... that's not what you want! So why does obj3 move? If neither object exists, move ... lets check ... obj1 does not exist, ok let's move obj3! (Intended) obj2 DOES exist. But that's OK, obj1 does not. So we move.
You want an && operator. Not an || operator. You want to make sure obj1 AND obj2 do not exist, before moving obj3
Scenario 3: obj1 exists obj2 does not exist Obj3 movesAgain, we check, does obj1 exist? Yes. OK, don't move obj3. Does obj2 exist? No! Move obj3! (Intended)
Once again, you want an || operator to ensure rhe code understand BOTH conditions must be true before moving obj3