Jump to content

Unwanted repeats of attacks


Recommended Posts

I'm having an issue with making the player do a different animation when using a weapon other than the normal attack animation. He plays the animation and uses the weapon just fine, until it's over. Then about 75% of the time he repeats the animation and attack a second time, sometimes even a third. This is if I just tap control F and then let go during the first attack.

Here's my code

Modmain
 

AddStategraphPostInit("wilson", function(sg)
	local old_doattack = sg.events.doattack.fn
	sg.events.doattack.fn = function(inst)
		local weapon = inst.components.combat and inst.components.combat:GetWeapon()
		if weapon and weapon:HasTag("spellbook") then 
			if not inst.components.health:IsDead() and not inst.sg:HasStateTag("doing") and not inst.sg:HasStateTag("attack") then
				inst.sg:GoToState("book")
				weapon.components.weapon:OnAttack(inst, inst.components.combat.target)
				inst.sg:AddStateTag("attack")
			end
		else
			return old_doattack(inst)
		end
	end
end)

Weapon prefab

local function onattack(inst, attacker, target)
	attacker:DoTaskInTime(2, function()
		if attacker.sg:HasStateTag("doing") then
			finishspell(inst,attacker,target)
		end
	end)
end
local function finishspell(inst, attacker, target)
	attacker.components.sanity:DoDelta(-TUNING.SANITY_SMALL)
	
	local pos = Vector3(target.Transform:GetWorldPosition())
	local hit = SpawnPrefab("statue_transition_2")
	hit.Transform:SetPosition(pos.x, pos.y, pos.z)
	attacker.SoundEmitter:PlaySound("dontstarve/wilson/hit")
	
	if target.components.sleeper and target.components.sleeper:IsAsleep() then
		target.components.sleeper:WakeUp()
	end
	
	if target.components.health and not target.components.health:IsDead() then
		if target.components.combat then
			target.components.combat:SuggestTarget(attacker)
			if target.sg and not target.sg:HasStateTag("frozen") and target.sg.sg.states.hit then
				target.sg:GoToState("hit")
			end
		end
		target.components.health:DoDelta(-20)
		local crit = math.random()
		if crit > 0.7 then
			local critical = SpawnPrefab("explode_small")
			critical.Transform:SetPosition(pos.x, pos.y, pos.z)
			attacker.SoundEmitter:PlaySound("dontstarve/creatures/bishop/shotexplo")
			target.components.health:DoDelta(-40)
		end
	end

	attacker.sg:RemoveStateTag("attack")
end

Obviously finishspell is before onattack in the file, but I'm listing it below it because that's the order it's called in.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...