Building a Table at Runtime.

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2124
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Building a Table at Runtime.

Post by Ranakastrasz »

Trying to build a table, using raw data, so as to support multiple mods. (Extracted via "expose-data-raw")
However, it doesn't seem to be working as intended. I can't figure out how to trace the issue due to several issues.
Printing to the player fails semi-randomly, so using it gives false negatives a lot.

Code: Select all


                            for x,equipmentData in ipairs(RanaMods.ModularArmor.equipmentData) do -- Count all scripted equipment
                                    --globalPrint(x)
                                for y,equipmentType in ipairs (equipmentData.equipment) do
Currently, the second line here is claiming that it is an invalid table.

Code: Select all


function registerEquipmentGroup(iGroup)

    --iGroup.mod = iMod
    if not RanaMods.ModularArmor.equipmentData then
        RanaMods.ModularArmor.equipmentData = {}

    end
    if iGroup and iGroup.name and iGroup.type then
        globalPrint(iGroup.name.." "..iGroup.type)
        
        for _, v in pairs(RanaMods.ModularArmor.equipmentData) do
            if v.name == iName then
                return
            end
        end
        table.insert(RanaMods.ModularArmor.equipmentData, iGroup)
        
    else
        return "Invalid Table"
    end
end

function registerPrototype (iGroup,iPrototype,iType)
-- Ideally, this will register equipment group itself, if it doesn't yet exist.
    if not RanaMods.ModularArmor.equipmentData then
        RanaMods.ModularArmor.equipmentData = {}
    end
    if iGroup and iGroup.name and iGroup.type then
        if iPrototype and iPrototype.name and iPrototype.power then

            for _, v in pairs(RanaMods.ModularArmor.equipmentData) do
                if v.name == iGroup.name then

                    thisTable = nil
                    if iType == "equipment" then
                        if not v.equipment then
                            v.equipment = {}
                        end
                        thisTable = v.equipment
                    elseif iType == "fuel" then
                        if not v.fuel then
                            v.fuel = {}
                        end
                        thisTable = v.fuel
                    else
                        globalPrint("Invalid Type "..iType)
                        return "Invalid Type"
                    end
                    
                    
                    for _, data in pairs(thisTable) do
                        if data.name == iPrototype.name then
                            data = iPrototype
                            return
                        end
                    end
                    table.insert(thisTable, iPrototype)
                    
                    break
                end
            end
        else
            return "Invalid iPrototype"
        end
    else
        return "Invalid iGroup"
    end
end


function reset_equipment()
    globalPrint("reset_equipment")
    RanaMods.ModularArmor.equipmentData = {}
end

function refresh_equipment()
    luadata = {raw = loadstring(game.entity_prototypes["DATA_RAW"].order)()}
    reset_equipment()
    for i, equipment in pairs (luadata.raw["battery-equipment"]) do
        if equipment.rana_mod then
            if equipment.rana_mod.powerType then
                -- This will happen repeatedly, but it should work correctly even with duplicate registrations, just failing instead.
                registerEquipmentGroup({name = equipment.rana_mod.powerGroup,type = equipment.rana_mod.powerType})
                registerPrototype({name = equipment.rana_mod.powerGroup,type = equipment.rana_mod.powerType},{name = equipment.name     ,power =  equipment.rana_mod.fuelPower},"equipment")
            end
            -- might have more data attachment types later.
        end
    end
    
    for i, fuel in pairs (luadata.raw["item"]) do
        if fuel.rana_mod then

            if fuel.rana_mod.powerType then
            
                registerEquipmentGroup({name = fuel.rana_mod.powerGroup,type = fuel.rana_mod.powerType})
                registerPrototype({name = fuel.rana_mod.powerGroup,type = fuel.rana_mod.powerType},{name = fuel.name     ,power =  fuel.rana_mod.fuelPower},"fuel")
            end
        end
    end
    
    
    --[[registerEquipmentGroup({name = "conduit",type = "conduit"})
    registerEquipmentGroup({name = "burner" ,type = "fuelled"})
    registerEquipmentGroup({name = "fusion" ,type = "fuelled"})

    registerPrototype({name = "conduit",type = "conduit"},{name = "semi-conduit-conduit-equipment"     ,power =  40 * 1000},"equipment","Modular-Armor")
    registerPrototype({name = "conduit",type = "conduit"},{name = "power-conduit-equipment"            ,power = 720 * 1000},"equipment","Modular-Armor")
    registerPrototype({name = "burner" ,type = "fuelled"},{name = "engine-equipment"                   ,power = 100 * 1000},"equipment","Modular-Armor")
    registerPrototype({name = "fusion" ,type = "fuelled"},{name = "fusion-reactor-equipment"           ,power = 960 * 1000},"equipment","Modular-Armor")
                    ]]--   
    --[[registerPrototype({name = "burner" ,type = "fuelled"},{name = "solid-fuel"  ,power = 25     * 1000 * 1000},"fuel","Modular-Armor")
    registerPrototype({name = "burner" ,type = "fuelled"},{name = "coal"        ,power = 8.     * 1000 * 1000},"fuel","Modular-Armor")
    registerPrototype({name = "burner" ,type = "fuelled"},{name = "raw-wood"    ,power = 4.     * 1000 * 1000},"fuel","Modular-Armor")
    registerPrototype({name = "fusion" ,type = "fuelled"},{name = "alien-fuel"  ,power = 200.   * 1000 * 1000},"fuel","Modular-Armor")
]]--

end
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

Re: Building a Table at Runtime.

Post by Adil »

With that obscure shred of the code, it's really difficult to understand what are you doing here.
Apparently you're sending a wrong table to `for x,equipmentData in ipairs(RanaMods.ModularArmor.equipmentData) do` or `--globalPrint(x)` or `for y,equipmentType in ipairs (equipmentData.equipment) do` or `function registerEquipmentGroup(iGroup)` or whatever it is the second line in your convention of numbering.

And I'm not going to wrap this snippet back into a mod myself just to perform runtime analysis of the data flow (and so might others). Especially, considering how I don't know the data you're tring to feed to it.

As for the problem. There's a whole bunch of `if`s in your function that performs reading of the data.raw. Apparently, they don't interact with the data in the way you'd like them to.
Here's this little guy:

Code: Select all

local debug=function(arg) if type(arg)=='table' then print(serpent.block(arg)) else print(arg) end end
It will help you here. Put it everywhere in `refresh_equipment()` to see how deep the code goes and what goes where.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Building a Table at Runtime.

Post by orzelek »

You can also replace print() with log() and browse through output in log file.

User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2124
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Building a Table at Runtime.

Post by Ranakastrasz »

That should help significantly. being unable to see what is going on is the main issue I have been having.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16

Post Reply

Return to “Modding help”