Enemies moving in sync with the player
#1
Posted 14 May 2012 - 04:24 AM
In my game there are certain types of enemies that only move when the player moves. If you've ever played Nethack or similar roguelikes, you understand the sort of system I mean.
I've tried two approaches, both in drag and drop (I'm not nearly good enough to get into coding yet):
One is to have the Step Towards action in the Any Key event, which isn't adequate - I don't want them to move when just any old key is pressed, just the direction keys.
The other is to have a Step Towards action in each of the movement key events - but then if the player is pressing, say, up and right at the same time, the monsters double their speed - so that's no good.
Any help would be appreciated, thanks.
#2
Posted 14 May 2012 - 06:13 AM
e.g:
Player:
Event keypress W:
//move up
Enemy:
Event keypress W:
//move up
Hope this helps
Edited by Katipo Games, 14 May 2012 - 06:16 AM.
#3
Posted 14 May 2012 - 09:22 AM
#4
Posted 14 May 2012 - 09:29 AM
Sorry, I guess I explained that badly. The enemies always move *towards* the player - but they only do so when the player is moving. Thanks anyway.
in the step event of your enemies, try using this :
// This is used to tell an object to move oward a point (position of obj_player in that case) by avoiding "solid" objects mp_potential_step(obj_player.x,obj_player.y,speed,false);
#5
Posted 14 May 2012 - 11:13 AM
Sorry, I guess I explained that badly. The enemies always move *towards* the player - but they only do so when the player is moving. Thanks anyway.
in the step event of your enemies, try using this :// This is used to tell an object to move oward a point (position of obj_player in that case) by avoiding "solid" objects mp_potential_step(obj_player.x,obj_player.y,speed,false);
That's a fine way of getting the enemies to move towards the player, but that hasn't been the issue - I find that the step towards action works well. It's getting them to *only* move when the player is moving that I'm having trouble with. This code doesn't address that. Thanks anyway.
EDIT: Maybe something like making the enemies active if the player speed is returned as greater than zero or something like that might work. I'll give that a try. I should know how to do this, I just did a bunch of tutorials, ugh.
Edited by Drum, 14 May 2012 - 11:21 AM.
#6
Posted 14 May 2012 - 03:43 PM
global.activatedEnemies = false;
In your enemy step event
// Checks if the enemies are ON or not
if (global.activatedEnemies = true)
{
// This is used to tell an object to move oward a point (position of obj_player in that case) by avoiding "solid" objects
mp_potential_step(obj_player.x,obj_player.y,speed,false);
}
In you character step event
// Checks if the speed is higher than 0, if so, activate the enemies
if (hspeed > 0 || vspeed > 0)
{
global.activatedEnemies = true;
}
// Else desactivae them
else
{
global.activatedEnemies = false;
}
That should work if I understood what you wanted
#7
Posted 14 May 2012 - 06:32 PM
make a global variable something like
global.activatedEnemies = false;
In your enemy step event// Checks if the enemies are ON or not if (global.activatedEnemies = true) { // This is used to tell an object to move oward a point (position of obj_player in that case) by avoiding "solid" objects mp_potential_step(obj_player.x,obj_player.y,speed,false); }
In you character step event// Checks if the speed is higher than 0, if so, activate the enemies if (hspeed > 0 || vspeed > 0) { global.activatedEnemies = true; } // Else desactivae them else { global.activatedEnemies = false; }
That should work if I understood what you wanted
Well, I haven't got it to work correctly, but from the looks of the code it's the sort of thing I want, so I'm guessing this is a case of user error?
I put the pieces of code exactly as written into their respective enemy and player object homes. I did not know where or how to insert the initial
global.activatedEnemies = false;bit of code - I tried putting it in various places and then not at all, but none of it worked.
Sorry, I am a bit hopeless with GML. Thanks for babysitting me anyway.
Edited by Drum, 14 May 2012 - 06:35 PM.
#8
Posted 14 May 2012 - 09:21 PM
// The x and y coordinates of the spot that the enemy is moving towards. // I'ts pased on your location. // distance = how far away they will be from you; // direction = what direction they will be from you; // you.xy = your current x or y coordinates. xx = lengthdir_x(distance,direction)+you.x; yy = lengthdir_y(distance,direction)+you.y; move_towards_point(xx,yy,sp); // sp = speed that the enemy will move towards that point at
Edited by adder_astros, 14 May 2012 - 09:25 PM.
#9
Posted 14 May 2012 - 10:41 PM
In the enemy's step event:
// The x and y coordinates of the spot that the enemy is moving towards. // I'ts pased on your location. // distance = how far away they will be from you; // direction = what direction they will be from you; // you.xy = your current x or y coordinates. xx = lengthdir_x(distance,direction)+you.x; yy = lengthdir_y(distance,direction)+you.y; move_towards_point(xx,yy,sp); // sp = speed that the enemy will move towards that point at
Oh bother, it looks like I've really made a hash of this thread because everybody has consistently got the wrong idea. My apologies to everybody who tried to help.
What I wanted was:
As long as the player is moving, the enemies will move towards the player. When the player stops, the enemies stop. It's basically a turn-based system where everybody only moves on the same turn.
I tried implementing FredyJabe's code but it simply didn't work - the enemies stayed rooted to the spot while I pranced about like an idiot. I guess I'll just keep hacking away at it, but I'm not confident of my ability to use GML at all and prefer a drag & drop solution. Thanks anyway adder_astros.
#10
Posted 14 May 2012 - 11:07 PM
global.moving = false;
Player Step event:
// Movement x += (-keyboard_check(vk_left) + keyboard_check(vk_right))*4; y += (-keyboard_check(vk_up) + keyboard_check(vk_down))*4; // Check if we moved if (x == xprevious && y == yprevious) global.moving = false; else global.moving = true;You can use your own movement code, but at the end of the step check if the player moved and set global.moving accordingly.
In enemy Step event:
if (global.moving == true) move_towards_point(Player.x,Player.y,2); else speed = 0;
#11
Posted 14 May 2012 - 11:21 PM
Player Create event:
global.moving = false;
Player Step event:// Movement x += (-keyboard_check(vk_left) + keyboard_check(vk_right))*4; y += (-keyboard_check(vk_up) + keyboard_check(vk_down))*4; // Check if we moved if (x == xprevious && y == yprevious) global.moving = false; else global.moving = true;You can use your own movement code, but at the end of the step check if the player moved and set global.moving accordingly.
In enemy Step event:if (global.moving == true) move_towards_point(Player.x,Player.y,2); else speed = 0;
This worked perfectly, thanks a whole ton. I'll totally credit you if I somehow manage to finish this off - how do you want to be credited? Just Locust?
#12
Posted 14 May 2012 - 11:26 PM
No need to credit me but if you want to, Locust will do.
#13
Posted 14 May 2012 - 11:33 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users











