Page 1 of 1

[Resolved] Identify floor/tile being mined name

Posted: Mon May 08, 2017 3:57 pm
by TheSAguy
Morning,

I'm trying to identify the name of the floor/tile I'm mining, but using my code below I'm only getting the name tile below the floor/tile I'm mining/picking up.

How should I adjust my code to give me the name of the tile being picked up vs. the tile underneath?

Code: Select all

local function Tile_Remove(event)
   

   local player = game.players[event.player_index]
   local robot = event.robot
   local surface = player and player.surface or robot.surface
   local position = event.positions

   for i, position in ipairs(position) do
      local currentTilename = surface.get_tile(position.x,position.y).name
      writeDebug(currentTilename) <-- My function that prints the name

   end   
         
end

local remove_events = {defines.events.on_player_mined_tile, defines.events.on_robot_mined_tile}
script.on_event(remove_events, Tile_Remove)

Thanks.

Re: Identify floor/tile being mined name

Posted: Mon May 08, 2017 4:47 pm
by 321freddy
The events on_player_mined_tile and on_robot_mined_tile are fired AFTER the tile has been mined which is why you only get the name of the tile underneath.
Use on_player_mined_item and on_robot_mined instead. Here is an example:

Code: Select all

script.on_event(defines.events.on_player_mined_item, function(event)
     local player = game.players[event.player_index]
     local tile_name = event.item_stack.name
     local tile = game.item_prototypes[tile_name].place_as_tile_result

     if tile and player.mining_state.mining then
          local tile_position = player.mining_state.position
          game.print("Tile "..tile_name..", pos "..serpent.line(tile_position))
     end
end)
With robots use robot.position as the tile position.

EDIT: code fixed

Re: Identify floor/tile being mined name

Posted: Mon May 08, 2017 6:10 pm
by TheSAguy
Thanks 321freddy, I'll give it a go!
P.S. Worked!

Re: Identify floor name while mining or placing

Posted: Sun Jun 03, 2018 12:48 am
by DeznekCZ
Hi 321freddy,

is nice solution but, i need the name know in every tick and every tick i don't have stack of tiles.

That looks like bigger problem in C++ code.

Thanks for answer

Re: Identify floor name while mining or placing

Posted: Sun Jun 03, 2018 8:31 am
by eradicator
DeznekCZ wrote:is nice solution but, i need the name know in every tick and every tick i don't have stack of tiles.
Not sure why you think you need to know a tiles name every tick...but that's quite easy. Tiles don't change unless an event is fired. So: Read tile name -> tile will have the same name forever or until an event is fired.

Re: [Resolved] Identify floor/tile being mined name

Posted: Sun Jun 03, 2018 9:00 am
by DeznekCZ