• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Why do I need a 2 frame delay here

Bentley

Member
Hello, sorry for the vague title, but it was hard to phrase. Basically, I have two objects, obj_control and obj_level.

obj_level creates a surface, draws spr_level to that surface, and then uses sprite_create_from_surface to create a sprite and assign that sprite to itself. So obj_level gets its bounding box in the first frame of the game in its draw event.

obj_level
Draw
Code:
if (surf == -1)
{
    surf = surface_create(room_width, room_height);
    surface_set_target(surf);
    draw_clear_alpha(c_black, 0);
    draw_sprite(spr_level, 0, 0, 0);
    sprite = sprite_create_from_surface(surf, 0, 0, room_width, room_height, true, true, 0, 0);
    sprite_index = sprite;
    surface_reset_target();
}

draw_self();

obj_control loops through the room checking for a position_meeting with obj_level. Because obj_level won't have its bounding box until its draw event, I decided to wait 1 frame before obj_control does the position_meeting checks.

obj_control
Create
Code:
alarm[0] = 1;
Alarm[0]
Code:
for (var xx = 0; xx < room_width; xx++)
{
    for (var yy = 0; yy < room_height; yy++)
    {
        if (position_meeting(xx, yy, obj_level))
        {
            grid[# xx, yy] = c_white;
        }
    }
}
The above did not work. Every position_meeting check returned false.

I spent a lot of time trying to fix this because I assumed there was nothing wrong with this order:

-frame 1:
obj_control sets alarm[0] to 1
obj_level's draw event runs and it gets its bounding box via sprite_create_from_surface

-frame 2:
obj_control's alarm triggers, and it loops through the room checking for a position_meeting with obj_level.

I tried settting alarm[0] from 1 to 2, and it worked. I want to avoid headaches like this again, so does anyone know why the 1 frame delay wasn't enough? Am I missing something obvious? Thanks for reading, and let me know if you need me to clarify anything.
 
Last edited:
Top