Jump to content

Recommended Posts

image.png.e35ee79729355c7e3d6dfb53f8b0f9c1.png

Hi! So, I wanted to do something similar with the shadow tools with a fx animation and sanity drain. I did a little digging in dst’s prefabs but I could not find a component for on item use.

Basically I want to spawn a FX effect on the object being chopped/mined and drain sanity when the item is used for each use.

I have basic knowledge of Lua. with functions and variables, still trying to learn returns and such

Any help would be really awesome! 
 

Tools don't have a dedicated on hit function in their component to make things easier like weapons do, but there is a push event when a character does a "work" action, which includes mining, chopping, and digging. You can call a listenforevent for this event in the onequip function of the tools.

local function OnDoingWork(owner, data)
    -- From Wolfgang's prefab
    if data ~= nil and data.target ~= nil then
		local workable = data.target.components.workable
		if workable ~= nil then
			local work_action = workable:GetWorkAction()
			if work_action ~= nil then
                -- Spawn FX and lower sanity code would go here
			end
		end
    end
end

local function onequip(inst, owner)
	-- The usual onequip code here too
    owner:ListenForEvent("working", OnDoingWork) -- "owner" instead of "inst" because the player's instance is what the event is being pushed to
end

local function onunequip(inst, owner)
	-- The usual onunequip code here too
    owner:RemoveEventCallback("working", OnDoingWork) -- This stops listening for the event when the item is unequipped.
end

 

Okay, So I put everything together; not sure if I did the sanity drain correctly. As soon as I craft the Axe my game crashes and throws this error

image.png.8a797d2be54d577e1bbe073c693ab3a9.png

 

My function looks like this

 

local function OnDoingWork(owner, data)
    -- From Wolfgang's prefab
    if data ~= nil and data.target ~= nil then
        local workable = data.target.components.workable
        if workable ~= nil then
            local work_action = workable:GetWorkAction()
            if work_action ~= nil then
 
                local fx_prefab = "shadow_despawn" and
 
                    owner.components.sanity:DoDelta(-.5 * TUNING.BATBAT_DRAIN)
 
                    if fx_prefab ~= nil then
                        local fx = SpawnPrefab(fx_prefab)  
                        local x, y, z = target.Transform:GetWorldPosition()
                        local radius = target:GetPhysicsRadius(.5)
                        local angle = (inst.Transform:GetRotation() - 90) * DEGREES
                        fx.Transform:SetPosition(x + math.sin(angle) * radius, 0, z + math.cos(angle) * radius)
                    end
                -- Spawn FX and lower sanity code would go here
            end
        end
    end
end

My onequpit and onunequpit functions look like this

local function OnEquip(inst, owner)
    -- This will override symbol "swap_body" of the equipping player with your custom build symbol.
    -- Here's what this function is overriding:
    -- owner.AnimState:OverrideSymbol(Player's_symbol, Your_build(*.zip_filename), Symbol_from_your_build(name_of_subfolder_with_art)
    owner.AnimState:OverrideSymbol("swap_object", "woven_axe", "swap_object")
 
    owner:ListenForEvent("working", OnDoingWork) -- "owner" instead of "inst" because the player's instance is what the event is being pushed to
   
    -- Players have 2 left arms - one of them is hidden when we are not holding an item and vice versa.
    -- Since we want to show an item on equip - hide ARM_normal and show ARM_carry.
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
end
 
local function OnUnequip(inst, owner)
    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
   
    owner:RemoveEventCallback("working", OnDoingWork) -- This stops listening for the event when the item is unequipped.
end

 

Edited by Doodle Monster
17 hours ago, Doodle Monster said:

Okay, So I put everything together; not sure if I did the sanity drain correctly. As soon as I craft the Axe my game crashes and throws this error

 

 

My function looks like this

 

local function OnDoingWork(owner, data)
    -- From Wolfgang's prefab
    if data ~= nil and data.target ~= nil then
        local workable = data.target.components.workable
        if workable ~= nil then
            local work_action = workable:GetWorkAction()
            if work_action ~= nil then
 
                local fx_prefab = "shadow_despawn" and
 
                    owner.components.sanity:DoDelta(-.5 * TUNING.BATBAT_DRAIN)
 
                    if fx_prefab ~= nil then
                        local fx = SpawnPrefab(fx_prefab)  
                        local x, y, z = target.Transform:GetWorldPosition()
                        local radius = target:GetPhysicsRadius(.5)
                        local angle = (inst.Transform:GetRotation() - 90) * DEGREES
                        fx.Transform:SetPosition(x + math.sin(angle) * radius, 0, z + math.cos(angle) * radius)
                    end
                -- Spawn FX and lower sanity code would go here
            end
        end
    end
end

Is the OnDoingWork function above or below the onequip and onunequip functions?

local functions should generally be above anything that is calling them

Edited by Merkyrrie
4 hours ago, Doodle Monster said:

@Merkyrrie I assume I have to specify the location of the FX somehow? I don't think the code for the attack will work

The same code should work, just use 'data.target' in place of 'target' for example 'target.Transform:GetWorldPosition()' becoming data.target.Transform:GetWorldPosition()

Data.target in this case is the instance of the rock/tree being worked.

I pulled it up ingame and gave it a test! Sanity drain is definitely working but the FX isn't showing up after I added the 'data.target'

I'm not sure If I need the math at the end that transforms the FX either here's my function and prefab file.

local function OnDoingWork(owner, data)
    -- From Wolfgang's prefab
    if data ~= nil and data.target ~= nil then
        local workable = data.target.components.workable
        if workable ~= nil then
            local work_action = workable:GetWorkAction()
            if work_action ~= nil then
 
                local fx_prefab = "shadow_despawn" and
 
                    owner.components.sanity:DoDelta(-.5 * TUNING.BATBAT_DRAIN)
 
                    if fx_prefab ~= nil then
                        local fx = SpawnPrefab(fx_prefab)  
                        local x, y, z = data.target.Transform:GetWorldPosition()
                        local radius = data.target:GetPhysicsRadius(.5)
                        local angle = (inst.Transform:GetRotation() - 90) * DEGREES
                        fx.Transform:SetPosition(x + math.sin(angle) * radius, 0, z + math.cos(angle) * radius)
                    end
                -- Spawn FX and lower sanity code would go here
            end
        end
    end
end

woven_axe.lua

1 hour ago, Baguettes said:

Put fx.Transform:SetPosition(x, y, z) under your local x, y, z, that may work.

Yeah I'm not sure if the math is actually necessary in this instance or not, however,

 local fx_prefab = "shadow_despawn" and

Remove the "and" here as its causing it to actually make fx_prefab nil here, as well as,

local angle = (inst.Transform:GetRotation() - 90) * DEGREES

"inst" here should be "owner" instead.

7 minutes ago, Baguettes said:

All prefabs from SpawnPrefab defaults to 0, 0, 0 if you don't specify where it should go.

This is correct, the Transform:SetPosition is necessary. I just meant the math applied to the workable's x,y,z in this SetPosition might not be necessary

  • Like 1

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