Jump to content

Recommended Posts

Hello guys, i searched on klei forums, on steam forums, on google, on youtube, but i cant find a way to make this mod work.
Im new to lua and to dont starve modding, still trying to understand so things.

My idea was to create a combat focus character in dont starve together, with a dodge skill, sanity gain on kill and an attack triggered by every 3r attack for now.

SummonHands
In my hunt to find already existing prefabs in the game so i dont have to make everything from 0 and yes im talking about animation xc. I found the shadowhand prefab, the one that comes at night to steal the fire. I thing my code doesnt work so well, maybe that the reason it didnt work. The hands spawned, went on a very short direction and stoped, didnt end the animation, didnt deal dmg.

Spoiler
-- Summon two shadow hands that move toward the target
local function summonHands(inst, target)
	for i = 1, 2 do
			local hand = _G.SpawnPrefab("shadowhand") -- Use _G to access SpawnPrefab
			if hand and target then
					-- Position the hand near the character
					local x, y, z = inst.Transform:GetWorldPosition()
					hand.Transform:SetPosition(x + math.random(-1, 1), y, z + math.random(-1, 1))

					-- Speed up the hand's animation
					-- hand.AnimState:SetPlayRate(1.5)              -- Increase animation speed

					-- Move hand towards the target and trigger attack animation on contact
					hand:DoTaskInTime(0, function()
							hand.components.locomotor:GoToPoint(target:GetPosition())
							
							hand:ListenForEvent("onattackother", function(_, data)
									if data.target and data.target.components.health then
											-- Apply damage to the target
											data.target.components.health:DoDelta(-HAND_DAMAGE)
									end
									
									-- Play the fire-snuffing animation on contact
									-- if hand.AnimState:HasAnimation("snuff") then
									-- 		hand.AnimState:PlayAnimation("snuff")  -- Ensure the animation exists
									-- end
									
									-- Return hand to the character after attack
									hand:DoTaskInTime(0.5, function()
											hand.Transform:SetPosition(inst.Transform:GetWorldPosition()) -- Use Transform instead of locomotor
											hand:Remove()  -- Remove the hand once it returns
									end)
							end)
					end)
			end
	end
end

-- Handle attacking, including counting attacks and triggering a special attack
local function onAttack(inst, data)
    if data.target and data.target:HasTag("monster") then
        inst.components.sanity:DoDelta(SANITY_GAIN_PER_MONSTER_KILL) -- Gain sanity on monster kill
    end

    inst.attackCounter = (inst.attackCounter or 0) + 1 -- Increment attack counter

    if inst.attackCounter >= ATTACK_COUNTER_LIMIT then
        inst.attackCounter = 0 -- Reset counter after special attack

        -- Trigger the special attack with hands
        if data.target and data.target.components.health and not data.target.components.health:IsDead() then
            summonHands(inst, data.target) -- Summon hands on the third hit
        end
    end
end

 

After i found that could use charlie's attack called grue in the game, but i couldnt get it to do dmg at day time or find the animation bank or name to edit. I only found sound names linked to those names when i search the files.
 

Dodge
For the dodge skill, i have no clue. It doesnt work at all and i cant comprehend why. Listen for a keyboard key didnt work, i coulnt get even that to work...
I tried different way to get it to work and it just end up being more complicated and difficult for me to understand what i was doing.

All that said, i hope someone with far more knowledge than me can help me with this mod, it would be amazing.
Thank you

Character.lua

Spoiler
local MakePlayerCharacter = require "prefabs/player_common"

local assets = {
		Asset("SCRIPT", "scripts/prefabs/player_common.lua"),
}

local prefabs = {}

-- Character stats
local STARTING_SANITY = 100
local STARTING_HEALTH = 150
local STARTING_HUNGER = 150

-- Special ability settings
local ATTACK_COUNTER_LIMIT = 3
local HAND_DAMAGE = 25
local SANITY_GAIN_PER_MONSTER_KILL = 10

-- local CONTROL_ACTION = "L"

