Stopping npcs

Started by Porksteak, May 13, 2017, 04:35:53 PM

Previous topic - Next topic
Is there a way to make it so usual npcs stop when they collide with another usual npc the way that they stop when they collide with a generalized npc? I'm trying to two npcs walk in a circle, but if one of the npcs is stopped the other one will just walk right through it.

May 13, 2017, 05:02:42 PM #1 Last Edit: May 14, 2017, 01:49:47 AM by Zefk
QuoteI'm trying to two npcs walk in a circle, but if one of the npcs is stopped the other one will just walk right through it.

I think you want the set_traversable function? Sets whether this NPC can be traversed by other entities.
http://www.solarus-games.org/doc/latest/lua_api_npc.html#lua_api_npc_set_traversable


QuoteIs there a way to make it so usual npcs stop when they collide with another usual npc the way that they stop when they collide with a generalized npc?

If you want them to stop when encountering another NPC, then use the on_obstacle_reached function.
http://www.solarus-games.org/doc/latest/lua_api_entity.html#lua_api_entity_on_obstacle_reached

May 13, 2017, 10:27:04 PM #2 Last Edit: May 13, 2017, 10:31:22 PM by MetalZelda
use entity:on_obstacle_reached(movement)

http://www.solarus-games.org/doc/latest/lua_api_entity.html#lua_api_entity_on_obstacle_reached

From there you have to configure the behaviour of what the entity do when it collide with something (and obviously, you can change it's movement)

Stopping an NPC ? No problem

Code (lua) Select

function entity:on_obstacle_reached(movement)
  -- Check if any movement exist
  if movement ~= nil then
    -- Stop the movement
    movement:stop()

    -- Do whatever you want here (restart a movement or else)
  end
end



But you have to define this event for each NPC that does what you want
Careful though, this event is called each time the entity collide with something, so, if you want to make this entity to interact only with another entity, use

http://www.solarus-games.org/doc/latest/lua_api_entity.html#lua_api_entity_get_facing_entity

Code (lua) Select

function entity:on_obstacle_reached(movement)
  -- Check if any movement exist
  if movement ~= nil then
    -- Check the entity facing this entity
    if self:get_facing_entity():get_name() == "name_of_your_npc" then
      -- Stop the movement
      movement:stop()

      -- Do whatever you want here (restart a movement or else)
     end
  end
end