Skip to content

Commit

Permalink
Solve #215 Better late init interest support
Browse files Browse the repository at this point in the history
  • Loading branch information
Insality committed Dec 3, 2022
1 parent 4f7dbf4 commit d93823a
Show file tree
Hide file tree
Showing 6 changed files with 605 additions and 5 deletions.
21 changes: 16 additions & 5 deletions druid/system/druid_instance.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ local function process_input(self, action_id, action, components)
end


local function schedule_late_init(self)
if self._late_init_timer_id then
return
end

self._late_init_timer_id = timer.delay(0, false, function()
self._late_init_timer_id = nil
self:late_init()
end)
end


--- Druid class constructor
-- @tparam DruidInstance self
-- @tparam table context Druid context. Usually it is self of script
Expand All @@ -215,10 +227,6 @@ function DruidInstance.initialize(self, context, style)
for i = 1, #base_component.ALL_INTERESTS do
self.components_interest[base_component.ALL_INTERESTS[i]] = {}
end

timer.delay(0, false, function()
self:late_init()
end)
end


Expand All @@ -244,6 +252,9 @@ function DruidInstance.new(self, component, ...)
if instance.init then
instance:init(...)
end
if instance.on_late_init then
schedule_late_init(self)
end

return instance
end
Expand Down Expand Up @@ -315,7 +326,7 @@ function DruidInstance.remove(self, component)
end


--- Druid late update function call after init and before udpate step
--- Druid late update function call after init and before update step
-- @tparam DruidInstance self
function DruidInstance.late_init(self)
local late_init_components = self.components_interest[base_component.ON_LATE_INIT]
Expand Down
Loading

0 comments on commit d93823a

Please sign in to comment.