ID:2250553
 
(See the best response by Kaiochao.)
So I am doing something I never thought I'd do. I basically am asking you guys for actual code. So I already have code given by a friend, but it's just horrible for npcs. When the npc follows players to go and attack them they are literally 1 tile behind them always. I try to use delay to slow them down but it just makes them have very jerky movements. This code uses a speed var to determine players movement speed. It works perfectly for players but when it comes to npcs and mobs it is just all wrong.
Best response
There are two kinds of movement: tile movement and pixel movement.

When using tile movement, it's common to have a cooldown time between steps as a way of controlling movement speed. This is typically used in conjunction with glide_size to make glides take exactly as much time as the cooldown, to maximize smoothness; when moving constantly, there should be no point where you stand still.

In pixel movement, steps always occur as frequently as possible (every tick) to maximize smoothness and responsiveness. Movement speed is controlled by the number of pixels moved each tick.

Gonna need more details on what your movement needs are. There is no "one size fits all" movement code for you. All games can do it differently.
I have tile movement due to it being the best method for me.
I would also like to point out, besides Tile and Pixel, there's also three styles of directional movement most BYOND games choose from. I don't recall what the exact terms are but I'll call them Directional, Omnidirectional, and semi-omnidirectional.

Directional is the standard direction movement where you can travel in 4 directions (NORTH, SOUTH, EAST, WEST) but must release your current movement to switch directions. (Ex: Moving NORTH and you want to go WEST, you can't go NORTH-WEST so you must release the UP arrow and then press the LEFT arrow.)

Semi-Omnidirectional is the direction movement where you can travel in the 8 directions, exactly like directional but you can bunch directions together like UP and LEFT would give you NORTH-WEST and etc. If you run into a horizontal wall while going diagonally, this method will stop you in your tracks.

Omnidirectional allows you to smoothly move and switch between the directions without having to fully release keys. This normally coincides with pixel movement, but have seen people stick with semi-omnidirectional instead which causes clunky movement. Whereas, this style movement is very smooth and perfect with pixel-movement. If you run into a horizontal wall while going diagonally, this method will continue your run but by choosing the vertical equivalent of your travel. Ex: Going NORTH-EAST into a wall while still using both NORTH and EAST, will then make you go EAST.


Getting back to the issue at hand, it's hard to say what exactly is the problem without seeing your NPC aggro code, your NPC movement code, and your player movement code.

If you just want copy+paste code you can check out the library section.

http://www.byond.com/ developer/?sort=date&text=tag%3amovement
At the moment I am using

mob
Henchmen
Gargala
icon = 'Gargala.dmi'
name = "Gargala"
proc
AI_Follow()
var/mob/Target
while(src)
for(var/mob/M in oview(4))
Target = M
if(Target)
if(Target in oview(4))
if(Target.client)
walk_towards(src,Target)
spawn(30) AI_Attack()

else
Target = null
sleep(1)
I am using a method by Zecronious named "Vector Movement" and it's perfect with the gliding. I tried to modify it to my game (Note I did not just copy+paste, I literally tried to edit as best as I could). Though the game is laggy because of the code.