Jump to content

Recommended Posts

I made a custom prefab based on grave. When this prefab is dug, i want it to "refill" after some time. I made a function for this, but i encounter a problem : when the even trigger, the function seems to work strangely. The anim is ok, i can try to dig the prefab, but i obtain no result. If i quit the game and log again, i can dig the prefab normally.

Here is the code i'm using :

local assets =
{
    Asset("ANIM", "anim/gravestones.zip"),
}

local prefabs =
{
    "ghost",
    "amulet",
    "redgem",
    "gears",
    "bluegem",
    "nightmarefuel",
}

for k = 1, NUM_TRINKETS do
    table.insert(prefabs, "trinket_"..tostring(k))
end

local LOOTS =
{
    nightmarefuel = 1,
    amulet = 1,
    gears = 1,
    redgem = 5,
    bluegem = 5,
}

local function OnNewDayFn(inst, data)
	print("function counter")
    if inst.components.workable == nil then
        inst.counter = inst.counter - 1
        if inst.counter <= 0 then
			inst.AnimState:PlayAnimation("gravedirt")
			inst:AddComponent("workable")
			inst.components.workable:SetWorkAction(ACTIONS.DIG)
			inst.components.workable:SetWorkLeft(1)
		end
    end        
end

local function onfinishcallback(inst, worker)
    inst.AnimState:PlayAnimation("dug")
    inst:RemoveComponent("workable")
	
	inst.counter = 1

   local num = math.random(1,20)
					local buried = {"log","twigs","cutgrass","rocks","flint","mudroot"}
					local commontreasure = {"redgem","bluegem","marble","goldnugget","papyrus"}
					local treasure = {"purplegem","onemanband","whistle","nightmarefuel","umbrella","commontreasuremap"}
					local raretreasure = {"gears","yellowgem","orangegem","greengem","armor_sanity","nightsword","livinglog","raretreasuremap"}
					local trinket = PickRandomTrinket() 


    if worker ~= nil then
			if worker:HasTag("treasurehunter") and worker:HasTag("luckyfinder") then
				if num <= 4 then
					inst.components.lootdropper:SpawnLootPrefab(commontreasure[math.random(1,#commontreasure)])
					print("extra loot common both tag") -- so you know if the loot isn't spawning for some reason
				elseif num > 4 and num <= 5 then
					inst.components.lootdropper:SpawnLootPrefab(trinket)
				elseif num > 5 and num <= 6 then
					inst.components.lootdropper:SpawnLootPrefab(treasure[math.random(1,#treasure)])
				elseif num > 6 and num <= 8 then
					inst.components.lootdropper:SpawnLootPrefab(raretreasure[math.random(1,#raretreasure)])
				else
				inst.components.lootdropper:SpawnLootPrefab(buried[math.random(1,#buried)])
				end
            elseif worker:HasTag("treasurehunter") then
				if num <= 1 then
					inst.components.lootdropper:SpawnLootPrefab(commontreasure[math.random(1,#commontreasure)])
				elseif num > 1 and num <= 2 then
					inst.components.lootdropper:SpawnLootPrefab(trinket)
				elseif num > 2 and num <= 3 then
					inst.components.lootdropper:SpawnLootPrefab(treasure[math.random(1,#treasure)])
				elseif num > 3 and num <= 4 then
					inst.components.lootdropper:SpawnLootPrefab(raretreasure[math.random(1,#raretreasure)])
				else
				inst.components.lootdropper:SpawnLootPrefab(buried[math.random(1,#buried)])
				end
			elseif worker:HasTag("luckyfinder") then
				if num <= 1 then
					inst.components.lootdropper:SpawnLootPrefab(commontreasure[math.random(1,#commontreasure)])
				elseif num > 1 and num <= 2 then
					inst.components.lootdropper:SpawnLootPrefab(trinket)
				elseif num > 2 and num <= 3 then
					inst.components.lootdropper:SpawnLootPrefab(treasure[math.random(1,#treasure)])
				elseif num > 3 and num <= 4 then
					inst.components.lootdropper:SpawnLootPrefab(raretreasure[math.random(1,#raretreasure)])
				else
				inst.components.lootdropper:SpawnLootPrefab(buried[math.random(1,#buried)])
				end
            else
            end
	end
end

local function GetStatus(inst)
    if not inst.components.workable then
        return "DUG"
    end
end

local function OnSave(inst, data)
    if inst.components.workable == nil then
        data.dug = true
    end
end

local function OnLoad(inst, data)
    if data ~= nil and data.dug or inst.components.workable == nil then
        inst:RemoveComponent("workable")
        inst.AnimState:PlayAnimation("dug")
    end
end


local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()

    inst.AnimState:SetBank("gravestone")
    inst.AnimState:SetBuild("gravestones")
    inst.AnimState:PlayAnimation("gravedirt")


    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

	inst.counter = 0
	
    inst:AddComponent("inspectable")
    inst.components.inspectable.getstatus = GetStatus

    inst:AddComponent("workable")
    inst.components.workable:SetWorkAction(ACTIONS.DIG)
    inst.components.workable:SetWorkLeft(1)
    inst:AddComponent("lootdropper")

    inst.components.workable:SetOnFinishCallback(onfinishcallback)


	inst:WatchWorldState("isday", OnNewDayFn)

    inst.OnSave = OnSave
    inst.OnLoad = OnLoad

    return inst
end

return Prefab("mudhole", fn, assets, prefabs)

 

Here is the function :


local function OnNewDayFn(inst, data)
	print("function counter")
    if inst.components.workable == nil then
        inst.counter = inst.counter - 1
        if inst.counter <= 0 then
			inst.AnimState:PlayAnimation("gravedirt")
			inst:AddComponent("workable")
			inst.components.workable:SetWorkAction(ACTIONS.DIG)
			inst.components.workable:SetWorkLeft(1)
		end
    end        
end

If i try to add this line :

    inst.components.workable:SetOnFinishCallback(onfinishcallback)

The game crash (variable "onfinishcallback" is not declared).

I probably need something to properly "refresh" the state of the prefab, but i don't know what. Any help will be welcome :)

 

Note : at the moment the counter is 1 for testing purpose; Some parts of the code could be a little messy, sorry for that, i didn't find a best way to manage it. Let me know if i should attach the mod : since it's a part of a work in progress i can't attach the entire mod, but i could make a small mod with only this prefab if it's really needed.

Thanks for your help.

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