GMS 2 Preventing an object from being created twice...?

E

eggyeox

Guest
Hi! I'm an absolute beginner, I've never taken a course or anything, I've been playing around with Gamemaker Studio 2, using tutorials, etc. In the intro of my game, I have a room with an object that, when I press space, creates an instance with my fade in/out object, which switches to the next room. When it's fading and space is pressed again, it starts fading again and skips to the room after. It works perfectly when you just press space once, but if a player mashes the space bar while trying to skip the intro, it glitches and skips the Start screen, so is there a way to prevent the new instance to be created while it fades and the space bar is pressed again?

The object in my first room just has the Create Instance event, which creates the fade in/out object.

As for the fade in/out object I'm using the following codes:
Create:
Code:
al = 0; fade1 = 1; depth = -9
Draw:
Code:
al = clamp(al + (fade1 * 0.05),0,1);
if (al == 1)
{
     room_goto_next();
     fade1 = -1;
}
if (al == 0) && (fade1 == -1)
{
    instance_destroy();
}
draw_set_color(c_black);
draw_set_alpha(al);
draw_rectangle(
        view_xport[0],
        view_yport[0],
        view_xport[0] + view_wport[0],
        view_yport[0] + view_hport[0],
        0
)
draw_set_alpha(1);
That's all, thank you!
 

Paskaler

Member
A quick and dirty way would be to add this at the top of the create event:

Code:
if instance_number(object_index) > 1 {
    instance_destroy();
    exit;
}
Another would be to check if the instance exists when pressing the spacebar:

Code:
if not instance_exists(obj_fade) and keyboard_check_pressed(vk_space) {
    // code
}
 
make a fade_status variable, set it to true after hitting spacebar and to false in that code bit
Code:
if (al == 0) && (fade1 == -1)
{
   instance_destroy();
   fade_status = false;
}
and add "fade_status = false" to your if statement where you check for the space press, further i would use keyboard_check_released if you are not already using it
 
Top