-- Summon two shadow hands that move toward the target
local function summonHands(inst, target)
	for i = 1, 2 do
			local hand = _G.SpawnPrefab("shadowhand") -- Use _G to access SpawnPrefab
			if hand and target then
					-- Position the hand near the character
					local x, y, z = inst.Transform:GetWorldPosition()
					hand.Transform:SetPosition(x + math.random(-1, 1), y, z + math.random(-1, 1))

					-- Speed up the hand's animation
					-- hand.AnimState:SetPlayRate(1.5)              -- Increase animation speed

					-- Move hand towards the target and trigger attack animation on contact
					hand:DoTaskInTime(0, function()
							hand.components.locomotor:GoToPoint(target:GetPosition())
							
							hand:ListenForEvent("onattackother", function(_, data)
									if data.target and data.target.components.health then
											-- Apply damage to the target
											data.target.components.health:DoDelta(-HAND_DAMAGE)
									end
									
									-- Play the fire-snuffing animation on contact
									-- if hand.AnimState:HasAnimation("snuff") then
									-- 		hand.AnimState:PlayAnimation("snuff")  -- Ensure the animation exists
									-- end
									
									-- Return hand to the character after attack
									hand:DoTaskInTime(0.5, function()
											hand.Transform:SetPosition(inst.Transform:GetWorldPosition()) -- Use Transform instead of locomotor
											hand:Remove()  -- Remove the hand once it returns
									end)
							end)
					end)
			end
	end
end

-- Handle attacking, including counting attacks and triggering a special attack
local function onAttack(inst, data)
    if data.target and data.target:HasTag("monster") then
        inst.components.sanity:DoDelta(SANITY_GAIN_PER_MONSTER_KILL) -- Gain sanity on monster kill
    end

    inst.attackCounter = (inst.attackCounter or 0) + 1 -- Increment attack counter

    if inst.attackCounter >= ATTACK_COUNTER_LIMIT then
        inst.attackCounter = 0 -- Reset counter after special attack

        -- Trigger the special attack with hands
        if data.target and data.target.components.health and not data.target.components.health:IsDead() then
            summonHands(inst, data.target) -- Summon hands on the third hit
        end
    end
end

-- Define the DODGE action
-- local DODGE = _G.Action()
-- DODGE.str = "Dodge"
-- DODGE.id = "DODGE"
-- DODGE.fn = function(act)
--     if act.doer ~= nil and act.doer:HasTag("player") then
--         local speed_multiplier = 2.5
--         act.doer.components.locomotor:SetExternalSpeedMultiplier(act.doer, "dodge_speed", speed_multiplier)

--         -- Remove speed boost after 0.5 seconds
--         act.doer:DoTaskInTime(0.5, function()
--             act.doer.components.locomotor:RemoveExternalSpeedMultiplier(act.doer, "dodge_speed")
--         end)

--         -- Set invincibility during dodge
--         act.doer.components.health:SetInvincible(true)
--         act.doer:DoTaskInTime(0.5, function()
--             act.doer.components.health:SetInvincible(false)
--         end)

--         return true
--     end
-- end

-- AddAction(DODGE)

-- OnKeyPressed function to detect L key and perform dodge
-- local function OnKeyPressed(inst, data)
--     if data.inst == ThePlayer then
--         if data.key == CONTROL_ACTION then
--             print("KEY_L (Dodge) has been pressed.")
--             if TheWorld.ismastersim then
--                 BufferedAction(inst, inst, ACTIONS.DODGE):Do()
--             else
--                 SendRPCToServer(RPC.DoWidgetButtonAction, ACTIONS.DODGE.code, inst, ACTIONS.DODGE.mod_name)
--             end
--         end
--     end
-- end

-- This initializes for both the server and client. Tags can be added here.
local common_postinit = function(inst) 
	-- Minimap icon
	inst.MiniMapEntity:SetIcon( "krasus.tex" )

	-- inst:AddComponent("keyhandler")
  -- inst:ListenForEvent("keypressed", OnKeyPressed)
end

-- Master post-initialization function for character stats and ability setup
local function master_postinit(inst)
	-- Grue Bank Animation
    -- Set character stats
    inst.components.health:SetMaxHealth(STARTING_HEALTH)
    inst.components.hunger:SetMax(STARTING_HUNGER)
    inst.components.sanity:SetMax(STARTING_SANITY)

		-- choose which sounds this character will play
		inst.soundsname = "wilson"

     -- Initialize attack counter and dodge cooldown
		 inst.attackCounter = 0
		 inst.dodgeCooldown = 0  -- Explicitly initialize dodgeCooldown
 
		 -- Listen for events
		 inst:ListenForEvent("onattackother", onAttack) -- Attack event
		--  inst:DoPeriodicTask(0.1, function(inst, dt) onUpdate(inst, dt) end) -- Update cooldowns every tick
 
		 -- Add components for combat and sanity
		 inst:AddComponent("sanity")
		 inst:AddComponent("health")
		 inst:AddComponent("hunger")
		 inst:AddComponent("combat")
		 inst:AddComponent("locomotor") -- Allows for movement capabilities
 end

return MakePlayerCharacter("krasus", prefabs, assets, common_postinit, master_postinit)



 

krasus.lua

Edited by Mindjigo

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