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

NPC disappearing when adding states.

ChrisC

Member
if i put no code in the thje step his normal assigned sprite shows but if i put code in there he doesnt show up.

create code
GML:
//triggers
alarmed = false;
scared = false;
alarmrange = 100;
panicrange = 50;


z = 0;
//states
IDLE = 10;
ALARMED = 11;
SCARED = 12;


state  = IDLE;
step event
GML:
switch (state) {
    case IDLE:
        image_speed = global.animFPS/2;     
        sprite_index = asset_get_index(sNPCman1idle);     
    break;
    
    case ALARMED:
        image_speed = global.animFPS/2;     
        sprite_index = asset_get_index(sNPCman1alarmed);   
    break;
    
    case SCARED:
        image_speed = global.animFPS/2;     
        sprite_index = asset_get_index(sNPCman1scared);     
    break;   
}

if instance_exists(oParPlayer)  // if there's any Player instance alive
    {nearestOpponent = instance_nearest(x, y + z, oParPlayer);  } // the enemy looks for the nearest one
    
//add if punch eventually
if distance_to_object(nearestOpponent) < alarmrange
{state = ALARMED}
 

TheouAegis

Member
asset_get_index() requires a string for the argument and is only meant for when you are concatenating said string (or maybe doing stuff with external files). As Nidoking said, in your case, you have absolutely no need for asset_get_index(). And the reason the sprite disappears is because asset_get_index(sNPCman1Idle) is returning -1, which means "no sprite".
 

Yal

šŸ§ *penguin noises*
GMC Elder
Just use the sprite names directly. asset_get_index is used for parsing text data, so it doesn't do what you think it does.
GML:
sprite_index = sNPCman1idle;

Also, in the future, when you have a function that can return an invalid value (like asset_get_index), you should check for invalid values and do some error-handling, so you don't get more glitches where your sprites disappear because you set them to garbage data :p
 
Top