Page 1 of 1

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

Posted: Wed May 01, 2019 8:45 am
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

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

Posted: Wed May 01, 2019 9:18 am
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.)

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

Posted: Wed May 01, 2019 9:41 am
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:

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

Posted: Wed May 01, 2019 9:56 am
by Bilka
Moved to modding interface requests.

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

Posted: Wed May 01, 2019 6:47 pm
by Rseding91
https://lua-api.factorio.com/latest/eve ... built_tile
item :: LuaItemPrototype (optional): The item type used to build the tiles

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

Posted: Wed May 01, 2019 7:03 pm
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.

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

Posted: Thu May 02, 2019 9:33 am
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

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

Posted: Thu May 02, 2019 6:08 pm
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.

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

Posted: Sat May 04, 2019 10:05 am
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 1906 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

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

Posted: Sat May 04, 2019 10:49 am
by Bilka
Okay, I included "item" in on_built_entity when using the ghost cursor for the next version.