B
Blatcho
Guest
edit: modified title to reflect forum rules
Hey everyone, long time lurker first time poster...
I've been playing around with GM for a month or so, and have built a basic top down project, to learn the ropes. What I'm looking for a bit of feedback at some of my code regarding NPC/enemy behaviour. It's basically a bunch of nested if/then, but i've read in a few places that arrays are better at handling enemy actions?
This is my obj_enemy 'Enemy Movement' Step event -
And this is the obj_enemy 'Enemy shoot' Step event -
So my questions are -
1. Is there a better way to implement the behaviour above? I think array's perhaps?
2. What is 'best practice' in terms of where code should live - in the object step events, or placing in the 'Scripts' folder? (or something else).
3. What's the best way to manage different object behaviour animations i.e. standing idle, moving, moving and shooting, running, running & shooting etc should I separate out individual objects i.e. body, gun or create single animations for each state and use an array to determine which to use?
4. I've read that using the drag/drop wizards can be restricting, so I've avoided using them, is this generally accepted as the way to go?
Finally, as per tradition whenever I enter a new forum, apologies for any newbie mistakes in posting
Hey everyone, long time lurker first time poster...
I've been playing around with GM for a month or so, and have built a basic top down project, to learn the ropes. What I'm looking for a bit of feedback at some of my code regarding NPC/enemy behaviour. It's basically a bunch of nested if/then, but i've read in a few places that arrays are better at handling enemy actions?
This is my obj_enemy 'Enemy Movement' Step event -
Code:
///Enemy movement
//Check to see whether can see player (within 45 degree angle, no obstruction and within range)
if (abs(angle_difference(varDir,image_angle)) <= 45 && varCanSeePlayer && (varDistanceToPlayer < varRange))
{
enemyX = obj_player.x;
enemyY = obj_player.y;
mp_potential_path(varMyPath, enemyX, enemyY,3,1,0);
path_start(varMyPath,3,path_action_stop,0);
//image_angle = point_direction(x,y,enemyX,enemyY);
image_angle = direction;
}
else
//Check object path state
if path_position != 1
{
//Ensure new x,y coordinates are empty
if !place_empty(enemyX, enemyY)
{
enemyX += 100;
enemyY += 100;
}
//Check if object is static
if varMoveSpeed = 0
{
//Reset current time
varTimeCurrent = current_time;
//Check if time lapsed is greated than interval
if (varTimeCurrent - varTimeStart >= varInterval)
{
varMoveSpeed = irandom(2);
varInterval = irandom(5000);
}
}
//Set path
mp_potential_path(varMyPath, enemyX, enemyY,3,1,0);
path_start(varMyPath,varMoveSpeed,path_action_stop,0);
//image_angle = point_direction(x,y,enemyX,enemyY);
image_angle = direction;
}
else if path_position = 1
{
//Reset timer, randomize move speed and x,y
varTimeStart = current_time;
varMoveSpeed = irandom(2);
enemyX = random(1024);
enemyY = random(768);
//Ensure new x,y coordinates are empty
if !place_empty(enemyX, enemyY)
{
enemyX += 100;
enemyY += 100;
}
//Set path
mp_potential_path(varMyPath, enemyX, enemyY,3,1,0)
path_start(varMyPath,varMoveSpeed,path_action_stop,0);
//image_angle = point_direction(x,y,enemyX,enemyY);
image_angle = direction;
}
Code:
///Enemy shoot
varShootCoolDown-=1;
varCanSeePlayer = !collision_line(x,y,obj_player.x,obj_player.y,obj_wall,false,true);
varDistanceToPlayer = distance_to_object(obj_player);
varDir = point_direction(x,y,obj_player.x,obj_player.y);
//var varIsInsideRadius = distance_to_point(obj_player.x, obj_player.y) < varRadius;
if (abs(angle_difference(varDir,image_angle)) <= 45 && varShootCoolDown<=0 && varCanSeePlayer && (varDistanceToPlayer < varRange))
{
varShootCoolDown = 10;
varBullet = instance_create(x,y,obj_projectile_enemy);
varBullet.speed = 15;
varBullet.direction = point_direction(x,y,obj_player.x,obj_player.y);
}
1. Is there a better way to implement the behaviour above? I think array's perhaps?
2. What is 'best practice' in terms of where code should live - in the object step events, or placing in the 'Scripts' folder? (or something else).
3. What's the best way to manage different object behaviour animations i.e. standing idle, moving, moving and shooting, running, running & shooting etc should I separate out individual objects i.e. body, gun or create single animations for each state and use an array to determine which to use?
4. I've read that using the drag/drop wizards can be restricting, so I've avoided using them, is this generally accepted as the way to go?
Finally, as per tradition whenever I enter a new forum, apologies for any newbie mistakes in posting
Last edited by a moderator: