on_player_dropped_item How to get item.name

Place to get help with not working mods / modding interface.
Post Reply
hackamod
Long Handed Inserter
Long Handed Inserter
Posts: 62
Joined: Mon Apr 25, 2022 2:39 pm
Contact:

on_player_dropped_item How to get item.name

Post by hackamod »

Code: Select all

script.on_event(defines.events.on_player_dropped_item,
    function(event)
        local droppedItemName = event.entity.stack.name
        ...
        ...
)
This snippet gets the name of the item dropped on the ground, and seems to work as intended as is.
(Let me know if you see something)

1: Is this the best way to determine "what" was dropped?
I want to detect when a player drops wood on the ground for use in making a random campfire to cook on.

2: I found this :
Class LuaItemStack
A reference to an item and count owned by some external entity.

Notes
In most instances this is a simple reference as in: it points at a specific slot in an inventory and not the item in the slot.

In the instance this references an item on a LuaTransportLine the reference is only guaranteed to stay valid (and refer to the same item) as long as nothing changes the transport line.
Which led me to believe this "stack" is the best handle to an item out in the world, laying on the ground...because there is no inventory slot for it...
Is this assumption correct, or is there a better way?

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

Re: on_player_dropped_item How to get item.name

Post by DaveMcW »

hackamod wrote:
Mon Apr 25, 2022 4:53 pm
Which led me to believe this "stack" is the best handle to an item out in the world, laying on the ground...because there is no inventory slot for it...
Correct, every item must be accessed through a LuaItemStack object. You can get this from the .stack property of an event. Or the .stack property of an item-entity.

hackamod
Long Handed Inserter
Long Handed Inserter
Posts: 62
Joined: Mon Apr 25, 2022 2:39 pm
Contact:

Re: on_player_dropped_item How to get item.name

Post by hackamod »

DaveMcW wrote:
Mon Apr 25, 2022 11:04 pm
Correct, every item must be accessed through a LuaItemStack object. You can get this from the .stack property of an event. Or the .stack property of an item-entity.
Thank you for that. I feel better about using it that way then.

This leads to another question:
Why do the notes at the top of the clas say:
In most instances this is a simple reference as in: it points at a specific slot in an inventory and not the item in the slot.
Can someone explain when to use the LuaStack, when not to, and what the difference is... I mean I do not understand what the class notes are getting at...It looks more like a warning to use something else to access an item.
I want to build like this on the ground,
minipit.png
minipit.png (52.56 KiB) Viewed 1611 times
and then light it with a hotkey. Once it "lights" I want to remove the wood and stones, and replace them with a campfire entity I created.
In this situation, (thanks to DaveMcW)I feel like I am on the right track, but I also read that note like a warning for a different situation.
Please forgive my noobish questions as I am just starting the learning curve into Lua and Factorio both, and any details I should already know, I probably do not know yet :D

Xorimuth
Filter Inserter
Filter Inserter
Posts: 624
Joined: Sat Mar 02, 2019 9:39 pm
Contact:

Re: on_player_dropped_item How to get item.name

Post by Xorimuth »

hackamod wrote:
Tue Apr 26, 2022 1:42 am
This leads to another question:
Why do the notes at the top of the clas say:
In most instances this is a simple reference as in: it points at a specific slot in an inventory and not the item in the slot.
Can someone explain when to use the LuaStack, when not to, and what the difference is... I mean I do not understand what the class notes are getting at...It looks more like a warning to use something else to access an item.
No, your approach is fine. Most of the time a LuaItemStack object is referring to an item stack in an inventory, which is what that warning is talking about. But you're not dealing with inventories so your LuaItemStack is different, so you can ignore that message.
My mods
Content: Freight Forwarding | Spidertron Patrols | Spidertron Enhancements | Power Overload
QoL: Factory Search | Remote Configuration | Module Inserter Simplified | Wire Shortcuts X | Ghost Warnings

hackamod
Long Handed Inserter
Long Handed Inserter
Posts: 62
Joined: Mon Apr 25, 2022 2:39 pm
Contact:

