SenL Posted June 4, 2022 Share Posted June 4, 2022 Hi, I'm currently working on a mod that gains exp from player killing enemies (using ListenForEvent entity_death). It's working fine. However, when a spider gets trapped - the trap Remove() the target thus "killing" it but it does not push event entity_death so my mod gets no exp. I'm trying to find out if it's possible to listen for event when a smallcreature is trapped (with the "trap"), and find out exactly its prefab. Is there a way? Thanks. Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/ Share on other sites More sharing options...
Leonidas IV Posted June 5, 2022 Share Posted June 5, 2022 You can hook the inst.components,trap.onharvest and push a event, like this: AddPrefabPostInit("trap", function(inst) local _onharvest = inst.components,trap.onharvest inst.components.trap:SetOnHarvestFn(function(inst) _onharvest(inst) GLOBAL.GetWorld():PushEvent("entity_death", { inst = self.target } ) end) end) Also, entity_death is pushed every time a mob dies, everywhere. Probably the player push a event when he kill something. Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1575300 Share on other sites More sharing options...
SenL Posted June 5, 2022 Author Share Posted June 5, 2022 (edited) Thanks, I will try. Edit: How does the function know the "self.target"? I mean what is "self" and does it always have "target"? Also will it replace the core SetOnHarvestFn (it uses/calls "onharvested"), or add on top of it? Edit2: Crashes on "self" - it's nil. Edited June 5, 2022 by SenL Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1575309 Share on other sites More sharing options...
Leonidas IV Posted June 9, 2022 Share Posted June 9, 2022 On 6/5/2022 at 10:52 AM, SenL said: Edit2: Crashes on "self" - it's nil. My bad. Here it is: AddPrefabPostInit("trap", function(inst) local _onharvest = inst.components.trap.onharvest inst.components.trap:SetOnHarvestFn(function(inst) _onharvest(inst) GLOBAL.GetWorld():PushEvent("entity_death", { inst = inst.components.trap.target } ) end) end) On 6/5/2022 at 10:52 AM, SenL said: How does the function know the "self.target"? I mean what is "self" and does it always have "target"? I wrote self because I was looking at the component trap while writing, and inside a component, self refers to the component instance On 6/5/2022 at 10:52 AM, SenL said: Also will it replace the core SetOnHarvestFn (it uses/calls "onharvested"), or add on top of it? Add on top. I saved the original function in the _onharvest variable and then called it before the push event. Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576069 Share on other sites More sharing options...
SenL Posted June 10, 2022 Author Share Posted June 10, 2022 Thanks. That works however I'm getting another issue. My mod copied from shadowwaxwell for this part: inst:ListenForEvent("entity_death", function(world, data) entitydeathfn(inst, data) end, GetWorld()) In it, the event entity_death from the trap onharvest sends inst but not data. I think the 2nd parameter should be {data = inst.component.trap.target} However I don't know how to do the 1st parameter. Ideally it should be my mod's inst (which is a head item that gains exp and level)... but I don't know if this is possible in modmain.lua... Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576242 Share on other sites More sharing options...
Leonidas IV Posted June 10, 2022 Share Posted June 10, 2022 (edited) 57 minutes ago, SenL said: Thanks. That works however I'm getting another issue. My mod copied from shadowwaxwell for this part: inst:ListenForEvent("entity_death", function(world, data) entitydeathfn(inst, data) end, GetWorld()) In it, the event entity_death from the trap onharvest sends inst but not data. I think the 2nd parameter should be {data = inst.component.trap.target} However I don't know how to do the 1st parameter. Ideally it should be my mod's inst (which is a head item that gains exp and level)... but I don't know if this is possible in modmain.lua... You will put your inst:ListenForEvent inside your prefab. Inst will be your item and data will be the table sent by the push event: { inst = inst.components.trap.target }. To access the entity that was caught in the trap, you will use data.inst inside entitydeathfn function (data is just the name you chose to name the table sent by the push event) Edited June 10, 2022 by Leonidas IV Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576254 Share on other sites More sharing options...
SenL Posted June 10, 2022 Author Share Posted June 10, 2022 (edited) This is what I have in modmain.lua Quote AddPrefabPostInit("trap", function(inst) local _onharvest = inst.components.trap.onharvest inst.components.trap:SetOnHarvestFn(function(inst) _onharvest(inst) GLOBAL.GetWorld():PushEvent("entity_death", { inst = inst.components.trap.target }) end) end) Then in my mod lua: Quote inst:ListenForEvent("entity_death", function(world, data) entitydeathfn(inst, data) end, GetWorld()) Inside entitydeathfn: Quote local function entitydeathfn(inst, data) local killedwhat = "unknown" if data.inst.prefab then --crashes here killedwhat = data.inst.prefab end ... Crashes with "attempt to index field 'inst' (a nil value)" Edit: This entitydeathfn works fine with enemies being killed (via usual method, ie not by rabbit trap) Edited June 10, 2022 by SenL Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576290 Share on other sites More sharing options...
Leonidas IV Posted June 11, 2022 Share Posted June 11, 2022 (edited) My bad, the target is set to nil after the mob is trapped... Well, you can save the mob in another variable, for example using SetOnSpringFn in the trap prefab , setting a inst.target = target. And then using { inst = inst.target} in the event. AddPrefabPostInit("trap", function(inst) local _onharvest = inst.components.trap.onharvest inst.components.trap:SetOnHarvestFn(function(inst) _onharvest(inst) GLOBAL.GetWorld():PushEvent("entity_death", { inst = inst.target }) end) inst.components.trap:SetOnSpringFn(function(inst, target, bait) inst.target = target end) end) Honestly, I find it easier for you to just use the entity_death event without data and give your item a flat rate of XP for captured mobs. They are all small mobs. Edited June 11, 2022 by Leonidas IV 1 Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576432 Share on other sites More sharing options...
SenL Posted June 11, 2022 Author Share Posted June 11, 2022 It works!! You are awesome! 1 Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576448 Share on other sites More sharing options...
SenL Posted June 11, 2022 Author Share Posted June 11, 2022 I think the "target" is not saved when the game is quit and reload. I also think I'll just forget this and gets no exp on silly trap. Like you said it's mostly puny spiders. Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576488 Share on other sites More sharing options...
Leonidas IV Posted June 12, 2022 Share Posted June 12, 2022 (edited) 17 hours ago, SenL said: I think the "target" is not saved when the game is quit and reload. I also think I'll just forget this and gets no exp on silly trap. Like you said it's mostly puny spiders. Well, you can save and load it. Or just give a flat XP rate at harvest Edited June 12, 2022 by Leonidas IV Link to comment https://forums.kleientertainment.com/forums/topic/140678-how-to-listen-for-event-when-smallcreature-is-trapped/#findComment-1576597 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now