So I've been trying to create a top down rpg party system where an npc will follow the player. However, the script I have makes the npc move a bit like a magnet (the player can only move in 8 directions but this npc floats towards it smoothly). I need a way for the npc to repeat the player's 8 directional movement, but with a delay. I'm not sure how to accomplish this though.
How would I make an object repeat the movements of the player?
You can call npc's movement commands with delay using Tween. interpolate_callback()
Thank you for your response When using tweens I have only ever used interpolate_property, I have not used interpolate_callback() before and am not really sure how it works. Also, would this stop the npc moving in more than 8 directions or would i have to do something separate to fix that?
- Edited
Usually, when you use an npc follow, you don't want it to exactly follow. If you do, that would work. Like the characters in the npc that have a line of followers behind them. But, generally you use navigation to get a path that avoids obstacles and gets close to the player but not too close. With 8 directions you would want to use astar navigation, but that means setting up a grid system. It works well with tile type maps. You give it AI behaviour similar to enemies.
- Edited
@ProbablyTooAmbitious said: Thank you for your response When using tweens I have only ever used interpolate_property, I have not used interpolate_callback() before and am not really sure how it works. Also, would this stop the npc moving in more than 8 directions or would i have to do something separate to fix that?
Good way would be to define a function in npc and player scripts that executes one move in a given direction. Something like move(direction). When player's move function is then called, it in turn calls npc's move function but with delay using Tween.interpolate_callback(npc, deay, "move", direction).
Note that this will do exactly what you asked in your original question - i.e. copy player movements verbatim. However, if you're dealing with roguelike maze-type environment then this will not suffice as npcs and player are not on the seme tile, thus a npc may get stuck in obstacles if just echoing player moves. In that case you'll need something like what @fire7side touched upon.
- Edited
Good news! I managed to get it working! However, an issue arises where the npc will teleport from the position it should be in to the player's position and then back rapidly. My code is:
func move(target):
move_tween.interpolate_callback(self, 0.45, "move", player.position)
move_tween.interpolate_property(self, "position", position, target, 0)
move_tween.start()
I think this has something to do with the duration in interpolate_property being 0, but if I change that the npc will no longer follow the player's exact movements and it functions the same as how it was before I added in interpolate_callback. I couldn't figure out how to get it working without interpolate_property.