Page 1 of 1
Detect entity Logistic Requester Chest and derivates ?
Posted: Fri Jul 29, 2016 7:17 pm
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"
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Fri Jul 29, 2016 8:37 pm
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
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Fri Jul 29, 2016 8:44 pm
by binbinhfr
Yes, not very clean but pcall always works
![Wink ;-)](./images/smilies/icon_e_wink.gif)
I was hoping for something more regular.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Fri Jul 29, 2016 8:55 pm
by Nexela
Having a has_request_slots or access to logistic mode would nice but pcall works wonders if used correctly.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Fri Jul 29, 2016 9:07 pm
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
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Fri Jul 29, 2016 9:21 pm
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.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sat Jul 30, 2016 1:54 pm
by Rseding91
I added LuaEntity::request_slot_count read for 0.13.13.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sat Jul 30, 2016 2:07 pm
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.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sat Jul 30, 2016 3:21 pm
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.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sat Jul 30, 2016 3:28 pm
by binbinhfr
Rseding91 wrote:It's 0 if the entity doesn't have request slots. No errors.
Nice ! Thx man.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sun Jul 31, 2016 6:57 am
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 ?
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sun Jul 31, 2016 7:20 am
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)
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sun Jul 31, 2016 12:31 pm
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.
![Sad :(](./images/smilies/icon_e_sad.gif)
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.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sun Jul 31, 2016 12:39 pm
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.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sun Jul 31, 2016 1:24 pm
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.
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Sun Jul 31, 2016 3:04 pm
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 !
Re: Detect entity Logistic Requester Chest and derivates ?
Posted: Thu Aug 25, 2016 5:38 am
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!