Detect entity Logistic Requester Chest and derivates ?

Place to post guides, observations, things related to modding that are not mods themselves.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

Hi,

while testing an entity in an event, I have to detect if it's a Logistic Requester Chest or a derivated object (created by another mod for example).
In order to deal with its request slots with get_request_slot.
But there is no "Logistic Requester Chest" type, just a "Logistic Container" type.
How can I detect if a Container Chest has Request Slots ? (i.e. is a Logistic Requester Chest or derivated)
Note that in data.lua, you have access to logistic_mode = "requester", that determinates the requester from the other "Logistic Containers"
My mods on the Factorio Mod Portal :geek:
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by Nexela »

Not able to test but would something like this work?

isRequester, inSlot = pcall(entity.get_request_slot(1)) -- return true/false and simpleitemstack/error or error code.... maybe drop the inSlot and do that seperatly.

edit: Fixed typo, expanded
Last edited by Nexela on Fri Jul 29, 2016 8:48 pm, edited 1 time in total.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

Yes, not very clean but pcall always works ;-)
I was hoping for something more regular.
My mods on the Factorio Mod Portal :geek:
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by Nexela »

Having a has_request_slots or access to logistic mode would nice but pcall works wonders if used correctly.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by aubergine18 »

Is there something on the entity.prototype you could test for? Alternatively, could you scan all the prototypes in data.raw when game starts and for any that meet your criteria add a flag to the prototype - that way you can later use entity.has_flag(your_flag) to detect if the entity is the required type before attempting entity.get_request_slot()

While pcall() will certainly work, it seems a very laggy way of doing things as in many cases an error will occur internally within the pcall() resulting in a stack trace.

IMO best solution would be to have a new entity.has_request_slots added to the API as suggested by @Nexela
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

that way you can later use entity.has_flag(your_flag)
Never thought about that to "transmit" informations from data.lua to control.lua when the API does not suppor the feature ! Nice idea, thx.
My mods on the Factorio Mod Portal :geek:
Rseding91
Factorio Staff
Factorio Staff
Posts: 14912
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by Rseding91 »

I added LuaEntity::request_slot_count read for 0.13.13.
If you want to get ahold of me I'm almost always on Discord.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

Rseding91 wrote:I added LuaEntity::request_slot_count read for 0.13.13.
But will it be 0 on none-requester entities ? or will it trigger an error ? or at least return 0 on any logical-container that cannot request ?
because my initial purpose was to detect if any entity is a requester type.
My mods on the Factorio Mod Portal :geek:
Rseding91
Factorio Staff
Factorio Staff
Posts: 14912
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by Rseding91 »

binbinhfr wrote:
Rseding91 wrote:I added LuaEntity::request_slot_count read for 0.13.13.
But will it be 0 on none-requester entities ? or will it trigger an error ? or at least return 0 on any logical-container that cannot request ?
because my initial purpose was to detect if any entity is a requester type.
It's 0 if the entity doesn't have request slots. No errors.
If you want to get ahold of me I'm almost always on Discord.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

Rseding91 wrote:It's 0 if the entity doesn't have request slots. No errors.
Nice ! Thx man.
My mods on the Factorio Mod Portal :geek:
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

aubergine18 wrote:Is there something on the entity.prototype you could test for? Alternatively, could you scan all the prototypes in data.raw when game starts and for any that meet your criteria add a flag to the prototype - that way you can later use entity.has_flag(your_flag) to detect if the entity is the required type before attempting entity.get_request_slot()

While pcall() will certainly work, it seems a very laggy way of doing things as in many cases an error will occur internally within the pcall() resulting in a stack trace.

IMO best solution would be to have a new entity.has_request_slots added to the API as suggested by @Nexela
Hi Aubergine,

did you ever try this ?
because trying to add a custom flag does not work for me :

Code: Select all

for _, inserter in pairs(data.raw["inserter"]) do
	if inserter.filter_count and inserter.filter_count > 0 then
		if not inserter.flags then inserter.flags = {} end
		table.insert(inserter.flags,"my_filter")
	end
end
it says unknown flag.
so apparently factorio only support its own flags...
could you tell me if I made a mistake ?
My mods on the Factorio Mod Portal :geek:
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

Good morning Rseding,

In the same order of idea, it would be great to have a
filter_slot_count :: uint [R] The number of filter slots this entity has. (0 if none or non applicable)
My mods on the Factorio Mod Portal :geek:
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by aubergine18 »

binbinhfr wrote: Hi Aubergine,

did you ever try this ?
because trying to add a custom flag does not work for me :
Nope, having done some additional digging it seems that flags are limited to those the game defines rather than an open-ended dictionary of strings. :( I guess that's why there's a specific function for checking flags rather than just exposing a table of the flags. Which is a shame, because being able to custom tag (folksonomy) prototypes would be very useful in many situations.

Now that the new property is added in latest release it should be easier to detect the requester chests, but there's still no easy way to group certain types of entities based on any abstract criteria.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14912
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by Rseding91 »

If entity prototype flags was a simple collection of strings that would increase the memory usage by a large amount and slow down checking them by an even larger amount since the entire contents of each string would need to be checked against every string on the entity prototype.

As it is now it's a simple bitmask and 1 bitwise operation to check if an entity has a flag defined - no memory allocations and no iterating to check.
If you want to get ahold of me I'm almost always on Discord.
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by aubergine18 »

@Rseding91: Agreed, bitmasks are much faster and efficient to work with, and certainly the best choice for the factorio flag taxonomy.

Would it be possible to expose the flags bitmask to the API? That way we could use the Bit32 library - eg. to check for multiple flags in a single operation, rather than multiple has_flag() calls.

Also, Strings in Lua aren't quite as bad as you're making out (still slower and more crufty than bitmasks obviously)...

From Lua GC docs:
Lua has unique strings, that means that each possible string exists only once within Lua. If a new string occurs Lua first checks if it already exists within the string pool. If it does, a reference to that older string is used. If it does not exist, a new string object is created and put into the string pool. The GC checks the string pool for unused strings and frees them.
A table of strings is actually a table of indices of those strings in the central string pool. When comparing strings, Lua actually compares their indices rather than the strings themselves. This means that string length and content has no bearing on the performance of operations that compare strings; it's as fast as comparing two numbers.

Would it be possible to get a .tags property on prototypes (and corresponding .has_tag() method on entities for lazy evaluation) so we can create custom folksonomies? While still slow, compared to bitmasks, it will greatly simplify many situations. For example, last night I was looking at the code for Nixie tubes mod, and there's three entities involved - currently that requires up to 3 string comparisons (well, the numeric index of those strings in the string pool) to determine if an entity is a nixie tube. By having a folksonomy, that would be reduced to a single comparison of a tag.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1525
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by binbinhfr »

Would it be possible to get a .tags property on prototypes (and corresponding .has_tag() method on entities for lazy evaluation) so we can create custom folksonomies?
I totally second this !
My mods on the Factorio Mod Portal :geek:
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Detect entity Logistic Requester Chest and derivates ?

Post by Nexela »

Rseding,

Me again! but don't get angry :)

Thanks for adding in the request_slot_count

but I have come across a problem and having luaentityprototype read for logistic_mode would come in handy. As always thanks for everything you and all the other devs do to keep this game awesome!
Post Reply

Return to “Modding discussion”