Jump to content

Recommended Posts

hello-o,
i'm new here and english is not my main language so sorry if my sentences suxx a bit D:

sooo i don't know how to code and  i'm trying to make a wood character mod and i want him to "spawn" wood logs when he takes damages from ennemies.
and also, sometimes just spawn randomly wood logs when he walks/runs

is this possible? :o
thank you for your help and time :)

Edited by Hse

This will spawn log with 50% chance when your char got hitted

inst:ListenForEvent("attacked", function(inst)
	inst:DoRaskInTime(0, function(inst)
		if math.random <= .5 then
			local wood = SpawnPrefab("log")
			wood.Transform:SetPostiton(inst.Transform:GetWorldPosition())
			
			if wood.Physics ~= nil then
				local x, y, z = wood.Transform:GetWorldPosition()
				wood.Physics:Teleport(x, .3, z)

				local angle = (math.random() * 20 - 10) * DEGREES
				angle = angle + math.random() * 2 * PI
				
				local speed = projectile and 2 + math.random() or 3 + math.random() * 2
				wood.Physics:SetVel(math.cos(angle) * speed, 10, math.sin(angle) * speed)
			end
		end
	end)
end)

 

  • Like 1

oh so yeah, i'm at the good place, in the character.lua.
well the code is at the end of the file

 


local MakePlayerCharacter = require "prefabs/player_common"


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

-- Custom starting items
local start_inv = {
}

-- When the character is revived from human
local function onbecamehuman(inst)
	-- Set speed when reviving from ghost (optional)
	inst.components.locomotor:SetExternalSpeedMultiplier(inst, "magicaca_speed_mod", 1)
end

local function onbecameghost(inst)
	-- Remove speed modifier when becoming a ghost
   inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "magicaca_speed_mod")
end

-- When loading or spawning the character
local function onload(inst)
    inst:ListenForEvent("ms_respawnedfromghost", onbecamehuman)
    inst:ListenForEvent("ms_becameghost", onbecameghost)

    if inst:HasTag("playerghost") then
        onbecameghost(inst)
    else
        onbecamehuman(inst)
    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( "magicaca.tex" )
end

-- This initializes for the server only. Components are added here.
local master_postinit = function(inst)
	-- choose which sounds this character will play
	inst.soundsname = "wilson"
	
	-- Uncomment if "wathgrithr"(Wigfrid) or "webber" voice is used
    --inst.talker_path_override = "dontstarve_DLC001/characters/"
	
	-- Stats	
	inst.components.health:SetMaxHealth(150)
	inst.components.hunger:SetMax(300)
	inst.components.sanity:SetMax(200)
	
	-- Damage multiplier (optional)
    inst.components.combat.damagemultiplier = 1
	
	-- Hunger rate (optional)
	inst.components.hunger.hungerrate = 1 * TUNING.WILSON_HUNGER_RATE
	
	inst.OnLoad = onload
    inst.OnNewSpawn = onload

	-- Movement speed (optional)
	inst.components.locomotor.walkspeed = 8
	inst.components.locomotor.runspeed = 11	
	
	inst:ListenForEvent("attacked", function(inst)
	inst:DoTaskInTime(0, function(inst)
		if math.random() < .4 then
			local wood = SpawnPrefab("log")
			wood.Transform:SetPostiton(inst.Transform:GetWorldPosition())
			
			if wood.Physics ~= nil then
				local x, y, z = wood.Transform:GetWorldPosition()
				wood.Physics:Teleport(x, .3, z)

				local angle = (math.random() * 20 - 10) * DEGREES
				angle = angle + math.random() * 2 * PI
				
				local speed = projectile and 2 + math.random() or 3 + math.random() * 2
				wood.Physics:SetVel(math.cos(angle) * speed, 10, math.sin(angle) * speed)
			end
		end
	end)
end)
end

return MakePlayerCharacter("magicaca", prefabs, assets, common_postinit, master_postinit, start_inv)

 

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