Jump to content

Recommended Posts

Hello DST modding community!

I recently started actively using some commands that change the prefabs under the cursor. Here is a list of these commands:
 

c_select().AnimState:SetErosionParams(0, -1, -1.0)

c_select().AnimState:SetBrightness(2)

c_select().Transform:SetScale(1,1,1)

c_select().Physics:SetCylinder(0,0)

c_select().AnimState:SetMultColour(0, 0, 0, 1)

 

That's where my problem begins. All these commands work fine and any values (almost) work too. However, after restarting the server or rebooting the server, everything resets: size, color, cylinder, and so on.

I tried to make my own mod that should fix this problem, it runs but prblemu does not solve (I attach it). 

Here begins my request:
I would be very grateful if you could give me commands that work even after rebooting the server or give instructions on how to compose them. Also you can help with the code if you can see any gross errors or flaws. I will accept any help and I will be very grateful.

Thank you all in advance! :)

(P.S. My english not that good so sorry for mistakes)

-- modmain.lua
local function SaveCustomParams(inst)
    if inst and inst:IsValid() then
        if inst.AnimState then
            inst.custom_erosion = {inst.AnimState:GetErosionParams()}
            inst.custom_brightness = inst.AnimState:GetBrightness()
            inst.custom_multcolour = {inst.AnimState:GetMultColour()}
        end
        if inst.Transform then
            inst.custom_scale = {inst.Transform:GetScale()}
        end
        if inst.Physics then
            inst.custom_physics = {inst.Physics:GetRadius(), inst.Physics:GetMass()}
        end
    end
end

local function LoadCustomParams(inst)
    if inst and inst:IsValid() then
        if inst.custom_erosion then
            inst.AnimState:SetErosionParams(unpack(inst.custom_erosion))
        end
        if inst.custom_brightness then
            inst.AnimState:SetBrightness(inst.custom_brightness)
        end
        if inst.custom_multcolour then
            inst.AnimState:SetMultColour(unpack(inst.custom_multcolour))
        end
        if inst.custom_scale then
            inst.Transform:SetScale(unpack(inst.custom_scale))
        end
        if inst.custom_physics then
            inst.Physics:SetCylinder(unpack(inst.custom_physics))
        end
    end
end

local function ApplyCustomParams()
    if TheWorld and TheWorld.components and TheWorld.components.entityspawner then
        local spawner = TheWorld.components.entityspawner
        for _, inst in pairs(spawner:GetAllEntities()) do
            LoadCustomParams(inst)
        end
    end
end

AddPrefabPostInit("world", function(world)
    world:DoTaskInTime(0, ApplyCustomParams)
end)

local function OnEntityRemove(inst)
    if inst and inst:IsValid() then
        SaveCustomParams(inst)
    end
end

AddPrefabPostInit("inspectable", function(inst)
    if inst and inst.components and inst.components.inspectable then
        inst:ListenForEvent("onremove", OnEntityRemove)
    end
end)

Data isn't going to be saved just because you assigned it to a variable attached to "inst". All saving is done through the "inst.OnSave" function (by storing it in the "data" table, the second parameter of that function).

That means you're going to need to overwrite that function with a new function that saves your stuff before calling the old one. This is sufficiently complicated that you'll want to ask in the modding section.

Also, your saving code is only going to run on a prefab called "inspectable", which probably doesn't exist. I assume you want something like

AddComponentPostInit("inspectable", function(self)
    self.inst:ListenForEvent("onremove", OnEntityRemove)
end)

which will run on any prefab with an "inspectable" component.

IDK what "TheWorld.components.entityspawner" is, but it's probably the wrong place to handle loading. "inst.OnLoad" handles that, and you'll need to overwrite this function before it gets called or you'll lose the data. IDK if going through the "inspectable" component post-init gets you there in time, but it's worth a shot.

Edited by Bumber64

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...