Jump to content

How to listen for event when smallcreature is trapped?


Recommended Posts

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
Share on other sites

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
Share on other sites

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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
Share on other sites

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

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