Jump to content

How do prefab post-inits work again?


Recommended Posts

I've had a mod I've been working on on and off for a while now, and there's one section of code I know will need to be a post init to avoid compatibility issues in the future.

 

This is the segment of code that will need to be a post-init, I just don't remember how it is set up (modmain.lua I think, but I don't really know how):

 

size adjuster .txt

Link to comment
Share on other sites

As the file itself says, it's written to be added to the fn() function of a custom prefab. You can add this second part of the file to existing prefabs, by doing this:

local function applyscale(inst, scale)
	inst.DynamicShadow:SetSize(2.5 * scale, 1.5 * scale)
end

AddPrefabPostInit("prefabname", function (inst)
	inst:AddComponent("scaler")
    inst.components.scaler.OnApplyScale = applyscale  

	local min_scale = 0.3
    local max_scale = 1.00

    local scaleRange = max_scale - min_scale
    local start_scale = min_scale + math.random() * scaleRange

    inst.components.scaler:SetScale(start_scale)
    local dt = 60 + math.random()*10
    inst.growtask = inst:DoPeriodicTask(dt, grow, nil, dt)
end)

The problem with this, is that the first part of the code says it needs to be added BEFORE any brain is added to the prefab (so I'm guessing we're talking about monsters and creatures here). I'm not entirely sure why that is, though, so I have no idea where to put this:

local function grow(inst, dt)
    if inst.components.scaler.scale < 0.5 then
        local new_scale = math.min(inst.components.scaler.scale + TUNING.ROCKY_GROW_RATE*dt, 0.75)
        inst.components.scaler:SetScale(new_scale)
    else
        if inst.growtask then
            inst.growtask:Cancel()
            inst.growtask = nil
        end
    end
end

It's hard to know, without knowing what kind of mod you're making, and without seeing your own code.

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...