• 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!

[SOLVED] Cutting down instance's searched in a with loop

Bentley

Member
I'm working on destructible terrain using objects. I keep cutting blocks into 4th's until they reach a minimum size. I was going off this image: https://www.dropbox.com/s/ml4mggx7vjwvhko/90qQO.png?dl=0

The with loop looks at a lot of blocks that are no where near the collision area. Does anyone have any ideas for reducing the amount of instance's checked?

Just a heads up about the code: the sprites are named based on their size. So s_block_1 is a 1x1 sprite, s_block_16 is a 16x16 sprite, etc.. Also, they are ordered in the resource tree from smallest to largest so I can easily set the sprites. Here's the code:

o_control
Mouse left pressed
Code:
var rad = 8;

// While there are still blocks in the collision area
while (collision_circle(mouse_x, mouse_y, rad, o_block, false, false))
{
    // Loop through all blocks
    with (o_block)
    {
        // If this block is in collision
        if (collision_circle(mouse_x, mouse_y, rad, id, false, false))
        {
            // If it's a 1x1 sprite
            if (sprite_index == s_block_1)
            {
                // Destroy it and don't split (perform events = false)
                instance_destroy(id, false);
            }
            // Otherwise it's a 2x2, 4x4, 8x8, or 16x16 sprite
            else
            {
                // Destroy it and split (perform events = true)
                instance_destroy(id, true);
            }
        }
    }
}
o_block
Destroy event
Code:
// Ex: If the block's sprite is s_block_16 than "next_sprite" equals s_block_8
var next_sprite = sprite_index - 1;

// Create 4 blocks and assign them smaller sprites
with (instance_create_layer(x, y, layer, o_block))
{
    sprite_index = next_sprite;
}
with (instance_create_layer(x + sprite_width / 2, y, layer, o_block))
{
    sprite_index = next_sprite;
}
with (instance_create_layer(x, y + sprite_height / 2, layer, o_block))
{
    sprite_index = next_sprite;
}
with (instance_create_layer(x + sprite_width / 2, y + sprite_height / 2, layer, o_block))
{
    sprite_index = next_sprite;
}
Thanks for reading.
 
Last edited:

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
If you are using GameMaker Studio 2, you might be interested in collision_circle_list. If not, use the returned value from collision_circle and destroy that instance; then run it again to get the next one (or stop)
 
Top