Page 1 of 1

Are fields of "event" table R or RW?

Posted: Wed Aug 24, 2016 8:13 pm
by apriori
I've tried to change item_stack that player gets after mining:

Code: Select all

script.on_event(defines.events.on_player_mined_item, function(event)
  if event.item_stack.name == "my-item" then
    game.players[1].print("Trying to change picked item_stack")
    event.item_stack.set_stack() -- or .clear() or anything else - doesn't matter
  end
end)
All these methods bring into error "field <method name> is nil value". And if I try event.item_stack = nil - nothing happens at all, like this row is not written. How can I change item_stack that player gets? Don't offer me changing of the prototype of the entity being mined.

Or, if I change the result of mining to "blueprint", is it posible to set up that blueprint item?
Here we can see, that the result of mining is LuaItemStack, but almost all fields are nil. Why?

Re: Are fields of "event" table R or RW?

Posted: Wed Aug 24, 2016 9:34 pm
by Nexela
Try attaching it to a player?

Code: Select all

script.on_event(defines.events.on_player_mined_item, function(event)
  if event.item_stack.name == "my-item" then
  player=game.players[event.player_index]
  player.print("Trying to change picked item_stack")
  player.cursor_stack.set_stack({name=coal, count=10})
  end
end)

Re: Are fields of "event" table R or RW?

Posted: Wed Aug 24, 2016 9:46 pm
by Nexela
Another way of doing it just to change stack sizes

Code: Select all

script.on_event(defines.events.on_player_mined_item, function(event)
  if event.item_stack.name == "my-item" then
  player=game.players[event.player_index]
 stack=event.item_stack
stack.count=20
  player.print("Trying to change picked item_stack count")
  -- Shouldnt be needed now -- player.cursor_stack.set_stack({name=coal, count=10})
  end
end)
edit: both of these are untested by me

Re: Are fields of "event" table R or RW?

Posted: Wed Aug 24, 2016 10:03 pm
by apriori
Nexela wrote:Another way of doing it just to change stack sizes

Code: Select all

script.on_event(defines.events.on_player_mined_item, function(event)
  if event.item_stack.name == "my-item" then
  player=game.players[event.player_index]
 stack=event.item_stack
stack.count=20
  player.print("Trying to change picked item_stack count")
  -- Shouldnt be needed now -- player.cursor_stack.set_stack({name=coal, count=10})
  end
end)
edit: both of these are untested by me
Ok I'll test. But:
A) You can mine even if you have another item stack in cursor.
B) I want to get blueprint and setup it.

Re: Are fields of "event" table R or RW?

Posted: Thu Aug 25, 2016 8:53 am
by apriori
Nexela wrote:Try attaching it to a player?

Code: Select all

  ...
  player.cursor_stack.set_stack({name=coal, count=10})
  ...
"try to index field "?", a nil value"

Re: Are fields of "event" table R or RW?

Posted: Thu Aug 25, 2016 9:30 am
by Adil
Event handlers are called by value. The table that you obtain is unique to your mod, you can alter the table if you wish, but as long as usual rules for game objects apply.
With the on_player_mined_item event, I'd guess the trick is that item stacks don't exist, its probably already pushed into player's inventory.
Probably you should use player's reference and inventories api to find added item and take it away and give another one.