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

Place to get help with not working mods / modding interface.
Post Reply
User avatar
oldskoolmatt
Inserter
Inserter
Posts: 21
Joined: Wed Nov 11, 2020 10:23 am
Contact:

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

Post 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--
Last edited by oldskoolmatt on Fri Oct 01, 2021 6:54 am, edited 1 time in total.
Image

User avatar
Silari
Filter Inserter
Filter Inserter
Posts: 490
Joined: Sat Jan 27, 2018 10:04 pm
Contact:

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

Post 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.

User avatar
oldskoolmatt
Inserter
Inserter
Posts: 21
Joined: Wed Nov 11, 2020 10:23 am
Contact:

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

Post 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
Image

User avatar
oldskoolmatt
Inserter
Inserter
Posts: 21
Joined: Wed Nov 11, 2020 10:23 am
Contact:

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

Post 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
Image

Post Reply

Return to “Modding help”