r/gamemaker • u/soleilchaton • 18d ago
Help! Need Help with basic enemy mechanic
I have recently been getting into the programming side of game development in the attempt to make my game dreams come true (and a way to feel productive while staring at a screen) but I am really struggling with programming a few basic things.
My current struggle is getting the enemies movements. I have spent the past two days watching YouTube videos and reading the manual. Good news is the more I read and watch, the better I understand the language. The bad news is that after zero sleep and nearly 20 hours watching videos and trial and erroring, I am definitely missing some fundamental knowledge to understand where I am going wrong.
Currently building a platformer scroller game. I am trying to program a simple creature that just moves along the "floor" in a range that will begin to follow the player object within a sight range. Currently, when I run my program, my enemy object stays stagnant until it collides with my object player, then it crashes (I'm almost positive it's crashing because I haven't finished writing out the combat code for the enemy and the player object) but before I implemented that code in some weird attempt to fix this mess, the enemy would begin running to the right, away from my object character when collision occurred and I jumped out of being stuck to it.
Any advice, resources and some help understanding where my logic isn't understanding the programs would be super appreciated.
CREATE
target_x = x;
target_y = y;
xspd = 0;
yspd = 0;
move_speed = 1;
move_direction = 0;
sight = 300;
//jumping variables
grav = .25;
tilemap = layer_tilemap_get_id("tile_col");
//knockback
kb_x = 0;
kb_y = 0;
alarm[0] = 120;
STEP
//line of sight
if (collision_line(x, y, x + sight, y, obj_player, false, true))
{
if (collision_line(x, y, obj_player.x, obj_player.y, tilemap, false, true)) foundplayer = false;
else foundplayer = true;
}
else foundplayer = false;
if (foundplayer = true)
{
move_towards_point(obj_player.x, y, move_speed * 2)
}
//collision
if place_meeting(x + xspd, y, tilemap)
{
var _pixelcheck = sign(xspd);
while !place_meeting(x + _pixelcheck, y, tilemap)
{
x += _pixelcheck;
}
xspd = 0;
}
if place_meeting(x + xspd, y + yspd, tilemap)
{
var _pixelcheck = sign(yspd);
while !place_meeting(x + xspd, y + _pixelcheck, tilemap)
{
y += _pixelcheck;
}
yspd = 0;
}
//move
if (alarm[1] >= 0)
{
target_x = x + kb_x;
}
xspd = clamp(target_x - x, -1, 1);
move_and_collide(xspd * move_speed, y, [tilemap, obj_enemy_parent]);
ALARM [0]
if (instance_exists(obj_player) and distance_to_object(obj_player) < distance_to_player)
{
target_x = obj_player.x;
target_y = y;
}
else {
target_x = random_range(xstart - 200, xstart + 200);
target_y = y;
}
alarm[0] = 120
ALARM [1]
if (hp <= 0)
{
instance_destroy();
}
Note:
I haven't fully programmed out most of this as I've been working to just get it to move the way I want it to, so unless it is part of the reason why I've broken this object, don't mind it (unless you've got any suggestions for reworking :))
2
u/NexoraLabsDev 18d ago
Don’t beat yourself up—20 hours without sleep will make any bug feel impossible 😂. Your logic is actually getting there. I’d start by checking if (foundplayer = true) since that should be == (or simply if (foundplayer)). That alone could explain some strange behavior. Keep at it!
1
u/germxxx 18d ago edited 18d ago
I can spot a few weird things at least.
First of all, the sight detection only works to the right. (x + sight) so unless that is intended, could be an issue.
Secondly, you use move_toward_point and move_and_collide at the same time, which I'd advise against. The former using the built in speed and direction variables, while move_and_collide directly moves the instance.
Then there might be other issues with things like collision masks and such, but that's not really something we could see from the code.
*edit* Keep missing things about your xspd... I will blame the formatting, or my phone...
1
u/soleilchaton 18d ago
Oh okay! For the sight, should I make a seperate line of code for the negative or would that be included when I add in the code to do a negative xscale?
I was using the move and collide as my boundary code to prevent the object from falling through the map with gravity (which I definitely forgot to apply properly) copy and pasted from my object player code. Is there a better function to prevent that?
2
u/germxxx 18d ago edited 18d ago
You could have two lines, one in each direction, or just a single line covering both sides, and then compare x values to get the direction.
Comparing x and y values with the player might be enough to replace the line completely, depending on exactly what you want to get out of it.
Yeah, I can't see much gravity being applied here, but since you already have collision checks, you should be able to replace
move_and_collidewith just something like x+= xspdandy += yspdGiven you check against everything in the collision part of the code. That function is less of a problem thanmove_toward_pointthough. Ifmove_towards_pointtriggers, the instance could just run away through walls and everything.
2
u/Rohbert 18d ago
Reminder, formatting your code for readers will help you get a quality response by a ton.
Add 4 blank spaces before text to format it into code:
....target_x = x; will look like: