Solve #215 Better late init interest support

This commit is contained in:
Insality
2022-12-03 16:16:55 +02:00
parent 4f7dbf49d3
commit d93823ae6a
6 changed files with 605 additions and 5 deletions

View File

@@ -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
@@ -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
@@ -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
@@ -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]