Page 1 of 1

on_put_item Event

Posted: Wed Mar 16, 2016 12:32 am
by lyravega
This event; "on_put_item" needs to contain one more thing; "item" itself. It currently contains {name, tick, position, player_index} and it really needs "item" in addition to those. The benefit of having "item" would be making a simple item name check to avoid proceeding further.

Also, this event is fired whether or not the action was successful. Another thing, a boolean to check if the action was successful or not would also be helpful. To round the suggestions up:

event.item - LuaItemStack; the item that the player (tried to) put down
event.success - boolean; whether or not the action was successful

Re: on_put_item Event

Posted: Wed Mar 16, 2016 1:07 am
by Supercheese
lyravega wrote:This event; "on_put_item" needs to contain one more thing; "item" itself. It currently contains {name, tick, position, player_index} and it really needs "item" in addition to those. The benefit of having "item" would be making a simple item name check to avoid proceeding further.
You can accomplish this via:

Code: Select all

script.on_event(defines.events.on_put_item, function(event)
	local player = game.get_player(event.player_index)
	local item = player.cursor_stack
...

Re: on_put_item Event

Posted: Wed Mar 16, 2016 3:39 am
by lyravega
I thought this would return an error for single items, apparently not. Thanks Supercheese :)

Re: on_put_item Event

Posted: Fri Apr 01, 2016 2:57 pm
by lyravega

Code: Select all

