Jump to content

Please check the existence of an add_component_if_missing component before it is added in loading.


Recommended Posts

Otherwise, mods cannot be disabled if they use this attribute.

 

-- entityscript.lua  1781~1802
function EntityScript:SetPersistData(data, newents)
    if self.OnPreLoad ~= nil then
        self:OnPreLoad(data, newents)
    end

    if data ~= nil then
        for k, v in pairs(data) do
            local cmp = self.components[k]
            if cmp == nil and type(v) == "table" and v.add_component_if_missing then
                self:AddComponent(k)
                cmp = self.components[k]
            end
            if cmp ~= nil and cmp.OnLoad ~= nil then
                cmp:OnLoad(v, newents)
            end
        end
    end

    if self.OnLoad ~= nil then
        self:OnLoad(data, newents)
    end
end

-- for example
function EntityScript:SetPersistData(data, newents)
    if self.OnPreLoad ~= nil then
        self:OnPreLoad(data, newents)
    end

    if data ~= nil then
        for k, v in pairs(data) do
            local cmp = self.components[k]
            if cmp == nil and type(v) == "table" and v.add_component_if_missing then
                if Components[k] or type(loadfile("scripts/components/"..k)) == "function" then
                    -- skip if the component do not exist
                    self:AddComponent(k)
                    cmp = self.components[k]
                end
            end
            if cmp ~= nil and cmp.OnLoad ~= nil then
                cmp:OnLoad(v, newents)
            end
        end
    end

    if self.OnLoad ~= nil then
        self:OnLoad(data, newents)
    end
end

 

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...