on_put_item Event

Things that already exist in the current mod API
Post Reply
lyravega
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Mon Jan 06, 2014 4:41 pm
Contact:

on_put_item Event

Post 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
Last edited by lyravega on Fri Apr 01, 2016 2:58 pm, edited 1 time in total.

Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: on_put_item Event

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

lyravega
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Mon Jan 06, 2014 4:41 pm
Contact:

Re: on_put_item Event

Post by lyravega »

I thought this would return an error for single items, apparently not. Thanks Supercheese :)

lyravega
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Mon Jan 06, 2014 4:41 pm
Contact:

Re: on_put_item Event

Post 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

User avatar
Impatient
Filter Inserter
Filter Inserter
Posts: 880
Joined: Sun Mar 20, 2016 2:51 am
Contact:

Re: on_put_item Event

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

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

Re: on_put_item Event

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

Supercheese
Filter Inserter
Filter Inserter
Posts: 841
Joined: Mon Sep 14, 2015 7:40 am
Contact:

Re: on_put_item Event

Post 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

lyravega
Long Handed Inserter
Long Handed Inserter
Posts: 66
Joined: Mon Jan 06, 2014 4:41 pm
Contact:

Re: on_put_item Event

Post by lyravega »

I use this event, because I need to check for some stuff before the on_built_entity event.

User avatar
binbinhfr
Smart Inserter
Smart Inserter
Posts: 1524
Joined: Sat Feb 27, 2016 7:37 pm
Contact:

Re: on_put_item Event

Post 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)
My mods on the Factorio Mod Portal :geek:

d3x0r
Filter Inserter
Filter Inserter
Posts: 316
Joined: Sun Jun 04, 2017 8:56 am
Contact:

Re: on_put_item Event

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

Jelmergu
Long Handed Inserter
Long Handed Inserter
Posts: 77
Joined: Mon Apr 04, 2016 8:49 am
Contact:

Re: on_put_item Event

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13175
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_put_item Event

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3699
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: on_put_item Event

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13175
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_put_item Event

Post 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.
If you want to get ahold of me I'm almost always on Discord.

Jelmergu
Long Handed Inserter
Long Handed Inserter
Posts: 77
Joined: Mon Apr 04, 2016 8:49 am
Contact:

Re: on_put_item Event

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

Rseding91
Factorio Staff
Factorio Staff
Posts: 13175
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_put_item Event

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: on_put_item Event

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

Jelmergu
Long Handed Inserter
Long Handed Inserter
Posts: 77
Joined: Mon Apr 04, 2016 8:49 am
Contact:

Re: on_put_item Event

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

Post Reply

Return to “Already exists”