Page 1 of 1
How to detect if a non-beacon machine can accept modules
Posted: Wed Sep 20, 2023 7:55 pm
by AlexTheNotsogreat
I'm trying to implement a way for machines to disable if there's too many beacons surrounding it, but the following function isnt working.
Code: Select all
script.on_event(defines.events.on_built_entity, function(event)
local machine = event.created_entity
if machine.prototype.allowed_effects == true then
if beacons_count > 1 then
machine.active = false
end
end
end)
the problem itself is the machine.prototype.allowed_effects conditional; since it refers to the entity prototype it doesnt result in an error, but it also seems to never be true regardless of whether or not a machine actually does accept module or beacon effects. Removing that condition and just using the beacons_count conditional works for entities that accept effects but gives an error message otherwise, meaning I can't bypass it.
Re: How to detect if a non-beacon machine can accept modules
Posted: Wed Sep 20, 2023 8:19 pm
by Pi-C
AlexTheNotsogreat wrote: Wed Sep 20, 2023 7:55 pm
Code: Select all
if machine.prototype.allowed_effects == true then
…
end
the problem itself is the machine.prototype.allowed_effects conditional; since it refers to the entity prototype it doesnt result in an error,
Sorry, you're wrong! The condition doesn't result in an error because it's refers to an entity prototype!
but it also seems to never be true regardless of whether or not a machine actually does accept module or beacon effects.
Your base assumption is wrong:
Code: Select all
type(machine.prototype.allowed_effects) == "boolean"
You can check that for yourself, just add this line before the condition check:
Code: Select all
game.print("machine.prototype.allowed_effects: "..serpent.block(machine.prototype.allowed_effects))
If you check the description of
LuaEntityPrototype::allowed_effects, you'll find that its value is a dictionary mapping strings to a boolean. Even if the entity prototype doesn't allow for beaconed effects, machine.prototype.allowed_effects will not be 'false', but an empty table. So what you really want to know is whether that table is empty! You could check for
Code: Select all
table_size(machine.prototype.allowed_effects) > 0
or simply use
Code: Select all
if next(machine.prototype.allowed_effects) then … end