[0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post Reply
User avatar
lovely_santa
Filter Inserter
Filter Inserter
Posts: 502
Joined: Sat Feb 18, 2017 9:41 pm
Contact:

[0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by lovely_santa »

Hi,

When having an item that lays down a tile, the on_player_built_tile event fires. When ghosting a tile, the on_build_entity fires that created a tile-ghost. However, the only way to get the name of the actual item that layed down this tile-ghost (as in the item that lays down tiles) is by checking the event.stack. In the case the player doesn't have items, so he just used the filter on his hotbar to lay down ghosts, the stack is obviously not valid to read and there is no way of reconstructing the actual item that was used.

I found this while working on my Land Mover mod, the function in question is on github for this report. So when the event fires but the event.stack is not valid_for_read, I have no way of getting the exact item prototype name of the item used to create that ghost.

EDIT: This code example shows the actual time the item name cannot be extracted (where it prints invalid stack)

Code: Select all

  on_ghost_tile = function(event)
    if event.created_entity.name == "tile-ghost" then
      if event.stack and event.stack.valid_for_read then
        if event.stack.name == "landmover" or event.stack.name == "landmover-mk2" then
          game.players[event.player_index].print{"messages.LM-no-tile-ghost"}
          event.created_entity.destroy()
        end
      else
        game.players[event.player_index].print("invalid stack")
      end
    end
  end,
Kind regards
lovely_santa
You can find all my mods on the mod portal. Also helping on Arch666Angel's mods.
Image

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by kovarex »

The most reliable way to get to just use the LuaEntity::ghost_name method (https://lua-api.factorio.com/0.17.34/Lu ... ghost_name) on the ghost.

So basically using:

Code: Select all

event.created_entity.ghost_name
This should work regardless the way it was built (by with item/ghost building/by blueprint or some other weird way that may be possible in the future.)

User avatar
lovely_santa
Filter Inserter
Filter Inserter
Posts: 502
Joined: Sat Feb 18, 2017 9:41 pm
Contact:

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by lovely_santa »

kovarex wrote:
Wed May 01, 2019 9:18 am
The most reliable way to get to just use the LuaEntity::ghost_name method (https://lua-api.factorio.com/0.17.34/Lu ... ghost_name) on the ghost.
That doesn't work, as that returns the name of the tile, not the item, as I have two items (a mk1 and a mk2) that create the same tile. As this is also a vanilla tile (water tile), there are quite some other mods out there that allow you to randomly place water, which will, again, mess with my mod. Also for future development I need to know when the mk1 and mk2 has been placed. :roll:
You can find all my mods on the mod portal. Also helping on Arch666Angel's mods.
Image

Bilka
Factorio Staff
Factorio Staff
Posts: 3139
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by Bilka »

Moved to modding interface requests.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

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

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by Rseding91 »

https://lua-api.factorio.com/latest/eve ... built_tile
item :: LuaItemPrototype (optional): The item type used to build the tiles
If you want to get ahold of me I'm almost always on Discord.

Bilka
Factorio Staff
Factorio Staff
Posts: 3139
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by Bilka »

Rseding91 wrote:
Wed May 01, 2019 6:47 pm
https://lua-api.factorio.com/latest/eve ... built_tile
item :: LuaItemPrototype (optional): The item type used to build the tiles
Building ghost tiles does not raise this event, it raises on_entity_built which does not provide the item info.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

kovarex
Factorio Staff
Factorio Staff
Posts: 8078
Joined: Wed Feb 06, 2013 12:00 am
Contact:

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by kovarex »

So, the on_built_entity, only provides the stack, but on_player_built_tile contains both the stack, and the item prototype (for cases like when the stack is exhausted I guess).
Wouldn't it be enough to just provide the item prototype in the on_player_built_entity as well?

Ref: on_built_entity: https://lua-api.factorio.com/latest/eve ... ilt_entity

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

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by Rseding91 »

kovarex wrote:
Thu May 02, 2019 9:33 am
So, the on_built_entity, only provides the stack, but on_player_built_tile contains both the stack, and the item prototype (for cases like when the stack is exhausted I guess).
Wouldn't it be enough to just provide the item prototype in the on_player_built_entity as well?

Ref: on_built_entity: https://lua-api.factorio.com/latest/eve ... ilt_entity
That's what I was going to do.
If you want to get ahold of me I'm almost always on Discord.

User avatar
lovely_santa
Filter Inserter
Filter Inserter
Posts: 502
Joined: Sat Feb 18, 2017 9:41 pm
Contact:

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by lovely_santa »

Hi,

I just tried using the new feature, but as the API states:
The item prototype used to build the entity. Note this won't exist in some situations (built from blueprint, undo, ghost-cursor building, etc).
I got this case:
Capture.PNG
Capture.PNG (2.51 MiB) Viewed 1548 times
I don't have items in my inventory, the only thing I have is the ghost on my quickbar. I want to use it to place down some tiles, but I have multiple items that place down the same tile. So the only way to distinguish both items in the placing event (on_built_entity in this case) is by knowing the item prototype name, which I still can't extract.

Code: Select all

  on_ghost_tile = function(event)
    if event.created_entity.name == "tile-ghost" then
      -- now we need to extract the item (name) that was used to build this ghost
      local itemName

      if event.item then
        itemName = event.item.name
      elseif event.stack and event.stack.valid_for_read then
        itemName = event.stack.name
      else
        game.players[event.player_index].print("Cannot extract item used to build this ghost.")
        return -- nothing more we can do about this
      end

      if itemName == "landmover" or itemName == "landmover-mk2" then
        game.players[event.player_index].print{"messages.LM-no-tile-ghost"}
        event.created_entity.destroy()
      end
    end
  end
  
As the API states already, event.item is not present, and neither is there a stack, so it prints the message out that it can't find an itemName.
I've attached the current version of the mod and the save file with the ghosts on the quickbar to try it out.

Kind regards
lovely_santa
Attachments
landmover_save.zip
(2.6 MiB) Downloaded 30 times
LandMover_0.2.6.zip
(110.87 KiB) Downloaded 29 times
You can find all my mods on the mod portal. Also helping on Arch666Angel's mods.
Image

Bilka
Factorio Staff
Factorio Staff
Posts: 3139
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: [0.17.34][modding api] Cannot get item name out of tile ghost in edge case.

Post by Bilka »

Okay, I included "item" in on_built_entity when using the ghost cursor for the next version.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Post Reply

Return to “Implemented mod requests”