Solarus-Games English Forum

Solarus => Development => Topic started by: magician on January 04, 2016, 05:21:36 PM

Title: Npc custom entity
Post by: magician on January 04, 2016, 05:21:36 PM
Hello,

I need help, how to change NPC size(16x16pixel) to larger number.
Title: Re: Npc custom size
Post by: GameboyArda on January 04, 2016, 05:26:11 PM
You could use Custom Entity
Like a code like this
Code ( lua) Select
set size 18x18
Title: Re: Npc custom size
Post by: Christopho on January 04, 2016, 05:34:53 PM
There is already an issue for that: https://github.com/christopho/solarus/issues/121

But yes, a custom entity can be used.
Title: Re: Npc custom size
Post by: magician on January 04, 2016, 05:37:30 PM
Quote from: GameboyArda on January 04, 2016, 05:26:11 PM
You could use Custom Entity
Like a code like this
Code ( lua) Select
set size 18x18

thanks,i already testing in custom entity and how to make obstacle? if i using this
Code (lua) Select
set_traversable_by(false) it still can bypass it.

edit:i found solution http://forum.solarus-games.org/index.php?topic=433.0 .
:D
Title: Re: [solved]Npc custom size
Post by: MetalZelda on January 04, 2016, 11:15:44 PM
You can custom entity the NPC, it would be the same thing, but you would be allowed to change various parameters suh as his size, bounding box, origin, etc. The best thing about it is that you don't have to find through your game database about the map where the NPC is to change a variant, everything can be done in a single file in /entities. And it doesn't impact any performance.

Code (lua) Select

local entity = ...
local game = entity:get_game()

--Shared NPC. The dialog pointer = the name of the entity in the map (in the editor).
--If you want all NPC to have a movement then create the movement in on_created()

function entity:on_created()
-- here, self is the entity
self:set_size(24,24) -- only multiple of 8 are allowed
self:set_traversable_by("hero", false)
end

local  hero_facing_npc = false
local action_command_speak = false

-- Hud notification
entity:add_collision_test("facing", function(entity, other)
if other:get_type() == "hero" then
   hero_facing_npc = true
  if hero_facing_npc then
    game:set_custom_command_effect("action", "open")
action_command_npc = true
   else
    game:set_custom_command_effect("action", nil)
   end
end
end)

function entity:on_interaction()
self:set_direction(game:get_hero():get_direction() / 2)
game:start_dialog(self:get_name())
end

function entity:on_update()
  if action_command_speak and not hero_facing_npc then
    game:set_custom_command_effect("action", nil)
    action_command_speak = false
  end
   hero_facing_npc= false
end


Not tested, but it act like the build in NPC class. The only thing is that you need to code it's behaviour if you wanna make your NPC more smart.
Remember that size of objects need to be a multiple of 8.
Title: Npc custom entity
Post by: magician on January 05, 2016, 09:53:48 PM
Quote from: Christopho on January 04, 2016, 05:34:53 PM
There is already an issue for that: https://github.com/christopho/solarus/issues/121

But yes, a custom entity can be used.
ahh, i see...
Quote from: MetalZelda on January 04, 2016, 11:15:44 PM
You can custom entity the NPC, it would be the same thing, but you would be allowed to change various parameters suh as his size, bounding box, origin, etc. The best thing about it is that you don't have to find through your game database about the map where the NPC is to change a variant, everything can be done in a single file in /entities. And it doesn't impact any performance.

Code (lua) Select

local entity = ...
local game = entity:get_game()

--Shared NPC. The dialog pointer = the name of the entity in the map (in the editor).
--If you want all NPC to have a movement then create the movement in on_created()

function entity:on_created()
-- here, self is the entity
self:set_size(24,24) -- only multiple of 8 are allowed
self:set_traversable_by("hero", false)
end

local  hero_facing_npc = false
local action_command_speak = false

-- Hud notification
entity:add_collision_test("facing", function(entity, other)
if other:get_type() == "hero" then
   hero_facing_npc = true
  if hero_facing_npc then
    game:set_custom_command_effect("action", "open")
action_command_npc = true
   else
    game:set_custom_command_effect("action", nil)
   end
end
end)

function entity:on_interaction()
self:set_direction(game:get_hero():get_direction() / 2)
game:start_dialog(self:get_name())
end

function entity:on_update()
  if action_command_speak and not hero_facing_npc then
    game:set_custom_command_effect("action", nil)
    action_command_speak = false
  end
   hero_facing_npc= false
end


Not tested, but it act like the build in NPC class. The only thing is that you need to code it's behaviour if you wanna make your NPC more smart.
Remember that size of objects need to be a multiple of 8.

thanks for the feature but i got error when implement this code "Error: In collision callback: [string "entities/cactus.lua"]:27: attempt to call method 'set_custom_command_effect' (a nil value)"

local cactus = ...
local sprite = cactus:get_sprite()
local game = cactus:get_game()

function cactus:on_created()

  cactus:set_size(32, 32)
  cactus:set_origin(16, 16)
  cactus:set_can_traverse("hero", false)
  cactus:set_traversable_by("hero",false)

  local movement = sol.movement.create("random_path")
  movement:start(cactus)
  sprite:set_animation("walking")

end

local  hero_facing_npc = false
local action_command_speak = false

-- Hud notification
cactus:add_collision_test("facing", function(cactus, other)
if other:get_type() == "hero" then
   hero_facing_npc = true
  if hero_facing_npc then
    game:set_custom_command_effect("action", "open")
        action_command_npc = true
   else
    game:set_custom_command_effect("action", nil)
   end
end
end)

function cactus:on_interaction()
self:set_direction(game:get_hero():get_direction() / 2)
game:start_dialog("welcome_sign")
end

function cactus:on_update()
  if action_command_speak and not hero_facing_npc then
    game:set_custom_command_effect("action", nil)
    action_command_speak = false
  end
   hero_facing_npc= false
end

Title: Re: Npc custom entity
Post by: MetalZelda on January 05, 2016, 10:36:31 PM
The set_custom_command_effect is a hud related code, if you don't use the one provided with MoS then you can delete it.
Title: Re: Npc custom entity
Post by: Christopho on January 06, 2016, 10:05:13 AM
Yes, game:get/set_custom_command_effect() are not in the Solarus API, these functions are added by ZSDX scripts. Don't call them if you don't define them.