Jump to content

Recommended Posts

Hi again,

So i have a custom structure and i would like it to disappears a few days after i build it. For example something like 6 days of lifetime.

Is it possible ?
If my structure is a container, i guess i could use this kind of code to drop all it containt when disappearing ?


	inst.components.lootdropper:DropLoot()
	inst.components.container:DropEverything()
	inst.SoundEmitter:PlaySound("dontstarve/common/destroy_metal")
	inst:Remove()

Thanks for any help.

36 minutes ago, Lumina said:

Thanks a lot, it's very helpful. But isn't "DoTaskInTime" for very short duration ?

Right now i am trying something with "DoPeriodicTask", is it better ? Worse ?

Based on abigail_flower prefab and untested.

local TUNING_DURATION = 30
inst.DoDestruction = function(inst)
    inst.components.lootdropper:DropLoot()
    inst.components.container:DropEverything()
    inst.SoundEmitter:PlaySound("dontstarve/common/destroy_metal")
    inst:Remove()
end
inst.StartDestruction = function(inst, timeleft)
    if inst.destroytimer
    then
        inst.destroytimer:Cancel()
    end
    inst.destroytimer = inst:DoTaskInTime(timeleft, DoDestruction)
    inst.timestart = GetTime() + timeleft - TUNING_DURATION
end
inst.OnSave = function(inst, data)
    if inst.timestart
    then
        local timeconsumed = GetTime() - inst.timestart
        if timeconsumed>0
        then
            data.timeconsumed = timeconsumed
        end
    end
end
inst.OnLoad = function(inst, data)
    if data and data.timeconsumed
    then
        local timeleft = math.max(0, TUNING_DURATION - data.timeconsumed)
        inst:StartDestruction(timeleft)
    end
end

__After the TheWorld.ismastersim check__
    inst:StartDestruction(TUNING_DURATION)

 

Thanks :) I used another way but right now i don't know what is the better resource wise. I'm using something similar to the test to regrow mushroom :

 


local function onsave(inst, data)
    data.timecheck = inst.timecheck
end

local function onload(inst, data)
    if data.timecheck or inst.timecheck then
        inst.timecheck = data.timecheck or inst.timecheck
    end
end

local function checkdestroy(inst)
    -- print("Time Check")
        inst.timecheck = inst.timecheck - 1
        if inst.timecheck <= 0 then
		inst.components.lootdropper:DropLoot()
		inst.components.container:DropEverything()
		inst.SoundEmitter:PlaySound("dontstarve/common/destroy_metal")
		inst:Remove()        end
end

    inst.timecheck = 44
	
    inst:DoPeriodicTask(TUNING.SEG_TIME*2, checkdestroy, TUNING.SEG_TIME + math.random()*TUNING.SEG_TIME)        

(and maybe some others code i forgot). It seems to work fine but i don't know if it's a good way to manage it.

Anyway thanks a lot, both of you :)

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...