Loaded Turrets


Recipes to craft turrets with pre-filled ammo which are ready to fire soon after being placed.

Tweaks
2 months ago
1.1
730
Combat

b Desync Unsafe

2 months ago
(updated 2 months ago)

Hello!

This is a very cool idea for a mod! And very well written code for a first Factorio mod :D Just one small thing, it looks to be multiplayer unsafe due to your usage of global, meaning it could cause desyncs. Most likely, everytime a player joins a MP game it will reset that player's global while control.lua is exectuted, even though some other player might have entries in their global.

Typically you only touch data that can change in runtime inside events, which includes the global table. So something like:

local function init()
    global.insert_on_tick = global.insert_on_tick or {}
    global.pending_unit_tick = global.pending_unit_tick or {}
end

script.on_init(init)
script.on_configuration_changed(init)

Also, a little side note, your on_entity_built might crash if the entity wasn't built from an item. The docs also show that item is optional.

2 months ago

Hi!

Thanks for your kind words and helpful advice! I will gladly take both suggestions. :)

2 months ago

I published a new version that incorporates your fixes. I ended up using "event.stack.name" instead which is always available and is also available in the on_robot_built_entity event since the mod support bots now.

Thanks again, and enjoy!

2 months ago

Nice! Happy to help! On last tiny thing, the event.stack could have valid_for_read == false meaning trying to read the name would crash (pretty sure at least). Large part of Factorio modding is filtering out when you have invalid data, and it's really easy to let things slip through.

2 months ago

Interesting! There really are very many ways you need to cover for data becoming invalid.

In this context I wonder if .stack will ever be invalid in the event handler where it is used to build an entity. The docs seem to indicate that .stack is not optional (as opposed to .item which is): https://lua-api.factorio.com/latest/events.html#on_built_entity

2 months ago

Yeah you are right, the stack will always be there, but the stack could point to something invalid. From the docs for LuaItemstack that I linked:

the item stack is blank but the entity that holds it is still valid.

Honestly not sure how this might happen. Likely be an edge case, like rebuilding an enity using undo in the editor, or something like that. But there's no guarentee that it won't happen.

New response