Re: on_player_dropped_item How to get item.name

Post by hackamod »

Xorimuth wrote:
Tue Apr 26, 2022 2:11 am
No, your approach is fine.
Thanks, I am feeling ever more confident in using this for my purpose based on the feedback but...*see below*
Xorimuth wrote:
Tue Apr 26, 2022 2:11 am
Most of the time a LuaItemStack object is referring to an item stack in an inventory, which is what that warning is talking about.
The note says :
In most instances this is a simple reference as in: it points at a specific slot in an inventory and not the item in the slot.
I am looking for the correct way to remove the item from the world, permanently, to be replaced with a new item.... and the closest thing I see in LuaItemStack is clear() to clear the stack.
This feels like dumping a bucket but leaving an empty bucket laying around....
Is this the proper way to delete the item_on_ground entity?
I still find it extremely unsettling that:
1: The note at the top of the class warns that it points at a specific slot in an inventory, not the item in the slot in "most" cases
2: The note goes on to list an exception to this rule, and this exception does not describe my use case, but a different situation completely
Can someone explain one of the "most use case" scenarios the note hints to... when it does not refer to the item as it does in my case...
I am trying to get my head around the factorio structure looking through the clouded glass of a new language..... lengthy details appreciated

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

Re: on_player_dropped_item How to get item.name

Post by DaveMcW »

hackamod wrote:
Tue Apr 26, 2022 4:51 am
the proper way to delete the item_on_ground entity?
The same way you delete any LuaEntity.

Code: Select all

/c game.player.selected.destroy()

Pi-C
Smart Inserter
Smart Inserter
Posts: 1646
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: on_player_dropped_item How to get item.name

Post by Pi-C »

hackamod wrote:
Tue Apr 26, 2022 4:51 am
I am looking for the correct way to remove the item from the world, permanently, to be replaced with a new item.... and the closest thing I see in LuaItemStack is clear() to clear the stack.
Look again at the event data you get from on_player_dropped_item:

Code: Select all

entity :: LuaEntity    The item-on-ground entity.
It is an entity, so you can use entity.destroy() to remove it, and entity.surface.create_entity to create a new entity in its place.

Code: Select all

script.on_event(defines.events.on_player_dropped_item, function(event)
    	local player = game.players[player.index]
    	local entity = event.entity
        local droppedItemName = event.entity.stack.name
        
        local surface = entity.surface
        local position = entity.position
        
        if droppedItemName = "wood" then
        	surface.create_entity{name = "new_entity_name", position = position, force = player.force} 
        	entity.destroy()
        end
end)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

hackamod
Long Handed Inserter
Long Handed Inserter
Posts: 62
Joined: Mon Apr 25, 2022 2:39 pm
Contact:

Re: on_player_dropped_item How to get item.name

Post by hackamod »

DaveMcW wrote:
Tue Apr 26, 2022 6:31 am

Code: Select all

/c game.player.selected.destroy()
Thanks for the tip! I was reading a topic about getting mouse position, and that not being possible....so I abandoned the idea of the player having focus, and aimed at keeping a handle on the pile on the ground... but
Does game.player.selected identify an object on the ground under the mouse? I will have to play with this some..


Pi-C wrote:
Tue Apr 26, 2022 6:34 am
Look again at the event data you get from on_player_dropped_item:

Code: Select all

entity :: LuaEntity    The item-on-ground entity.
It is an entity, so you can use entity.destroy() to remove it, and entity.surface.create_entity to create a new entity in its place.
I started there actually, and when I tried entity.name and anything I dropped was named "item_on_ground" instead of "wood" or "stone", I began drilling down through the entity, to the stack, and finally seen words that seemed to identify the object; which is what led to this topic... and your final code solution is how I originally thought it might look, until I didnt see "wood" show up as the name... I suppose I should have just tried destroy at that point instead of being so concerned with the name of the item.
Thanks everybody for the feedback. Your time is appreciated
I must go play with player.selected and see what fun I can have with the open world interaction ideas I have
Thanks again :D

Post Reply

Return to “Modding help”