Jump to content

Recommended Posts

The contents may be strange using a translator.

 

 

I'm creating a tool to use as a dedicated tool for a character.
But this is the first time I've ever made a tool that's not a weapon.
Playing the fanflute restores his mental strength of 20 and 40 for the player on the screen, and I want to have four freshwater fish and one eel in the inventory.
I don't know where to get the code to put the item in the inventory or restore the mentality.

 

 

Edited by macarong

You can try this code:

-- in your custom item's file

local function onheard(inst, musician, instrument)
    if inst.components.farmplanttendable ~= nil then -- include these 2 lines if you want this item be able to tend farm plants
	   inst.components.farmplanttendable:TendTo(musician)
    end

    if inst.components.sanity then
        inst.components.sanity:DoDelta(inst == musician and 20 or 40)
    end
  
    if inst == musician then
        if inst.components.inventory then
            for i = 1, 4 do 
                local fish = SpawnPrefab("pondfish")
                inst.components.inventory:GiveItem(fish)
            end
            inst.components.inventory:GiveItem(SpawnPrefab("eel"))
        end
    end
end

local function fn()
    local inst = CreateEntity()

    -- snip --

    --tool (from tool component) added to pristine state for optimization
    inst:AddTag("tool")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")
    inst:AddComponent("instrument")
    inst.components.instrument.range = 15 -- replace this with the range of your item, 1 unit distance is the same as stone wall's width
    inst.components.instrument:SetOnHeardFn(onheard)

    inst:AddComponent("tool")
    inst.components.tool:SetAction(ACTIONS.PLAY)

    inst:AddComponent("finiteuses") -- add those lines if your item has finite uses
    inst.components.finiteuses:SetMaxUses(5) -- your custom item's durability
    inst.components.finiteuses:SetUses(5)
    inst.components.finiteuses:SetOnFinished(inst.Remove)
    inst.components.finiteuses:SetConsumption(ACTIONS.PLAY, 1)

    return inst
end

 

On 8/2/2023 at 1:14 AM, _zwb said:

You can try this code:

-- in your custom item's file

local function onheard(inst, musician, instrument)
    if inst.components.farmplanttendable ~= nil then -- include these 2 lines if you want this item be able to tend farm plants
	   inst.components.farmplanttendable:TendTo(musician)
    end

    if inst.components.sanity then
        inst.components.sanity:DoDelta(inst == musician and 20 or 40)
    end
  
    if inst == musician then
        if inst.components.inventory then
            for i = 1, 4 do 
                local fish = SpawnPrefab("pondfish")
                inst.components.inventory:GiveItem(fish)
            end
            inst.components.inventory:GiveItem(SpawnPrefab("eel"))
        end
    end
end

local function fn()
    local inst = CreateEntity()

    -- snip --

    --tool (from tool component) added to pristine state for optimization
    inst:AddTag("tool")

    inst.entity:SetPristine()

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")
    inst:AddComponent("instrument")
    inst.components.instrument.range = 15 -- replace this with the range of your item, 1 unit distance is the same as stone wall's width
    inst.components.instrument:SetOnHeardFn(onheard)

    inst:AddComponent("tool")
    inst.components.tool:SetAction(ACTIONS.PLAY)

    inst:AddComponent("finiteuses") -- add those lines if your item has finite uses
    inst.components.finiteuses:SetMaxUses(5) -- your custom item's durability
    inst.components.finiteuses:SetUses(5)
    inst.components.finiteuses:SetOnFinished(inst.Remove)
    inst.components.finiteuses:SetConsumption(ACTIONS.PLAY, 1)

    return inst
end

 

 

There was a little wandering, but it succeeded. Thank you! You are my lifesaver.

Edited by macarong

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