How do I insert some used ammo?

Place to get help with not working mods / modding interface.
Post Reply
sore68
Fast Inserter
Fast Inserter
Posts: 123
Joined: Mon May 02, 2016 8:39 am
Contact:

How do I insert some used ammo?

Post by sore68 »

Hi

If ammo magazine_size = 10, I want insert used ammo (1~9/10)

I used two types code

Code: Select all

local inv = entity.get_inventory(1)
game.players[player_index].insert({name = inv[1].prototype.name, count = inv[1].count})
game.players[player_index].insert(inv[1])
But they are return only full ammo (10/10)

Is there a solution for this?


sore68
Fast Inserter
Fast Inserter
Posts: 123
Joined: Mon May 02, 2016 8:39 am
Contact:

Re: How do I insert some used ammo?

Post by sore68 »

Code: Select all

game.players[player_index].get_inventory(1).insert(inv[1])
if game.players[player_index].get_inventory(defines.inventory.player_main).find_item_stack(inv[1].prototype.name) then
game.players[player_index].get_inventory(defines.inventory.player_main).find_item_stack(inv[1].prototype.name).drain_ammo(game.item_prototypes[inv[1].prototype.name].magazine_size - inv[1].ammo)
end
I understand! Thank you :D

and i have 1 question...

How do I apply it to this code?

Code: Select all

entity.surface.create_entity{name = "item-on-ground", position = entity.position, stack = {name = inv[1].prototype.name, count = inv[1].count}}.order_deconstruction(entity.force)

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

Re: How do I insert some used ammo?

Post by Klonan »

sore68 wrote:

Code: Select all

game.players[player_index].get_inventory(1).insert(inv[1])
if game.players[player_index].get_inventory(defines.inventory.player_main).find_item_stack(inv[1].prototype.name) then
game.players[player_index].get_inventory(defines.inventory.player_main).find_item_stack(inv[1].prototype.name).drain_ammo(game.item_prototypes[inv[1].prototype.name].magazine_size - inv[1].ammo)
end
I understand! Thank you :D

and i have 1 question...

How do I apply it to this code?

Code: Select all

entity.surface.create_entity{name = "item-on-ground", position = entity.position, stack = {name = inv[1].prototype.name, count = inv[1].count}}.order_deconstruction(entity.force)
Something like this:

Code: Select all

local item = entity.surface.create_entity
{
  name = "item-on-ground",
  position = entity.position,
  stack = 
  {
    name = inv[1].prototype.name,
    count = inv[1].count
  }
}
item.stack.drain_ammo(1)
item.order_deconstruction(entity.force)

sore68
Fast Inserter
Fast Inserter
Posts: 123
Joined: Mon May 02, 2016 8:39 am
Contact:

Re: How do I insert some used ammo?

Post by sore68 »

Klonan wrote:
sore68 wrote:

Code: Select all

game.players[player_index].get_inventory(1).insert(inv[1])
if game.players[player_index].get_inventory(defines.inventory.player_main).find_item_stack(inv[1].prototype.name) then
game.players[player_index].get_inventory(defines.inventory.player_main).find_item_stack(inv[1].prototype.name).drain_ammo(game.item_prototypes[inv[1].prototype.name].magazine_size - inv[1].ammo)
end
I understand! Thank you :D

and i have 1 question...

How do I apply it to this code?

Code: Select all

entity.surface.create_entity{name = "item-on-ground", position = entity.position, stack = {name = inv[1].prototype.name, count = inv[1].count}}.order_deconstruction(entity.force)
Something like this:

Code: Select all

local item = entity.surface.create_entity
{
  name = "item-on-ground",
  position = entity.position,
  stack = 
  {
    name = inv[1].prototype.name,
    count = inv[1].count
  }
}
item.stack.drain_ammo(1)
item.order_deconstruction(entity.force)

Oh!! Great thank you!!! :D
Thanks to you, I realized the new way!

Post Reply

Return to “Modding help”