Author Topic: Stopping npcs  (Read 228 times)

Porksteak

  • Newbie
  • *
  • Posts: 8
    • View Profile
Stopping npcs
« on: May 13, 2017, 04:35:53 pm »
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.

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: Stopping npcs
« Reply #1 on: May 13, 2017, 05:02:42 pm »
Quote
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.

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


Quote
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?

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
« Last Edit: May 14, 2017, 01:49:47 am by Zefk »

MetalZelda

  • Hero Member
  • *****
  • Posts: 507
    • View Profile
Re: Stopping npcs
« Reply #2 on: May 13, 2017, 10:27:04 pm »
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
  1. function entity:on_obstacle_reached(movement)
  2.   -- Check if any movement exist
  3.   if movement ~= nil then
  4.     -- Stop the movement
  5.     movement:stop()
  6.  
  7.     -- Do whatever you want here (restart a movement or else)
  8.   end
  9. end
  10.  
  11.  

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
  1. function entity:on_obstacle_reached(movement)
  2.   -- Check if any movement exist
  3.   if movement ~= nil then
  4.     -- Check the entity facing this entity
  5.     if self:get_facing_entity():get_name() == "name_of_your_npc" then
  6.       -- Stop the movement
  7.       movement:stop()
  8.  
  9.       -- Do whatever you want here (restart a movement or else)
  10.      end
  11.   end
  12. end
  13.  
  14.  
« Last Edit: May 13, 2017, 10:31:22 pm by MetalZelda »