Jump to content

[Solved] Need help with adding new state to Spider


Recommended Posts

Hello, if someone can help me with this that would really be great :D!

So, basically I want to add a new dodge state to spiders since it seems they have an unused evade animation & personally my mod char is strong so I want to buff all entities to be more of a challenge.

So, here's the code

Spoiler

AddPrefabPostInit("spider", function(inst)
	local _GetAttacked = inst.components.combat.GetAttacked
	function inst.components.combat:GetAttacked(self, ...)
		local dodge = math.random(1,1)
		
		if inst.sg:HasStateTag("dodging") and not dodge == 1 then
			return false
		elseif dodge == 1 then
			inst.sg:GoToState("dodge")
			return false
		end
		return _GetAttacked(self, ...)
	end
	
end)

AddStategraphState("spider", State{
	name = "dodge",
	tags = { "canrotate", "busy", "dodging" },

	onenter = function(inst, attacker)
		inst.components.locomtor:Stop()
		inst.AnimState:PlayAnimation("evade")
		
		inst.sg.statemem.attacker = attacker
	end,
	
	onexit = function(inst)
		inst.components.locomotor:Stop()
		inst.Physics:ClearMotorValOverride()
	end,
	
	timeline =
	{
		TimeEvent(1*FRAMES, function(inst) inst:ForceFacePoint(inst.sg.statemem.attacker) inst.Physics:SetMotorVelOverride(20,0,0) inst.SoundEmitter:PlaySound("dontstarve/creatures/spiderwarrior/jump") end),
		TimeEvent(6*FRAMES, function(inst)
			inst.Physics:ClearMotorValOverride()
			inst.components.locomotor:Stop()
		end),
	},
	
	events =
	{
	EventHandler("animover", function(inst)
		if inst.AnimState:AnimDone() then
			inst.sg:GoToState("idle")
		end
	end),
	},
})

 

The problem I'm having now is I want the Spider to always SetMotorVelOverride the way it looks like it's evading but I don't know how to do that, & the SetMotorVelOverride sometimes moves the spider very short distance or sometimes a very far distance! I have no idea why that happens since I took it straight from the spider warrior's lunge attack :confused:

Any help would be appreciated :wilson_flower:!!

Edited by SuperDavid
Link to comment
Share on other sites

So the thing is that you should generally avoid calling Stategraph:GoToState() method outside of the stategraphs.

You can see that the original GetAttacked don't call it directly but instead pushes events. These events are then intercepted by the stategraph and interpreted using a EventHandler.

Also it is a bit contradictory right now because even if your spider dodges, the standard GetAttacked is called.

I would try something like

Spoiler

AddPrefabPostInit("spider", function(inst)
	
	if not GLOBAL.TheWorld.ismastersim then
		return inst
	end
	
	local _GetAttacked = inst.components.combat.GetAttacked
	function inst.components.combat:GetAttacked(self, attacker, damage, weapon, stimuli)
		local dodge = math.random(0,1)
		if dodge < 0.5 then 
        	dodge = 0
        else 
        	dodge = 1
        end
		
		if dodge == 1 then
			self.inst:PushEvent("dodge")
            return false
		else
			return _GetAttacked(self, ...)
        end
	end
	
end)

AddStategraphState("spider", State{
	name = "dodge",
	tags = { "canrotate", "busy", "dodging" },

	onenter = function(inst, attacker)
		inst.components.locomotor:Stop()
		inst.AnimState:PlayAnimation("evade")
		
		inst.sg.statemem.attacker = attacker
	end,
	
	onexit = function(inst)
		inst.components.locomotor:Stop()
		inst.Physics:ClearMotorVelOverride()
	end,
	
	timeline =
	{
		TimeEvent(1*FRAMES, function(inst) inst:ForceFacePoint(inst.sg.statemem.attacker) inst.Physics:SetMotorVelOverride(20,0,0) inst.SoundEmitter:PlaySound("dontstarve/creatures/spiderwarrior/jump") end),
		TimeEvent(6*FRAMES, function(inst)
			inst.Physics:ClearMotorVelOverride()
			inst.components.locomotor:Stop()
		end),
	},
	
	events =
	{
	EventHandler("animover", function(inst)
		if inst.AnimState:AnimDone() then
			inst.sg:GoToState("idle")
		end
	end),
	},
})
                      
AddStategraphEvent("SGspider.lua", GLOBAL.EventHandler("dodge", function(inst) 
                      	if not inst.sg:HasStateTag("dodging") then
                      		inst.sg:GoToState("dodge")
                      	else
                      		-- I don't know what you want to do in this case?
                      	end
                      end))

 

Untested and probably have errors but this is to give you the general idea

Edited by ZupaleX
Link to comment
Share on other sites

Oops Nver mind it's because 

AddStategraphEvent("SGspider.lua", GLOBAL.EventHandler("dodge", function(inst) 

had to be

AddStategraphEvent("spider", GLOBAL.EventHandler("dodge", function(inst) 

 

Now all I need is the  SetMotorVelOverride  thingy :wilson_smile:

Edited by SuperDavid
Link to comment
Share on other sites

Try to add

print("Spider should dodge")

inside the EventHandler to see if this is called. Currently you have your random number set to be between 1 and 1 so it will always be 1 and the spider should always dodge.

 

EDIT: yeah sorry  don't know what I was thinking when I put that in the ADdStategraphEvent(...) .....

what do you want to do with that SetMotorVelOverride function? This literally sets the speed of your entity to a specific value. Why do you want to do that?

Edited by ZupaleX
Link to comment
Share on other sites

I just want the Spider to move a little to the opposite direction the attack hit it because in its dodge animation it looks like it jumps to the side

3.gif

Though I don't know how I would do that. I see in spider warrior's lunge action it has MotorVal so I thought maybe that's how?

Link to comment
Share on other sites

Oh I see.

Then you should override the velocity with a more reasonable value, like 5? Also if you want your spider to move sideway you have to do  SetMotorVelOverride(0, 0, 5), putting the override on the first argument will make it move in the direction it's facing.

The thing with  SetMotorVelOverride is that it is cancelled as soon as another "action is pushed. But since your spider is in the "dodge" state it should always be 6 frame long until the state is over.

Except if another state can randomly be set as active while the spider is dodging. And since you put the tag "busy" in the "dodge" state the only way I can see it happens is if the "attacked" state is triggered somehow even though the spider should have dodged.

I'm off to bed though now so I won't see your posts until tomorrow (later today actually, it's 4am now.)

 

Edited by ZupaleX
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...