Are fields of "event" table R or RW?

Place to get help with not working mods / modding interface.
Post Reply
apriori
Filter Inserter
Filter Inserter
Posts: 259
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

Are fields of "event" table R or RW?

Post 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?
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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)

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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

apriori
Filter Inserter
Filter Inserter
Posts: 259
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

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

Post 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.
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

apriori
Filter Inserter
Filter Inserter
Posts: 259
Joined: Thu Feb 18, 2016 8:13 pm
Contact:

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

Post 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"
Any code or mods posted by me are WTFPL, unless otherwise copyrights are specified.

User avatar
Adil
Filter Inserter
Filter Inserter
Posts: 945
Joined: Fri Aug 15, 2014 8:36 pm
Contact:

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

Post 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.
I do mods. Modding wiki is friend, it teaches how to mod. Api docs is friend too...
I also update mods, some of them even work.
Recently I did a mod tutorial.

Post Reply

Return to “Modding help”