Page 1 of 1

How do I insert some used ammo?

Posted: Fri Feb 17, 2017 2:55 pm
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?

Re: How do I insert some used ammo?

Posted: Fri Feb 17, 2017 3:13 pm
by Klonan

Re: How do I insert some used ammo?

Posted: Fri Feb 17, 2017 3:36 pm
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)

Re: How do I insert some used ammo?

Posted: Fri Feb 17, 2017 3:48 pm
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)

Re: How do I insert some used ammo?

Posted: Fri Feb 17, 2017 4:16 pm
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!