r/gamemaker • • 3d ago

Resolved Why is the else statement breaking this?

if (global.split == 2){
global.split = 1;
} 
else {
global.split = 2;
}

Essentially, I got two instances of an object, and I want to essentially swap control between them with a button press. I got a global variable for that and got spawn conditions to have a local variable different for each instance, probably not the most efficient way but I'm just getting started.

Now, when I *just* do the if statement it properly swaps to the other instance. However, I want to be able to swap back and forth, which I can't do with just that, so I added an else.
If the variable is 2, it's supposed to make it 1.
If the variable isn't 2, it's supposed to make it 2.
Pretty simple.
But for some reason the else breaks it. It just doesn't swap at all and I don't get why.

1 Upvotes

24 comments sorted by

4

u/Majestic-Singer7503 3d ago

global.split = 3 - global.split;

1

u/Privatizitaet 2d ago

I tried, it doesn't work. I get what it's doing, and it SHOULD work, but... for some reason it just doesn't and I don't get WHY.
The only time the variable is altered besides this is when the instance is created, so why would that affect it?
And even weirder, if I just set the value to 1 it works. I just don't understand why I can't set it back to 2.

4

u/Hammerofsuperiority 2d ago

Wait a moment... Do you have that code in the object that you spawn twice?

Because if that is the case, then that is the problem, every button press is running the code twice, once in every instance.

2

u/Privatizitaet 2d ago

Yeah, that wasa the issue, I got it fixed now

0

u/ErikLeppen 2d ago

Shorter, yes, but bad for readability and understandability.

3

u/Majestic-Singer7503 2d ago edited 2d ago

ok...

// SWITCH 1 <-> 2 

global.split = 3 - global.split

-_-

1

u/germxxx 2d ago

Can always go the route of ternary. Also poor readability for those who aren't used to them.
But at least the intent is clear, which "3-variable" isn't.

global.split = global.split == 2 ? 1 : 2

Which is just the same thing that it was originally, which you can also put on one line if you want.

if global.split == 2 then global.split = 1; else global.split = 2

Either way, none of this actually fixes the problem at hand, or actually changes anything.

1

u/No_Assistant_2554 3d ago

I assume this code is in the step event. It does not break it just follows the logic you are giving. You are essentially saying: if split is 2 make it 1, else (as in if split is not 2) make it 2. So if you variable is 1 the else statement kicks in and makes it 2. Basically ist circles through this constantly.

1

u/Privatizitaet 3d ago

Not in the step event. Key press.

4

u/Hammerofsuperiority 3d ago

Use pressed.

1

u/BobHobbsgoblin 2d ago

Make it so that when your key gets pressed it increases global split by one, put a line into your step event that if global split is greater than two then global split = global split minus 2.

That'll make it so that it's always only ever going to be one or two.

1

u/Privatizitaet 2d ago

But that's not the issue. It CANNOT be anything other than 1 or 2 in this case because I'm not adding to the value, I am specifically setting the value to 1 or 2, and I have nothing that adds to the value, so that's just unnecessary. The issue isn't that split is greater than 2. If it were greater than 2 then the whole thing wouldn't work, but it does work in that regard.
I have a local variable that is different between the two instances. 1 or 2. It checks if that variable is equal to global.split and only then does the step event work.
And the step event DOES work. Even after I press the button repeatedly, because the button press just doesn't do anything for some reason if I add the else

1

u/BobHobbsgoblin 2d ago

Im not suggesting a reason for your problem, I'm suggesting you scrap the code for the if statement you posted and replace it with what I put to see if this would work instead

1

u/Privatizitaet 2d ago

But why WOULD that fix it?
I didn't think you were offering a reason, but you offered a solution, one that just wouldn't work, it's not functional. It's incomplete. Sure, I get rid of everything I got right now and make your suggestion. Great, now if global split is greater than 2 it will get reduced by 2.
And what now?
It still has no way to be greater than 2, so that will never happen. Where am I supposed to increase it according to you? What am I missing here?

I did what you said. Scrapped the if statement and put yours into a step event.
Now I have global.split at 2 and nothing that can increase or decrease the value.

So what else do you want me to do to make your solution work? Where should I now put the increase to the value?
I tried just adding to the value when pressing space but that just broke it again.

1

u/BobHobbsgoblin 2d ago

I think you missed parts of what I said the first time.

But ignore everything I said before.

Where your if statement was just replace it with this:

global.WhateverItWas = global.WhateverItWas * -1;

Have one of your objects operate when the variable is 1 and the other operates if it's -1. There's no ifs involved, it will always swap it between those two values

1

u/Privatizitaet 2d ago

Yeah I did miss something, that's on me, sorry for that. Brain skipped over the first sentence.

Anyway, I figured out the issue was because I had two instances of the object and didn't realize they both would do this action, causing it to go back to default since I essentially pressed it twice.
Found a different solution without a global variable though

1

u/BobHobbsgoblin 2d ago

Yeah after I made my last comment I read someone else's thing asking about that. I just sort of had made the assumption that it was in a controller object, or that you only executed the code in the active object but not both at the same time. Happens to us all

3

u/Melodic_Gold4862 3d ago

As a general rule I'd put your code inside of the step event using code checks for the key press as it makes it easier to manage and debug, I've always found other gamemaker events like Key Press to be frustrating for this kind of thing.

The alternative is to use a Switch statement to create a mini state machine as someone else said. These are a good thing to get used to using in general and are more robust than if/else checks.

0

u/sylvain-ch21 hobbyist :snoo_dealwithit: 3d ago

this code should be fine; do you reset the value of global.split to one in a step event or anything else somewhere?
also it is in the key press (would run everyframe which could be the reason it feels like it fails sometime) or the key pressed event (run once)

1

u/Privatizitaet 2d ago

It is run once, and no, the value does not get reset anywhere. Like I said, it somewhat works if I remove the else

1

u/Majestic-Singer7503 3d ago

No. 2 -> 1 and immediately 1 -> 2

1

u/Privatizitaet 2d ago

But why would it do that? It's a single instance that happens once when the key is pressed. Why would it run both the if AND the else at once? The whole point of that statement is to be either or, no?
So if global.split == 2 why would it run the else? That's what I'm not understanding

-1

u/nainainaii 3d ago

Tbh not sure why you’re having this issue, maybe a switch case would work?

Switch(global.split)

{

Case 1:

Global.split = 2

Break;

Case 2:

Global.split = 1

Break;

}

-1

u/Majestic-Singer7503 3d ago

global.split = 3 - global.split; 1 single line