• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Enemies are not working

obama

Member
I'm watching Heartbeast's tutorial on eme,y states and I type in the code he typed, but the game keeps crashing every time I get near an enemy

Code for the enemy wander state
Code:
///scr_enemy_wander_state()
scr_check_for_player();
phy_position_x += sign(targetx - x) * spd;
phy_position_y += sign(targety - y) * spd;
Enemy chase state
Code:
///scr_enemy_chase_state()
scr_check_for_player();
    phy_position_x += sign(targetx.x - x)*spd;
    phy_position_y += sign(targety.y - y)*spd;
enemy idle state code
Code:
///scr_enemy_idle_state()
scr_check_for_player();
check for player code
Code:
///scr_check_for_player()
if (instance_exists(obj_player)){
    var dis = point_distance(x, y, obj_player.x, obj_player.y)
    if (dis < sight) {
        state = scr_enemy_chase_state;
        targetx = obj_player.x;
        targety = obj_player.y;
    } else {
         scr_enemy_choose_next_state();
         }
} else {
    scr_enemy_choose_next_state();
    }
enemy choose next state
Code:
///scr_enemy_choose_next_state
if (alarm[0] <= 0) {
        targetx = random(room_width);
    targety = random(room_height);
    state = choose(scr_enemy_wander_state, scr_enemy_idle_state);
    alarm[0] = 60*irandom_range(2, 4);

}
I'm using GM2 by the way. Can someone help me on this
 

Attachments

The "targetx.x" and "targety.y" entries look redundant- targetx is already returning the target's x value, same for targety and the y value.
 

Yal

šŸ§ *penguin noises*
GMC Elder
Yeah, you're using "targetx" and "targety" everywhere except the script that crashes (which uses "target.x" and "target.y" instead). Remove those dots and it should work, that's why it crashes (you're trying to access the variables of target which doesn't exist).

I type in the code he typed,
Protip: don't just copy code blindly, try to understand how it works too.
 

obama

Member
Yeah, you're using "targetx" and "targety" everywhere except the script that crashes (which uses "target.x" and "target.y" instead). Remove those dots and it should work, that's why it crashes (you're trying to access the variables of target which doesn't exist).


Protip: don't just copy code blindly, try to understand how it works too.
Alright, thanks. I'll try not to copy code blindly
 
Top