Page 1 of 1

Check if X armor is equipped [control.lua][SOLVED]

Posted: Fri Oct 01, 2021 1:55 am
by oldskoolmatt
This is probably a dumb question, but, i can't find the right docs to address the armor slot in control stage.

This is what i'm trying to achieve

Code: Select all

if ARMOR_SLOT contains X_ARMOR then
   -- execute code--

Re: Check if X armor is equipped [control.lua]

Posted: Fri Oct 01, 2021 2:16 am
by Silari
You want get_inventory(inventory) to access the inventory of a Player or Entity. There's a link on that page to the defines list which you use as a parameter to get the inventory you need - defines.inventory.character_armor for the player's armor.

Off the top of my head, should look something like

Code: Select all

playerarmor = player.get_inventory(defines.inventory.character_armor)[1]
if playerarmor and playerarmor.name == "Nameofsomearmor" then
    dostuff
end
AFAIK you can't have more than 1 slot in the armor inventory so indexing it with [1] should be fine, and I think it'll be nil if there's nothing equipped so you need to check for that before trying to access the name.

Re: Check if X armor is equipped [control.lua]

Posted: Fri Oct 01, 2021 2:27 am
by oldskoolmatt
Silari wrote: Fri Oct 01, 2021 2:16 am

Code: Select all

playerarmor = player.get_inventory(defines.inventory.character_armor)[1]
if playerarmor and playerarmor.name == "Nameofsomearmor" then
    dostuff
end
Jesus i did just the same without indexing [1]...

EDIT:
[1] refers to the slot iself so is never nil, [1].name returns the name of the equipped armor, now i'm trying to figure out how to check if there's any armor equipped and post an update as soon as i find a way

Re: Check if X armor is equipped [control.lua]

Posted: Fri Oct 01, 2021 6:54 am
by oldskoolmatt
SOLUTION:
To check if the slot is empty or not, valid_for_read must be used

Code: Select all

local armor = player.get_inventory(defines.inventory.character_armor)[1]

if armor.valid_for_read and armor.name == "ARMOR_NAME" then
    -- EXECUTE CODE --
end