table not being populated

Place to get help with not working mods / modding interface.
Skybeach88
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sun May 28, 2017 11:10 pm
Contact:

table not being populated

Post by Skybeach88 »

Hi everyone, I have been working on a mod, as part of it I am trying to generate a table that contains only the entities that can be placed by a player and also be blueprinted, I have been running into issues of the game telling me that "minable" or "placeable_by" are not part of the LuaEntityPrototype, so I opted to check for a flag and only copy entities that do NOT have the flag "not_blueprintable" that seems to get me through the function but it is not actually copying any information and populating my table, Here is the code I am using, I am at a loss as to what is happening, so any advice would be most appreciated

Code: Select all

-- Function to populate the placeableEntities table
local function populatePlaceableEntities()
    for _, prototype in ipairs(game.entity_prototypes) do     --I have tried pairs and ipairs here, ipairs seems to get through the function, but pairs errors out
        -- Check if the entity is placeable and does not have the "not-blueprintable" flag
        if not prototype.flags["not-blueprintable"] then
            -- Create a copy of the entity with only necessary fields
            local entityCopy = {
                name = prototype.name,
                selection_box = prototype.selection_box,
            }
            table.insert(placeableEntities, entityCopy)     --This seems to be where pairs errors out at.
        end
    end
end
I apologize if this is a noobie question, This is the second mod I have made for factorio, and the first one that has custom actions, the first one was just creating a new production chain. I am working on a fairly ambitious project and currently do not know Lua as well as I probably should, (i know python and java though), thanks for any input you can give me!
Pi-C
Smart Inserter
Smart Inserter
Posts: 1725
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: table not being populated

Post by Pi-C »

Skybeach88 wrote: Tue Nov 14, 2023 11:52 am

Code: Select all

-- Function to populate the placeableEntities table
local function populatePlaceableEntities()
    for _, prototype in ipairs(game.entity_prototypes) do     --I have tried pairs and ipairs here, ipairs seems to get through the function, but pairs errors out
See the description of game.entity_prototypes: It's a table of prototypes, indexed by prototype.name. Using ipairs will NOT throw an error because it can only used with arrays with indexes that are consecutive numbers starting with 1 (i.e. 1, 2, 3, …). As your table is indexed by strings, the loop will be skipped.

Code: Select all

            table.insert(placeableEntities, entityCopy)     --This seems to be where pairs errors out at.
What exactly is the error message? Did you define a table placeableEntities? Does the prototype where the crash occurs have any flags? According to the description of EntityPrototype::flags, this is optional, so it may be nil -- and trying to index a nil value will fail.

In order to find the error, I'd log the value of prototype.flags. I'm pretty sure that the last value before you get the crash will be nil.

Code: Select all

local function populatePlaceableEntities()
    for _, prototype in pairs(game.entity_prototypes) do     -- Use pairs!
        log(string.format("Flags of %s:\t%s", _, prototype.flags))
    
        -- Check if the entity is placeable and does not have the "not-blueprintable" flag
--        if not prototype.flags["not-blueprintable"] then
        if not (prototype.flags and prototype.flags["not-blueprintable"]) then
            -- Create a copy of the entity with only necessary fields
            local entityCopy = {
                name = prototype.name,
                selection_box = prototype.selection_box,
            }
            table.insert(placeableEntities, entityCopy)     --This seems to be where pairs errors out at.
        end
    end
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”