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

State Machines

C

Captain_ArrowWound

Guest
I creating an enemy which will use state machines to determine where on the map it will go to. My code isn't working because the enemy disappears but still continues firing. I have tried many different ways to fix it, but nothing is seeming to work. So I would approtiate it if someone has idea for what I am doing wrong in my code. Thanks for the help! Here is my code.

obj_enemyMove - create
Code:
enemyValue = choose (1,2,3);

cooldown = 0;

forestx = choose(100,150,200,250,300,350,400,450,500,550,600,650,700,750,800);
foresty = choose(900,850,1000,1050,1100,1150,1200,1250,1300,1350,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2050,2100,2150,2200);

townx = choose(1100,1150,1200,1250,1300,1350,1400,1450,1500,1550,1600,1650,1700,1750,1800,1850,1900,1950,2000,2050,2100,2150,2200);
towny = choose(500,550,600,650,700,750,800,850,900,950,1000,1050,1100,1150,1200,1250,1300,1350,1400,1450,1500);

state = ENEMYSTATE.FREE;
enum ENEMYSTATE
{
    FREE,
    PLAYER,
    FOREST,
    TOWN,
}

depth = -200;
obj_enemyMove - step
Code:
switch (state)
{
    case ENEMYSTATE.FREE: scr_free(); break;
    case ENEMYSTATE.FOREST: scr_forest(); break;
    case ENEMYSTATE.TOWN: scr_town(); break;
    case ENEMYSTATE.PLAYER: scr_player(); break;
}
Script - scr_free
Code:
if (enemyValue == 1)
{
    switch (state)
    {
        case ENEMYSTATE.PLAYER: scr_player(); break;
    }
}
if (enemyValue == 2)
{
    switch (state)
    {
        case ENEMYSTATE.FOREST: scr_forest(); break;
    }
}
if (enemyValue == 3)
{
    switch (state)
    {
        case ENEMYSTATE.TOWN: scr_town(); break;
    }
}
Script - scr_player
Code:
if (cooldown <= 0)
{
grid = mp_grid_create(0,0,room_width/32,room_height/32,32,32);
path = path_add();
mp_grid_add_instances(path,obj_tree,1);
mp_grid_add_instances(path,obj_wall,1);
mp_grid_add_instances(path,obj_window,1);


mp_grid_path(grid,path,x,y,obj_player.x + random_range(-400,400),obj_player.y + random_range(-1000,1000),1);
path_start(path,1.5,0,1);
cooldown = 50;
}

if (distance_to_object(obj_player or obj_friend) <= 350) and (cooldown < 0)
{
    instance_create_layer(x,y,"Bullets",obj_bulletEnemy);
    direction = point_direction(obj_enemyMove.x,obj_enemyMove.y,obj_friend.x or obj_player.x,obj_friend.y or obj_player.y,);
    cooldown = 100;
}

cooldown = cooldown - 1;
Script - scr_forest
Code:
if (cooldown <= 0)
{
grid = mp_grid_create(0,0,room_width/32,room_height/32,32,32);
path = path_add();
mp_grid_add_instances(path,obj_tree,1);
mp_grid_add_instances(path,obj_wall,1);
mp_grid_add_instances(path,obj_window,1);


mp_grid_path(grid,path,x,y,forestx,foresty,1);
path_start(path,1.5,0,1);
cooldown = 50;
}
if (distance_to_object(obj_player or obj_friend) <= 350) and (cooldown < 0)
{
    instance_create_layer(x,y,"Bullets",obj_bulletEnemy);
    direction = point_direction(obj_enemyMove.x,obj_enemyMove.y,obj_friend.x or obj_player.x,obj_friend.y or obj_player.y,);
    cooldown = 100;
}

cooldown = cooldown - 1;
Script - scr_town
Code:
if (cooldown <= 0)
{
grid = mp_grid_create(0,0,room_width/32,room_height/32,32,32);
path = path_add();
mp_grid_add_instances(path,obj_tree,1);
mp_grid_add_instances(path,obj_wall,1);
mp_grid_add_instances(path,obj_window,1);


mp_grid_path(grid,path,x,y,townx,towny,1);
path_start(path,1.5,0,1);
cooldown = 50;
}
if (distance_to_object(obj_player or obj_friend) <= 350) and (cooldown < 0)
{
    instance_create_layer(x,y,"Bullets",obj_bulletEnemy);
    direction = point_direction(obj_enemyMove.x,obj_enemyMove.y,obj_friend.x or obj_player.x,obj_friend.y or obj_player.y,);
    cooldown = 100;
}

cooldown = cooldown - 1;
Thanks Again!
 

TheouAegis

Member
Are you destroying your grid anywhere? because unless I miss that line in your code, you have a serious memory leak going on there.

You also should be using else between each of your enemyValue checks.

Do you have any code in the draw event?

Also debug your coordinates. Maybe your grid is outside the room.
 
I

immortalx

Guest
Code:
if (distance_to_object(obj_player or obj_friend) <= 350)
That doesn't seem right. You should probably write
Code:
if ((distance_to_object(obj_player) <= 350) or (distance_to_object(obj_friend) <= 350))
 

Joe Ellis

Member
Why have you got switches with only one option?


Code:
    switch (state)
    {
        case ENEMYSTATE.PLAYER: scr_player(); break;
    }
}
if (enemyValue == 2)
{
    switch (state)
    {
        case ENEMYSTATE.FOREST: scr_forest(); break;
    }
}
if (enemyValue == 3)
{
    switch (state)
    {
        case ENEMYSTATE.TOWN: scr_town(); break;
    }
these would be the same if you just used an if
 
T

Taddio

Guest
Like Joe said, you'd be better off with a switch(enemyValue) and cases 2: and 3: , in this piece if code, but I guess it works anyway
 
Top