Using the suggestions above, I have refined the code. Here is the current version:
-- Follower's Movement
-- Initialize table to store the hero's old coordinates <List[0] = x, List[1] = y>:
List = {}
-- Obtain the hero's movement speed
local speed = hero:get_walking_speed()
-- Initialize the movement
local movement = sol.movement.create("target")
movement:set_target(Follower:get_position())
movement:set_speed(speed)
movement:start(Follower)
-- Movement pathing:
function hero:on_position_changed(x, y, layer)
-- Update list and move accordingly
-- If this is not the hero's first movement:
if List[0] ~= nil then
-- Move the NPC to the old coordinates
movement:set_target(List[0], List[1])
end
-- Record the hero's current position:
List[0] = x
List[1] = y
end
Initially, the movement works exactly as I would like. However, there are three problems that appear after the hero either stops moving or changes direction:
1. The NPC "Follower" continues moving after reaching the target position.
2. It is impossible for the hero to move backwards since Follower is in the way.
3. When I move the hero in a new direction, I want Follower to continue moving forward before then moving in the new direction, as I want Follower to follow the exact same path as the hero. Follower does not do this; I suspect this problem is related to problem 1.
Three questions:
1. How do I remove collision between the NPC Follower and the hero?
2. Any suggestions for how to make Follower stop moving when the hero stops moving?
3. Any idea what could be causing the third problem?
Incidentally, after playing Ocean's Heart recently, I reached a point in the game where an NPC follows the hero, and the NPC does exactly what I'm trying to get Follower to do: they follow the exact path the hero just walked. Does anyone know how Ocean's Heart did this?
UPDATE 2: I added the line, "Follower:set_traversable(true)", so now the hero can pass through Follower. The other two problems remain; Follower continues moving either left-and-right if facing up or down or up-and-down if facing left or right after the hero stops moving, and the movement only works the way that I want it to until the hero changes direction:
When I have the hero go in a new direction, Follower should move to the last place the hero was in the first direction before moving in the new direction, since I want Follower to follow the same path as the hero. Instead, Follower stands still for a while and then catches up. Any idea what is causing this?
There's another problem: despite Follower's speed being set to the hero's speed in the code, when I tested it, Follower slowly begins falling behind.
UPDATE 3: I set Follower to ignore obstacles to see if that was the reason it stood still whenever the hero turned to the left or right. It was the reason, and it revealed a different problem: despite the existence of the table being to record where the hero was previously, whenever the hero stops moving, follower stops at the hero's current position, not the hero's position a step previously.