script.on_event(defines.events.on_put_item, function(event)
   local player = game.get_player(event.player_index)
   local item = player.cursor_stack
...
If anyone is using a similar code above, don't leave it at this. Because it may cause an error if the user is holding LMB down and placing stuff like crazy, after the last item, it'll throw an error.

Code: Select all

if not item.valid_for_read then return end
Add this after the item is defined, to avoid an error.

Which brings me back to the same thing. on_put_item event also containing the LuaItemStack that the player just (tried to) put down, would be nice. Also, whether or not if it was successful, that'd be another thing it may be useful, as right now even trying to put an item down fires this event, even if it wasn't successful. So, to round up the suggestion;

event.item - LuaItemStack; the item that the player (tried to) put down
event.success - boolean; whether or not the action was successful

Re: on_put_item Event

Posted: Sat Apr 02, 2016 3:26 am
by Impatient
i concur. it is helpful if the events offer ALL the objects and informations which are part of the event. even more it is helpful if an event also provides the contextual information about that event. and it is very easy to implement. all the information and the context is known on the side where the event is raised. the only chore is to find the right format for the information provided and document what info is provided and in what format.

my origin is c#.net and there it is a charm. all the information i ever need to handle an event is the information provided by the event or i can get to it via the objects provided by the event. no other information gathering necessary.

Re: on_put_item Event

Posted: Sat Apr 02, 2016 10:46 pm
by Adil
Why would you want to use this event anyway. In its current state it is almost completely useless, except for a convoluted implementation of selection rectangle by blueprint.
Whenever the placement succeeds, you get on_built_entity event.

Re: on_put_item Event

Posted: Sat Apr 02, 2016 11:22 pm
by Supercheese
Adil wrote:Why would you want to use this event anyway. In its current state it is almost completely useless...
Why, for targeting your orbital ion cannons, of course! :D

Re: on_put_item Event

Posted: Sun Apr 03, 2016 2:15 am
by lyravega
I use this event, because I need to check for some stuff before the on_built_entity event.

Re: on_put_item Event

Posted: Sun Apr 17, 2016 10:17 am
by binbinhfr
lyravega wrote:I use this event, because I need to check for some stuff before the on_built_entity event.
It would be nice if in this event we could eventually cancel the forecoming on_built_entity event. Very useful if you use something in your hand to trigger another action, but you don't really want to create an objet on the ground. Or if you want to check some conditions before creating an object (ex: same kind of conditions that refuse the build of a mining drill if there is no resource under it)

Re: on_put_item Event

Posted: Wed Jul 26, 2017 7:06 am
by d3x0r
EDIT: SORRY I was wrong; misread it... on_built_entity is what I thought this was about

---
At this time, entity is part of the event (0.15.30 at least probably earlier)

event.entity.destroy() aborts the event, and stops it from being sent to other things.

---
How do I test if the item has been placed with shift click or normal? in the first case I need to just destroy the item; in the second, I need to also insert it back to the player; but in the first case the item wasn't removed from the player, so it creates extra copies.

This is done because this type of item should only be used when placed with other items. (scrap train stop on scrap rails)

I see - I can trap on_put_item viewtopic.php?f=28&t=48225&p=280766&hil ... nt#p280766 and do a lot of work to figure out what the item is, hopefully the direction the item is, and then check...

Re: on_put_item Event

Posted: Mon Oct 08, 2018 2:07 pm
by Jelmergu
@one of the devs(or moderators)

I don't know who put this in 'Already exists' because it does not belong here. The 'Already exist' forum is for items that already exist in the modding API.
According to the current documentation(0.16.51) no item is 'item' in the event.

The only way to do this now is with a bit of a hack that can easily fail if the cursor_stack has a size of 1. To catch that fail you have to listen to the on_player_cursor_stack_changed event, and remember the last (and current) cursor stack item.

Re: on_put_item Event

Posted: Mon Oct 08, 2018 9:19 pm
by Rseding91
Jelmergu wrote:
Mon Oct 08, 2018 2:07 pm
@one of the devs(or moderators)

I don't know who put this in 'Already exists' because it does not belong here. The 'Already exist' forum is for items that already exist in the modding API.
According to the current documentation(0.16.51) no item is 'item' in the event.

The only way to do this now is with a bit of a hack that can easily fail if the cursor_stack has a size of 1. To catch that fail you have to listen to the on_player_cursor_stack_changed event, and remember the last (and current) cursor stack item.
There is no item in the event because the item is the players cursor stack.

Re: on_put_item Event

Posted: Mon Oct 08, 2018 9:29 pm
by DaveMcW
Rseding91 wrote:
Mon Oct 08, 2018 9:19 pm
There is no item in the event because the item is the players cursor stack.
... unless you used the last 1 item, then nothing is in the cursor stack.

Re: on_put_item Event

Posted: Mon Oct 08, 2018 9:32 pm
by Rseding91
DaveMcW wrote:
Mon Oct 08, 2018 9:29 pm
Rseding91 wrote:
Mon Oct 08, 2018 9:19 pm
There is no item in the event because the item is the players cursor stack.
... unless you used the last 1 item, then nothing is in the cursor stack.
The event won't fire if there's nothing in the cursor as you can't "put item" when there is no item to put.

Re: on_put_item Event

Posted: Thu Oct 11, 2018 5:39 pm
by Jelmergu
It is getting fired though, during development of my there is my ghost mod I had a lot of nil exceptions from the game in my on_put_item event.

So this should be a bug report, where on_put_item gets fired when it should but should not? (documentation says it is fired just before a player builds something, whether the stack is or is not 1 or more)

Re: on_put_item Event

Posted: Thu Oct 11, 2018 5:45 pm
by Rseding91
Jelmergu wrote:
Thu Oct 11, 2018 5:39 pm
It is getting fired though, during development of my there is my ghost mod I had a lot of nil exceptions from the game in my on_put_item event.

So this should be a bug report, where on_put_item gets fired when it should but should not? (documentation says it is fired just before a player builds something, whether the stack is or is not 1 or more)
If you can reproduce it with only your mod yes. If you have mods that are firing the event in an invalid state then it's up to those mods to not do that.

Re: on_put_item Event

Posted: Fri Oct 12, 2018 10:13 am
by eradicator
Did a quick test and for me it works correctly even for the last item in a stack:

Code: Select all

/c
game.player.cursor_stack.set_stack{name='assembling-machine-1',count=1}
script.on_event(defines.events.on_put_item,function(event)
  game.print(game.players[event.player_index].cursor_stack.name)
  end)
-- local pos = game.player.position
-- game.player.build_from_cursor{position={pos.x-5,pos.y}} --except for this
I did notice however that build_from_cursor doesn't fire on_put_item. As the doc states it fires normal events i'm not sure if that is intended.

Re: on_put_item Event

Posted: Fri Oct 12, 2018 7:37 pm
by Jelmergu
Sadly(or rather not that sadly) I cannot reproduce the issue anymore. I do remember having to make a work around for it. I will keep on trying to find the issue again.