Jump to content

How to make game count something as an attack


Recommended Posts

Hello, if someone can help me with this that'd be really awesome of you :D!

So, basically I have code where if my character presses a button he can preform a special attack with unique animation and stuff, now the problem is this attack is not dealing damage based off my character's damage multiplier or for example if he attacks with a torch it will not set entity on fire also tool won't lose durability I think this happen because game is not counting this as a real attack :(?

This is part of the code I currently use to preform an attack on an entity

Spoiler

local equip = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
local target = GLOBAL.GetClosestInstWithTag({"_health", "_combat"}, inst, 3)
inst.components.combat:SetTarget(target)
inst.components.combat:StartAttack()
inst.components.locomotor:Stop()
inst.AnimState:PlayAnimation("slash_heavy")
if target ~= nil then
	inst.components.combat:BattleCry()
	if target:IsValid() then
			inst:FacePoint(target:GetPosition())
	end
end


if target ~= nil then
	if equip ~= nil then
	local weapon_damage = equip.components.weapon.damage
		target.components.combat:GetAttacked(inst, weapon_damage, nil)
	else
		target.components.combat:GetAttacked(inst, 10, nil)
	end
	inst:PushEvent("onattackother", {target, weapon, projectile, stimuli}) -- this doesn't work?
end

 

Is there a way I can make the code that this special attack which happens when press a key still acts same as a normal attack or will I have to do every single thing for this special attack manually? Like making tool lose durability, checking to see character if they have damage multiplier, ect. 

Thanks so much for your time and have very great new year :D!!!

 

Edit: maybe the game needs to have "inst:PerformBufferedAction()" for ACTIONS.ATTACK but how?

 

Edit 2: So I added this code to it as well 

inst:PerformBufferedAction(GLOBAL.ACTIONS.ATTACK)
inst:PushBufferedAction(GLOBAL.ACTIONS.ATTACK)

but it just crashes saying, any advice what I should try next?

[00:00:31]: [string "scripts/entityscript.lua"]:1274: attempt to call method 'TestForStart' (a nil value)
Edited by SuperDavid
Link to comment
Share on other sites

ok here is all the code of this new attack

 

modmain.lua

Spoiler

AddStategraphState("wilson", State{
    name = "attack_tfc",
    tags = { "busy", "attack", "notalking", "attack_tfc" }, --"autopredict"

	onenter = function(inst)
		ForceStopHeavyLifting(inst)
		
		local equip = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		--local weapon_range = equip.components.weapon.attackrange
		local target = GLOBAL.GetClosestInstWithTag({"_health", "_combat"}, inst, 3)
		if target ~= nil and not target.components.health:IsDead() and not target:HasTag("playerghost") and target.invicivlke == nil then
			if target.invicivlke == true and not equip then
				inst.components.combat:SetTarget(target)
				inst.components.combat:StartAttack()
			elseif target.invicivlke == nil then
				inst.components.combat:SetTarget(target)
				inst.components.combat:StartAttack()
			end
		end
		inst.components.locomotor:Stop()
		
		if equip ~= nil and equip:HasTag("whip") then
			inst.AnimState:PlayAnimation("whip_pre")
			inst.AnimState:PushAnimation("whip", false)
			inst.SoundEmitter:PlaySound("dontstarve/common/whip_pre", nil, nil, true)
		elseif equip ~= nil and equip:HasTag("book") then
			inst.AnimState:PlayAnimation("attack_book")
			inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_whoosh", nil, nil, true)
		elseif equip ~= nil and equip:HasTag("chop_attack") then --and inst:HasTag("woodcutter")
			inst.AnimState:PlayAnimation(inst.AnimState:IsCurrentAnimation("woodie_chop_loop") and inst.AnimState:GetCurrentAnimationTime() < 7.1 * FRAMES and "woodie_chop_atk_pre" or "woodie_chop_pre")
			inst.AnimState:PushAnimation("woodie_chop_loop", false)
		elseif equip ~= nil and equip.components.weapon ~= nil and not equip:HasTag("punch") then
			inst.AnimState:PlayAnimation("atk_pre")
			inst.AnimState:PushAnimation("atk", false)
			inst.SoundEmitter:PlaySound(
			(equip:HasTag("icestaff") and "dontstarve/wilson/attack_weapon") or
			(equip:HasTag("shadow") and "dontstarve/wilson/attack_nightsword") or
			(equip:HasTag("firestaff") and "dontstarve/wilson/attack_weapon") or
			"dontstarve/wilson/attack_weapon", nil, nil, true)
		elseif equip ~= nil and (equip:HasTag("light") or equip:HasTag("nopunch")) then
			inst.AnimState:PlayAnimation("atk_pre")
			inst.AnimState:PushAnimation("atk", false)
			inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_weapon", nil, nil, true)
		elseif inst:HasTag("beaver") then
			inst.AnimState:PlayAnimation("throw")
			inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_whoosh", nil, nil, true)
		else
			inst.AnimState:PlayAnimation("punch")
			inst.SoundEmitter:PlaySound("dontstarve/wilson/attack_whoosh", nil, nil, true)
		end
			
		if target ~= nil and not target.components.health:IsDead() and not target:HasTag("playerghost") and target.invicivlke == nil and target.invicivlke == nil then
			inst.components.combat:BattleCry()
			
			if target:IsValid() then
				inst:FacePoint(target:GetPosition())
			end
		end
		

		if inst.components.playercontroller ~= nil then
			inst.components.playercontroller:RemotePausePrediction()
		end
	end,
		
	timeline =
	{
	TimeEvent(10 * FRAMES, function(inst)
		local target = GLOBAL.GetClosestInstWithTag({"_health", "_combat"}, inst, 3)
		local equip = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		local fist_damage = inst.components.combat.defaultdamage
		
		if target ~= nil and not target.components.health:IsDead() and not target:HasTag("playerghost") and target.invicivlke == nil and target.invicivlke == nil then
			if equip ~= nil then
				local weapon_damage = equip.components.weapon.damage
				target.components.combat:GetAttacked(inst, weapon_damage, nil)
			else
				target.components.combat:GetAttacked(inst, fist_damage, nil)
			end
			inst:PushEvent("onattackother", {target, weapon, projectile, stimuli})
		end
		inst.attack_tfc = nil
		inst.sg:RemoveStateTag("busy")
		inst.sg:RemoveStateTag("attack_tfc")
	end),
	},

	events =
		{
		EventHandler("equip", function(inst) inst.sg:GoToState("idle") end),
		EventHandler("unequip", function(inst) inst.sg:GoToState("idle") end),
		EventHandler("animover", function(inst)
			if inst.AnimState:AnimDone() then
				inst.sg:GoToState("idle")
			end
		end),
		},
		
	onexit = function(inst)
		if inst.sg:HasStateTag("abouttoattack") then
			inst.components.combat:CancelAttack()
			inst.attack_tfc = nil
		end
	end,
})

local ATTACK_TFC = GLOBAL.Action()
ATTACK_TFC.str = "Attack"
ATTACK_TFC.id = "ATTACK_TFC"
ATTACK_TFC.fn = function(act)
	if act.target.components.health:IsDead() or act.target:HasTag("playerghost") or act.target.attack_tfc == true or act.target.components.rider:IsRiding() or act.target.jump == true or act.target.sg:HasStateTag("busy") then return end
	
	act.target.attack_tfc = true
	act.target.sg:GoToState("attack_tfc")
end
AddAction(ATTACK_TFC)

the plan is i think the current attack system in dont starve is way too easy (even Minecraft has a more advanced combat system), so I want make attack system more like other games where you need to at least move up to your target and click button for every attack.

Anyways problem with this code know is the game don't count it as a real attack by the inst for some reason, I'm using "target.components.combat:GetAttacked(inst, weapon_damage, nil)" to make the target get attacked from this state but this way the damage isn't being multipliered by the character & the weapon's effects don't happen (like torch setting stuff on fire) or they don't lose durability.

Is there a different combat function I am suppose using since combat:GetAttacked don't seem to really work.

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...