on_put_item Event
on_put_item Event
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
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
Last edited by lyravega on Fri Apr 01, 2016 2:58 pm, edited 1 time in total.
-
- Filter Inserter
- Posts: 841
- Joined: Mon Sep 14, 2015 7:40 am
- Contact:
Re: on_put_item Event
You can accomplish this via: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.
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
I thought this would return an error for single items, apparently not. Thanks Supercheese 

Re: on_put_item Event
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
...
Code: Select all
if not item.valid_for_read then return end
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
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.
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
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.
Whenever the placement succeeds, you get on_built_entity event.
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.
I also update mods, some of them even work.
Recently I did a mod tutorial.
-
- Filter Inserter
- Posts: 841
- Joined: Mon Sep 14, 2015 7:40 am
- Contact:
Re: on_put_item Event
Why, for targeting your orbital ion cannons, of course!Adil wrote:Why would you want to use this event anyway. In its current state it is almost completely useless...

Re: on_put_item Event
I use this event, because I need to check for some stuff before the on_built_entity event.
Re: on_put_item 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)lyravega wrote:I use this event, because I need to check for some stuff before the on_built_entity event.
My mods on the Factorio Mod Portal 

Re: on_put_item Event
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...
---
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
@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.
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
There is no item in the event because the item is the players cursor stack.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.
If you want to get ahold of me I'm almost always on Discord.
Re: on_put_item Event
... unless you used the last 1 item, then nothing is in the cursor stack.Rseding91 wrote: Mon Oct 08, 2018 9:19 pmThere is no item in the event because the item is the players cursor stack.
Re: on_put_item Event
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.
If you want to get ahold of me I'm almost always on Discord.
Re: on_put_item Event
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)
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
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.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 want to get ahold of me I'm almost always on Discord.
- eradicator
- Smart Inserter
- Posts: 5211
- Joined: Tue Jul 12, 2016 9:03 am
- Contact:
Re: on_put_item Event
Did a quick test and for me it works correctly even for the last item in a stack:
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.
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
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Re: on_put_item Event
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.