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

GML Player Instance Destroyed When Switching States

T

TheSandwiche

Guest
I've been handling my player's different attacks with a state machine, but I've encountered a recurring issue where if the player initiates an attack in midair at a certain close distance from the ground, the player object is destroyed and their hurtbox is left behind and it's occurring before a hitbox is generated on image_index 3. I've verified that the object is being destroyed and not just passing through the ground or having a weird issue with it's alpha value. 90% of the time when a player does a mid-air attack and lands on the ground mid-animation, it just cancels the animation, destroys the invisible hitbox, and the player transitions back to their normal 'free' state, but the other 10% of the time this bug occurs. I've already searched the forums with no luck. Any ideas?

I've posted the code for the mid-air attack state where I think the issue is coming from below:
Code:
vsp = vsp + grv;
var move = key_Right - key_Left;
hsp = walksp * move;
if(place_meeting(x+hsp, y, oWall))
{
    hsp = 0;
}
y = y + vsp;
x = x + hsp;
if (facingRight)
{
face_Direction = 1;  
facingLeft = 0;
} else if (facingLeft)
{
    face_Direction = -1;
    facingRight = 0;
}
if (vsp < 0) && (!key_Jump_Held) {
vsp = vsp + 0.6;  
}
//Start of the attack
if (sprite_index != Player_AirAttack_Right)
{
    sprite_index = Player_AirAttack_Right;
    image_index = 0;
    ds_list_clear(hitByAttack);
}
if (hitBoxCount < 1 && image_index >= 2)
{
hitbox = hitbox_create(30, 40, 10, -21, 180, 1, 1);
hitBoxCount = 1;
}

var hitByAttackNow = ds_list_create();
if(hitbox != -1){
    with(hitbox){
        x = other.x + xOffset;
        y = other.y + yOffset;      
        mask_index = Hitbox;
            var hits = instance_place_list(x,y, oEnemies, hitByAttackNow, false);
            if (hits > 0)
            {
            for (var i = 0; i < hits; i++)
                {
                var hitID = hitByAttackNow[|i];
                if (ds_list_find_index(hitByAttack,hitID) == -1)
                {
                    //then add it to the list
                    ds_list_add(hitByAttack,hitID);
                    with (hitID)
                    {
                        show_debug_message("Collision detected");
                        HP = HP - 1;
                    }
                }
                }
            }
  
    }
  
}
if(place_meeting(x, y+vsp, oWall) && vsp > 0)
{
    vsp = 0;
    grv = 0;
    sprite_index = Player_Idle_Right;
    state = PLAYERSTATE.FREE;
    if (hitbox){
    instance_destroy(hitbox);
    hitBoxCount = 0;
    }
} else if (place_meeting(x, y+vsp, oWall) && vsp <=0){
vsp = 0.1 + grv;  
}
ds_list_destroy(hitByAttackNow);
mask_index = Player_Idle_Right;
if (animation_end())
{
        sprite_index = Player_Fall_Right;
        state = PLAYERSTATE.FREE;
        mask_index = Player_Idle_Right;
        instance_destroy(hitbox);
        hitBoxCount = 0;
}
 
O

Odlaw

Guest
Does the player instance get destroyed at any point or just the hit boxes? Would you be able to provide more code for the player and or state machine?
 

TheouAegis

Member
Check if hitbox is greater than 0. Sounds like hitbox is -1, which is self i think, so the player is destroying itself.

-1 is self
-2 is other
-3 is all
-4 is noone
 
Top