Page 1 of 1

Get EquipmentGrid?

Posted: Thu Sep 24, 2015 7:22 pm
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...

Re: Get EquipmentGrid?

Posted: Thu Sep 24, 2015 7:31 pm
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

Re: Get EquipmentGrid?

Posted: Fri Sep 25, 2015 5:59 pm
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

Re: Get EquipmentGrid?

Posted: Fri Sep 25, 2015 6:55 pm
by Ranakastrasz
Mainly because I am not very good at LUA, and copied existing examples.

Re: Get EquipmentGrid?

Posted: Fri Sep 25, 2015 11:29 pm
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 >.>