Helping making more efficient code

Avram

Member
Hi all,

Is there a more efficient way to write the following code?

If the enemy is hit with obj_fire_up then the enemy's health (blob_health) is decreased.

I have five frames of damage for it to cycle through (e.g. health at 50, move to frame 01, health at 40, move to frame 02, etc.)

Thanks in advance!

GML:
if instance_place(x,y,obj_fire_up)
{
    blob_health -= 1;
    spin += 0.1;
    //bad code to animate
    if blob_health = 50
    {
        image_index = 1;
        instance_create(x,y,obj_blob_bit);
    }
    if blob_health = 40
    {
        image_index = 2;
        instance_create(x,y,obj_blob_bit);
    }
    if blob_health = 30
    {
        image_index = 3;
        instance_create(x,y,obj_blob_bit);
    }
    if blob_health = 20
    {
        image_index = 4;
        instance_create(x,y,obj_blob_bit);
    }
    if blob_health = 10
    {
        image_index = 5;
        instance_create(x,y,obj_blob_bit);
    }
    if blob_health = 0
    {
        for (var i = 0; i < 6; i += 1)
        {
            instance_create(x,y,obj_blob_bit);
        }
        instance_destroy();
    }
}
 

TailBit

Member
just the bad code part
GML:
    //bad code to animate
    if (blob_health mod 10) == 0
    {
        image_index++;
        for (var i = 0; i < 1 + (blob_health == 0)*5; i += 1)instance_create(x,y,obj_blob_bit);
        if blob_health == 0 instance_destroy()
    }
 

FoxyOfJungle

Kazan Games
@TailBit
I think you don’t need to use the for loop, although it’s not relevant, you can use repeat:

GML:
if (blob_health mod 10 == 0)
{
    image_index += 1;
    repeat(5) {instance_create(x,y,obj_blob_bit);}
    if (blob_health == 0) {instance_destroy();}
}
Although I know that "repeat" is a loop, it makes the code more compact.

 
Last edited:

TheouAegis

Member
Both loops are different than what OP had. oP created 1 blob bit every time hp was reduced to a multiple of 10, not for each 10 hp lost. So you don't need the loop at all.

Also that loop will just create 5 blob bits in the exact same place, which is pointless unless each bit is given a UNIQUE movement.
 

TailBit

Member
So just use repeat(1 + (blob_health == 0)*5) then?

And he could be setting random direction in their create event
 

Avram

Member
Well spotted! Yes, each time the enemy loses 10 points of health it transitions to the next frame and generates obj_blob_bit which, as TheouAegis suggested, does have its own unique direction. Thanks again!
 

FoxyOfJungle

Kazan Games
Also that loop will just create 5 blob bits in the exact same place, which is pointless
However this was not specified, for example, in the create event of obj_blob_bit, there could be some kind of movement, such as using the built-in speed and direction functions... This is a "trick" to create particles without much effort.
 

TailBit

Member
You'd just have the instance_create by itself.
The original code creates 6 blobs on the final hit .. did you miss that?

But alright, I'll let it stay alone and instead add the repeat(5) in the dead check..
GML:
    //bad code to animate
    if (blob_health mod 10) == 0
    {
        image_index++;
        instance_create(x,y,obj_blob_bit);

        if blob_health==0{
            repeat(5)instance_create(x,y,obj_blob_bit);
            instance_destroy();
        }
    }
 
Top