Getting the specific bullet-count in a players' active weapon's magazine

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:

Getting the specific bullet-count in a players' active weapon's magazine

Post by KoRaLLL »

Hey guys, I'm trying to get the bullet count in order to determine whether or not a player is shooting.

I was originally running:

Code: Select all

if p.shooting_state.state ~= defines.shooting.not_shooting then
But this triggers even if the player is just holding down SPACE or C - so my logic was to capture their bullet count and see whether or not it is changing.

So what I've done so far, was loop through the players, grabbing their ammo inventory:

Code: Select all

local this_player_ammo = p.get_inventory(defines.inventory.character_ammo).get_contents()
But obviously this just returns string -> int, which is great for magazine count, but I'm trying to capture the bullet count. And from what I'm seeing in the docs, I need to use .ammo on an AmmoItem, I'm just not sure how to get this from a player.

Any help is always appreciated, thank you!

And if there's any better way to determine whether or not a player is actively shooting instead of this method, that's awesome too!!

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Getting the specific bullet-count in a players' active weapon's magazine

Post by Deadlock989 »

The character_ammo inventory is an array of LuaItemStacks, apparently left to right as you look at it in the GUI, with 3 slots in vanilla (I'm not sure if this inventory can have its size changed by mods). You can get the currently selected gun and ammo slot with character.selected_gun_index. Use that as the index of the character_ammo inventory, and then from the resulting LuaItemStack you can access the ammo property to get the remaining bullets.

e.g. to print the first player's selected gun ammo bullet count to the console:

Code: Select all

/c game.print(game.players[1].get_inventory(defines.inventory.character_ammo)[game.players[1].character.selected_gun_index].ammo)
I don't know if there is any better way to find out if something is actually expending bullets or not. I don't think this way would work well for ammo that has 1 "bullet" per ammo item, e.g. rockets.

PS. Also remember that it is possible for players not to have a character (god mode, cutscenes) so you should check they have a valid character before you try to access the character's properties.
Image

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

Re: Getting the specific bullet-count in a players' active weapon's magazine

Post by KoRaLLL »

Deadlock989 wrote:
Tue Sep 13, 2022 10:49 pm
The character_ammo inventory is an array of LuaItemStacks, apparently left to right as you look at it in the GUI, with 3 slots in vanilla (I'm not sure if this inventory can have its size changed by mods). You can get the currently selected gun and ammo slot with character.selected_gun_index. Use that as the index of the character_ammo inventory, and then from the resulting LuaItemStack you can access the ammo property to get the remaining bullets.

e.g. to print the first player's selected gun ammo bullet count to the console:

Code: Select all

/c game.print(game.players[1].get_inventory(defines.inventory.character_ammo)[game.players[1].character.selected_gun_index].ammo)
I don't know if there is any better way to find out if something is actually expending bullets or not. I don't think this way would work well for ammo that has 1 "bullet" per ammo item, e.g. rockets.

PS. Also remember that it is possible for players not to have a character (god mode, cutscenes) so you should check they have a valid character before you try to access the character's properties.
Thank you so much for this, this is exactly what I was looking for. I completely forgot that I didn't actually need to use get_inventory() and that was turning it from LuaItemStack to a table with str -> int. I really appreciate the help!

With this I was able to create a is_shooting var for each player to detect whether or not they're actively shooting their weapon :D

Post Reply

Return to “Modding help”