Trying to get the active weapon that a player is using?

Place to get help with not working mods / modding interface.
Post Reply
KoRaLLL
Burner Inserter
Burner Inserter
Posts: 15
Joined: Sun Sep 11, 2022 7:22 pm
Contact:

Trying to get the active weapon that a player is using?

Post by KoRaLLL »

Hey all,

I was just wondering how you'd go about 'detecting' the current weapon that a player is using?

I figured it would be something like:

Code: Select all

local weapon = p.get_inventory(defines.inventory.character_guns).get_contents()
Where p = player - but this returns 'Unknown key: "1"'

Thanks

KoRaLLL
Burner Inserter
Burner Inserter
Posts: 15
Joined: Sun Sep 11, 2022 7:22 pm
Contact:

Re: Trying to get the active weapon that a player is using?

Post by KoRaLLL »

I realised that I was using print() instead of serpent.block to output this.

So I'm one step closer, however, the real output is something like:

Code: Select all

}
  65.917 Script @__code__/control.lua:19: {
  ["combat-shotgun"] = 1,
  ["pistol"] = 1,
  shotgun = 1
}
How would I be able to get which of these is the active item in the players hand?

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

Re: Trying to get the active weapon that a player is using?

Post by Silari »

It's part of the character entity: https://lua-api.factorio.com/latest/Lua ... _gun_index . You can get the player's character, if any, using the LUAPlayer.character attribute.

Be sure to check if the player has a character first - they may not if they are using sandbox, or mods. If the player doesn't have a character they won't have any guns.

KoRaLLL
Burner Inserter
Burner Inserter
Posts: 15
Joined: Sun Sep 11, 2022 7:22 pm
Contact:

Re: Trying to get the active weapon that a player is using?

Post by KoRaLLL »

Silari wrote:
Sun Sep 11, 2022 10:42 pm
It's part of the character entity: https://lua-api.factorio.com/latest/Lua ... _gun_index . You can get the player's character, if any, using the LUAPlayer.character attribute.

Be sure to check if the player has a character first - they may not if they are using sandbox, or mods. If the player doesn't have a character they won't have any guns.
Ahh - this makes perfect sense, thank you.

Do you have any guidance on navigating the docs? I spent hours googling things like 'factorio player active weapon modding' 'player active item factorio' and nothing came up.

I'm struggling to find the right pointers within the docs, and as far as I'm aware there's no search functionality

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Trying to get the active weapon that a player is using?

Post by Klonan »

KoRaLLL wrote:
Mon Sep 12, 2022 8:20 am
I'm struggling to find the right pointers within the docs, and as far as I'm aware there's no search functionality
Search is here:
Attachments
firefox_rwdAkVhFyP.png
firefox_rwdAkVhFyP.png (122.84 KiB) Viewed 1152 times

Bilka
Factorio Staff
Factorio Staff
Posts: 3129
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Trying to get the active weapon that a player is using?

Post by Bilka »

KoRaLLL wrote:
Mon Sep 12, 2022 8:20 am
Do you have any guidance on navigating the docs? I spent hours googling things like 'factorio player active weapon modding' 'player active item factorio' and nothing came up.

I'm struggling to find the right pointers within the docs, and as far as I'm aware there's no search functionality
There is a search bar for most objects, methods and attributes on the left, in the sidebar.

You can also open https://lua-api.factorio.com/latest/Classes.html and use your browser's search function (usually under ctrl +f) to search through all objects, methods, attributes and their short descriptions. Since the short descriptions are on the page, it tends to be more useful than the sidebar search. E.g. for the question here you could search for "weapon" on the Classes page and find it right away.
Screenshot_20220912_103550.png
Screenshot_20220912_103550.png (344.16 KiB) Viewed 1152 times
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

KoRaLLL
Burner Inserter
Burner Inserter
Posts: 15
Joined: Sun Sep 11, 2022 7:22 pm
Contact:

Re: Trying to get the active weapon that a player is using?

Post by KoRaLLL »

Klonan wrote:
Mon Sep 12, 2022 8:38 am
KoRaLLL wrote:
Mon Sep 12, 2022 8:20 am
I'm struggling to find the right pointers within the docs, and as far as I'm aware there's no search functionality
Search is here:
You are a life saver. Thank you so much!
Bilka wrote:
Mon Sep 12, 2022 8:38 am
KoRaLLL wrote:
Mon Sep 12, 2022 8:20 am
Do you have any guidance on navigating the docs? I spent hours googling things like 'factorio player active weapon modding' 'player active item factorio' and nothing came up.

I'm struggling to find the right pointers within the docs, and as far as I'm aware there's no search functionality
There is a search bar for most objects, methods and attributes on the left, in the sidebar.

You can also open https://lua-api.factorio.com/latest/Classes.html and use your browser's search function (usually under ctrl +f) to search through all objects, methods, attributes and their short descriptions. Since the short descriptions are on the page, it tends to be more useful than the sidebar search. E.g. for the question here you could search for "weapon" on the Classes page and find it right away.
Screenshot_20220912_103550.png
Yeah, CTRL + F'ing is what I've been doing, but obviously it meant that I glanced over things like in my original request.

Thank you both so much for your guidance here!

Post Reply

Return to “Modding help”