Get EquipmentGrid?

Place to get help with not working mods / modding interface.
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Get EquipmentGrid?

Post by vzybilly »

I'm at abit of a loss for how to get a player's EquipmentGrid.

I've looked at Player, Controller, Game, Force, nowhere is there anything about EquipmentGrid. I was hoping to just call something like player.equipment_grid but as it stands now, I'll have to watch everyone's cursor for a new EquipmentGrid and add it to a table but that seems quite hacky and... stupid...

I hope I'm just overlooking something but at the moment, I don't think I am...
Will code for Food. I also have 11+ mods!
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Get EquipmentGrid?

Post by Ranakastrasz »

It belongs to the item, which belongs to the player inventory, which belongs to the character, which belongs to the player, which belongs to the game.
This is a stripped down version of the gameloop I used to manipulate the equipment in my modular armor mod.

Code: Select all

local thisPlayer = nil
    local players = game.players
        
    for i=1, #players do
        thisPlayer = players[i]
        if (thisPlayer.character) then
            local armor = thisPlayer.get_inventory(defines.inventory.player_armor)[1] -- Check for armour presence.
            if (armor.valid_for_read) then
                
                if (armor.has_grid) then -- Check for grid existence.
                    local grid = armor.grid
                    
                    
                    local energy = 0 -- Total energy and energy capacity
                    local energyCap = 0
                    local shieldHealth = 0 -- Total shield and shield capacity for auto-balancing.
                    local shieldCap = 0
                    for i,equipment in ipairs(grid.equipment) do -- Loop through all equipment.
                        if (equipment.max_energy ~= 0) then
                            energy = energy + equipment.energy -- If it has energy, add values to total value.
                            energyCap = energyCap + equipment.max_energy
                          
                        else
                        
                        end
                        
                        if equipment.max_shield ~= 0 then
                            shieldHealth = shieldHealth + equipment.shield -- Same with shield.
                            shieldCap = shieldCap + equipment.max_shield
                        else
                        end
                    end
                else
                end
            else
            end
        else
            -- No player
        end
    end
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
Rseding91
Factorio Staff
Factorio Staff
Posts: 15879
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Get EquipmentGrid?

Post by Rseding91 »

Just wondering - why do you increment over player indexes instead of simply iterating the players directly?

Code: Select all

for i,player in pairs(game.players) do
  if player.character then
    -- stuff here
  end
end
If you want to get ahold of me I'm almost always on Discord.
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2179
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Get EquipmentGrid?

Post by Ranakastrasz »

Mainly because I am not very good at LUA, and copied existing examples.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
vzybilly
Fast Inserter
Fast Inserter
Posts: 143
Joined: Thu May 14, 2015 6:10 pm
Contact:

Re: Get EquipmentGrid?

Post by vzybilly »

Thanks for the help... it kinda feels weak that there isn't a direct access to the armour slot since there is only one but I guess I'll have to run with this... I'm hoping to be able to get some code made tomorrow but... things might not happen >.>
Will code for Food. I also have 11+ mods!
Post Reply

Return to “Modding help”