Quote from: Christopho on September 24, 2016, 12:08:32 PM
But what is the problem with obstacles in the first place? If I remember correctly, when an entity with a path movement encounters an obstacle, it is temporarily stopped and the movement continues normally when the route is cleared.
I'm wanting to do something similar to Majora's Mask where NPCs follow precise routes at different times of the day. So at a certain time the NPC will be in an exact spot. If the NPC were to stop temporarily because the player got in their way, then not only would the NPC be delayed in getting to their destination, but there would also be discontinuities in the NPC's movements as the player transitions between maps.
This also means that when the player enters a map, the map script has to calculate where the NPCs are supposed to be depending on what time it is. This turned out to be not as difficult as I thought it would be, and I have code to do this that has been working well.
Quote from: Diarandor on September 24, 2016, 08:09:35 AM
Another solution is making NPCs traversable, for the hero, during their movements. When they stop moving, you can make them to be a solid obstacle, unless the hero is overlapping them (in that case, wait with a timer until the hero stops overlapping the NPC).
I think this solution will work for me. Thanks!
I think I came up with a better solution than using a timer. Once the NPC stops its movement, I assign a function to hero:on_position_changed(), and then every time the hero moves I check if there's still overlap with the NPC. When the hero is not overlapping the NPC, I make the NPC non-traversable again and then set hero.on_position_changed to nil so that the function stops getting called every time the hero moves.
I'll improve the code so that it works with multiple NPCs moving around at the same time (and I'd have to modify it a bit if I ever want to use hero:on_position_changed() for something else), but here's example code for illustrative purposes:
Code (lua) Select
--Code to begin an NPCs movement
local hero = game:get_hero() --this is not a map script, so hero needs to be defined
npc:set_enabled(true)
npc:set_traversable(true)
local mov = sol.movement.create"path"
mov:set_path(event.path)
mov:set_speed(16)
mov:set_ignore_obstacles(true)
mov:set_loop(false)
mov:start(npc, function()
--set npc back to non-traversable once player is no longer overlapping
hero.on_position_changed = function(self, x, y, layer)
if not self:overlaps(npc, "overlapping") then
npc:set_traversable(false)
hero.on_position_changed = nil
end
end
if npc_loc.facing then npc:get_sprite():set_direction(npc_loc.facing) end
end)