Jump to content

Recommended Posts

Hi everyone, I know the title sounds hilarious, but one of the perks that my character has is that her hat can produce pudding, and i hope to make this happen every game day as long as she wears or has the hat in her inventory, and also if there's no existed pudding in her inventory (for I'm thinking to make the pudding non-stackable and non-perishable). However this totally go beyond my ability to code, here's what i came up with

inst:DoPeriodicTask(16*TUNING.SEG_TIME, function(inst)
	    if inst.components.inventory:HasItemWithTag("custom_hat") = 0 or inst.components.inventory:HasItemWithTag("pudding") = 1 then
		        return
	    end
		
		if inst.components.inventory:HasItemWithTag("custom_hat") = 1 then
		inst.components.hat.prize = "kanna_pudding"
		end
	end
	

And if I put this code to my character's script, do I also need to define the two custom items (the hat and the pudding) in prefab list? or is there a better way to link this three together? currently I have these three all in their own prefab file.

thank you!

15 hours ago, _zwb said:

I'd say it's better to make it a periodic task for the custom hat, instead of custom character. Start task whenever it is equipped, stop task when unequipped.

Hi! thanks for the suggestion! that sound really nice, then i have a question, do you think it's possible to use a harvestable component in this case? does this component compatible with periodic task? What I'm thinking is to make the hat do a "pudding generation" task every game day, and harvestable only when it's equipped? not sure whether this will work... But, thank you! 

local function ondaychange(inst)
    if TheWorld.state.isday then
        if inst.components.harvestable then
            inst.components.harvestable:Grow()
		end
	end
end
		
local function fn(Sim)
    inst:AddComponent("harvestable")
	inst.components.harvestable:Setup("kanna_pudding", 1, nil, onharvest, nil)
	inst:ListenForEvent("ondaychange", ondaychange)
	
	return inst
end

So I kind of twisted the code for beebox and night vision together.. but I have no idea whether this can work or not x.x I think I prefer harvestable component, cuz it seems to me that you'll have to click on and harvest pudding or it will stop "growing"?

local function ondaychange(inst)
    if TheWorld.state.enterlight then
        if inst.components.harvestable then
            inst.components.harvestable:Grow()
		end
	end
end
		
local function fn(Sim)
    inst:AddComponent("harvestable")
	inst.components.harvestable:Setup("kanna_pudding", 1, nil, onharvest, nil)
	inst:ListenForEvent("daychange", ondaychange)
	
	return inst
end

inst.WatchWorldState("enterlight", ondaychange)

maybe this way is better...? I noticed there's start and stop grow components but not sure if they are needed in this case...

And I'm also wondering, cuz beebox has different texture according to the amount of honey it stores, however when I looked into its lua file I didn't see any particular code in shifting the animation.. so it's already made within the scml file...?

--as for item that can be equipped,it'd be easy to add useableitem components to reach some fn
local function setcanuse(inst)
    if inst.components.finiteuses
    and inst.components.finiteuses.current <= 0
    then
        if inst.components.useableitem then
            inst:RemoveComponent("useableitem")
        end
    else
        if not inst.components.useableitem then
            inst:AddComponent("useableitem")
        end
    end
end

local function addpudding(inst)
    if inst.components.finiteuses:GetPercent() < 1 then
        inst.components.finiteuses:SetUses(inst.components.finiteuses.total)
    end
    setcanuse(inst)
end

local function onuse(inst)
    local owner = inst.components.inventoryitem.owner
    if inst.components.finiteuses
    and not inst.components.finiteuses == 0
    then
        local pudding = SpawnPrefab("kanna_pudding")
        pudding.Transform:SetPosition(inst.Transform:GetWorldPosition())
        if owner.components.inventory then
            owner.components.inventory:GiveItem(pudding)
        end
    end
    return false
end


local function fn()
    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetMaxUses(1)
    inst.components.finiteuses:SetUses(1)
    inst.components.finiteuses:SetOnFinished(setcanuse)--or you can do nothing here
    
    inst:AddComponent("useableitem")
    inst.components.useableitem:SetOnUseFn(onuse)
    
    inst:WatchWorldState("startcaveday", addpudding)--add finiteuses every morning start
end

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