• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Beginner, issue with var++ code

S

Spiralnord

Guest
Howdy,
I'm working on a project in GameMaker: Studio and I'm already stuck.
The code is supposed to be simple, this is what is has to do:

If you click ten times on a button, a window will pop up telling you to stop it. The code resets after the window appears.

This is the code I've got:
clicks = 0;
if mouse_check_button_pressed(mb_left) {
if (clicks < 10) {
clicks += 1;
}
else {
show_message("Stop it, lad.");
}
}

Nothing happens after I click it a bunch of times. Help?
 
S

Spikehead777

Guest
It looks like your clicks variable never gets past 1. Why? Well, look at where you set it to 0; every step, you reset it. Your fix, therefore, is to only set clicks to 0 once. You can use the create event of an object, the room start event, the game start event, or even the room creation code.
 
S

Spiralnord

Guest
It looks like your clicks variable never gets past 1. Why? Well, look at where you set it to 0; every step, you reset it. Your fix, therefore, is to only set clicks to 0 once. You can use the create event of an object, the room start event, the game start event, or even the room creation code.
I tried the same code but moved the "clicks = 0;" code to a separate creation code in the object. It still doesn't work. Suggestions?
 

Jezla

Member
What event is the rest of your code in?

clicks = 0;

Should be in the create event. Everything else you posted should be in the step event.
 
S

Spiralnord

Guest
What event is the rest of your code in?

clicks = 0;

Should be in the create event. Everything else you posted should be in the step event.
OH MY GOD IT WORKS NOW.
Thanks a lot, now I'm gonna go learn why it has to be in a step event.
 
Top