filtered event entity_with_owner

Place to get help with not working mods / modding interface.
Post Reply
User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

filtered event entity_with_owner

Post by TelemakFactorio »

I am trying to know in the event on_entity_destroyed if the event.entity parameter is an EntityWithOwner with a conditional test without raising any errors or exception 100% of the cases ;
But I have no idea on how to test this. I am doing this because I have written a piece of code that is using the last_owner property. Can anyone help me ?

If not possible / subsiadary question : I am using the last_user property because I want to know the force which the last destroyed entity BUILDING is belonging to.
Is there an other way to do that ?

Many thanks

Bilka
Factorio Staff
Factorio Staff
Posts: 3140
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: filtered event entity_with_owner

Post by Bilka »

event.entity.force is the force the entity belongs to.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

Re: filtered event entity_with_owner

Post by TelemakFactorio »

Oh yeah, but if I want to be sure it is a building ?
And if I want the code to work with any other mods ?
The correct name of the event is on_entity_die

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: filtered event entity_with_owner

Post by eradicator »

There's no inherit concept of what a "building" is and what is not, so you'll have to supply that list yourself. For example using a table lookup construct like this:

Code: Select all

if_building = {
  ['assembling-machine'] = true,
  ['furnace'] = true,
  ['electric-pole'] = true,
  }

script.on_event(defines.events.on_entity_died,function(event)
  if is_building[event.entity.type] then
    print('A building has died!')
    end
  end)
As you now have a predefind list of entities you already know that all of them have a force. And mods can't change that so it will be compatible. Btw, it's easier to answer these questions if you state the exact goal you're aiming for instead of some vague "i need this behavior", because experienced modders often know better ways to achieve what you're trying to do than you can think of :P.

User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

Re: filtered event entity_with_owner

Post by TelemakFactorio »

Ah ok :)
It is for the mod Comprehensive Biters.
When an alien destroy a player building from any faction, the alien is killed, then the evolution is recalculated, eventualy in some cases (last building of its kind ex last factory) the attack is stopped.

I am a bit violent because of my ignorance of the API details.
I am testing (last_user property ~= nil) to know if it is a building. For now I had no exception but it is maybe based on luck.
I was unable to find the responsible alien so I kill 1 alien around the building (filter, limited to 1 with sharp bounding box) : the event.cause is optional, in my tests it was nil.
To stop the attack I am doing kill_all_units.

Bilka
Factorio Staff
Factorio Staff
Posts: 3140
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: filtered event entity_with_owner

Post by Bilka »

TelemakFactorio wrote:Ah ok :)
It is for the mod Comprehensive Biters.
When an alien destroy a player building from any faction, the alien is killed, then the evolution is recalculated, eventualy in some cases (last building of its kind ex last factory) the attack is stopped.
In that case, you should check for the player-creation flag of the entity prototype to see if it is a building, that would easily be compatible with other mods. You can check for that the following way:

Code: Select all

if event.entity.has_flag("player-creation") then ...
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: filtered event entity_with_owner

Post by eradicator »

Bilka wrote:In that case, you should check for the player-creation flag of the entity prototype to see if it is a building, that would easily be compatible with other mods. You can check for that the following way:

Code: Select all

if event.entity.has_flag("player-creation") then ...
At that point you're relying on mod authors to do the right thing (== very bad idea). Furthermore i'm not aware that the "player-creation" flag has any actual effect (feel free to educate me if you know more). So it's not as such wrong to have an i.e. assembly machine without the flag. And lastly even the base game has things like cars that are clearly not buildings but have a "player-creation" flag.
I recommend relying soley on engine-inherit properties that no mod can change. I.e. things like the prototype-type solution i posted above.
If you're currently using an unfiltered on_entity_died event handler that checks for last_user... try shooting a few trees/rocks/biters and see if it crashes. You'll know how much luck you've had so far then ;).

User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2633
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: filtered event entity_with_owner

Post by steinio »

just pcall it and done :p
Image

Transport Belt Repair Man

View unread Posts

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: filtered event entity_with_owner

Post by eradicator »

steinio wrote:just pcall it and done :p
<º))))><

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: filtered event entity_with_owner

Post by bobingabout »

Only people who arn't confident with their code use a pcall.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

Re: filtered event entity_with_owner

Post by TelemakFactorio »

That's cool because I didn't know for all these solutions.
Today I will probably do the shoot rocks and trees test.
eradicator's solution is great but needs more work fo each mod with buildings to adapt.
I am not elitist bob and if I have to I will use pcall for the explained reasons I'll do it in last resort :)

User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

Re: filtered event entity_with_owner

Post by TelemakFactorio »

:lol: I have shoot trees and event.last_user.entity is nil

If it is not the cartesian method to filter buildings, maybe it will function after all.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: filtered event entity_with_owner

Post by eradicator »

TelemakFactorio wrote:eradicator's solution is great but needs more work fo each mod with buildings to adapt.
I am not elitist bob and if I have to I will use pcall for the explained reasons I'll do it in last resort :)
My solution relies on prototype types, which can not be changed by mods. There's no need to handle anything "for each mod", exactly as i said above.
And pcall will cause more problems and headache down the line than it "fixes" because it just hides the error. Don't listen to the troll. This has nothing to do with any sort of "elitism".

Also don't forget to shoot a car/train/wagon :P.

User avatar
TelemakFactorio
Long Handed Inserter
Long Handed Inserter
Posts: 53
Joined: Fri Oct 14, 2016 4:30 pm
Contact:

Re: filtered event entity_with_owner

Post by TelemakFactorio »

I get it. Implementing a list right now.

Post Reply

Return to “Modding help”