hkzorman / advanced_npc

Advanced NPC for Minetest, using mobs_redo API
Other
18 stars 5 forks source link

Add method for Action queue timer #23

Closed BrunoMine closed 6 years ago

BrunoMine commented 6 years ago

This control is required for the API to control the NPC (in on_step callback).

-- Action queue timer
-- Check if actions and timers aren't locked
if self.actions.action_timer_lock == false then
    -- Increment action timer
    self.actions.action_timer = self.actions.action_timer + dtime
    if self.actions.action_timer >= self.actions.action_interval then
        -- Reset action timer
        self.actions.action_timer = 0
        -- Check if NPC is walking
        if self.actions.walking.is_walking == true then
            -- Move NPC to expected position to ensure not getting lost
            local pos = self.actions.walking.target_pos
            self.object:moveto({x=pos.x, y=pos.y, z=pos.z})
        end
        -- Execute action
        self.freeze = npc.execute_action(self)
        -- Check if there are still remaining actions in the queue
        if self.freeze == nil and table.getn(self.actions.queue) > 0 then
            self.freeze = false
        end
    end
end

Could this be simplified for final developers with a simple method? (eg npc.action_queue_timer(luaentity, dtime))

You may need to process other things in the future, so you can use a more comprehensive syntax method (eg npc.on_step(luaentity, dtime))

hkzorman commented 6 years ago

Thanks for this suggestion. Your past issue where you needed to add the action timer made me think about this and in the latest version this has been implemented as npc.step(self, dtime) https://github.com/hkzorman/advanced_npc/blob/5f9afae5d9f110bb8bdecf362da03ed9f14a95bf/npc.lua#L1366

BrunoMine commented 6 years ago

Great!