Jump to content

How do I listen for combat on a prefab?


Recommended Posts

Hi!

I want to know how to write a hook for when a mob starts to fight.

I'm guessing it's possible to simply add a "listenforevent" to the mob, but I'm unsure what events trigger during combat or how to find what events are available to listen for. Are there any files I can read that contain all events or something like that?

Or, if there is another, better, way - please let me know!

I just want to make the royal pig guards swap their torches if they fight during the night - which should be easy enough provided I can make a function trigger whenever they start to fight.

 

edit: So far I have tried listening for "attack", "onattack", "onattackother", "onhitother" and "doattack" with no luck.

Edited by Somnei
Link to comment
Share on other sites

It's supposed to work, maybe you're not doing it right.

local function SwapEquip(inst, data)
  	print("Testing if it's run")
	--Stuff
end

AddPrefabPostInit("the_pig_prefab", function(inst)
	inst:ListenForEvent("onattack", SwapEquip)
end)

 

  • Like 1
Link to comment
Share on other sites

Hm, that is strange. I wonder if the royal guards are just bugged somehow...

I wrote some code based on your example and it didn't work, so I then tried copying your code so mine was identical (replacing "the_pig_prefab" with "pigman_city" and "pigman_royalguard" as well to be on the safe side - still nothing.

I've made a workaround by instead adding an attackcallback on their weapon, but I'm just a bit puzzled as to why the other way isn't working for me.

Link to comment
Share on other sites

On 8/30/2022 at 7:50 AM, Somnei said:

Hm, that is strange. I wonder if the royal guards are just bugged somehow...

I wrote some code based on your example and it didn't work, so I then tried copying your code so mine was identical (replacing "the_pig_prefab" with "pigman_city" and "pigman_royalguard" as well to be on the safe side - still nothing.

I've made a workaround by instead adding an attackcallback on their weapon, but I'm just a bit puzzled as to why the other way isn't working for me.

You can try to print something and see if it shows on console.

Link to comment
Share on other sites

17 minutes ago, KXWl1 said:

You can try to print something and see if it shows on console.

Very good tip for debugging! I always make sure to use a lot of prints when I am stuck on a problem.

  • Like 1
Link to comment
Share on other sites

On 8/29/2022 at 2:24 PM, Leonidas IV said:
local function SwapEquip(inst, data)
  	print("Testing if it's run")
	--Stuff
end

AddPrefabPostInit("the_pig_prefab", function(inst)
	inst:ListenForEvent("onattack", SwapEquip)
end)

Its onattackother not onattack

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