How to detect if a non-beacon machine can accept modules

Place to get help with not working mods / modding interface.
Post Reply
AlexTheNotsogreat
Long Handed Inserter
Long Handed Inserter
Posts: 96
Joined: Thu May 14, 2015 12:54 am
Contact:

How to detect if a non-beacon machine can accept modules

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

Pi-C
Smart Inserter
Smart Inserter
Posts: 1654
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to detect if a non-beacon machine can accept modules

Post 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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding help”