Page 1 of 1

[SOLVED]on_player_built_tile error

Posted: Tue Aug 25, 2020 12:00 pm
by CruWiT
I want create event when build a tile like stone-path and destroy decoratives. I use this links;
https://lua-api.factorio.com/latest/eve ... built_tile
https://lua-api.factorio.com/latest/Lua ... ecoratives
this is my control.lua;

Code: Select all

script.on_event(defines.events.on_player_built_tile, function(event)
	local surface = game.surfaces[event.surface_index]
	local tiles = event.tiles
	
	for _,tile in pairs(tiles) do
	local tile_position = surface.get_tile(tile.position.x, tile.position.y)
	surface.destroy_decoratives{area={{-0.5, -0.5}, {0.5, 0.5}}, position=tile_position}
	end
end)
and I have this error;

The mod Decorative Removal caused a non-recoverable error.
Please report this error to the mod author.

Error while running event Decorative-Removal::on_player_built_tile (ID 45)
LuaTile doesn't contain key x.
stack traceback:
[C]: in function 'destroy_decoratives'
__Decorative-Removal__/control.lua:7: in function <__Decorative-Removal__/control.lua:1>
stack traceback:
[C]: in ?
[C]: in function 'destroy_decoratives'
__Decorative-Removal__/control.lua:7: in function <__Decorative-Removal__/control.lua:1>

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 12:03 pm
by Klonan

Code: Select all

	local tile_position = surface.get_tile(tile.position.x, tile.position.y)
should be

Code: Select all

	local tile_position = surface.get_tile(tile.position.x, tile.position.y).position
	
But also, why use tile_position, when you are looking up the tile from tile.position?

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 12:35 pm
by CruWiT
Klonan wrote: Tue Aug 25, 2020 12:03 pm

Code: Select all

	local tile_position = surface.get_tile(tile.position.x, tile.position.y)
should be

Code: Select all

	local tile_position = surface.get_tile(tile.position.x, tile.position.y).position
	
But also, why use tile_position, when you are looking up the tile from tile.position?
Error is gone thanks. But decoratives on tiles doesn't remove when place stone-path.

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 2:49 pm
by mrvn
Is the tiles position you get the corner or the center of the tile? This would affect your area.

And maybe make the area a bit bigger so decorations that are outside the tile but overlap it also get removed.

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 3:42 pm
by eradicator
Destroying decoratives doesn't require control stage code. You can just change the prototype.
First mod portal search hit: https://mods.factorio.com/mod/CleanedConcrete

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 3:43 pm
by CruWiT
mrvn wrote: Tue Aug 25, 2020 2:49 pm Is the tiles position you get the corner or the center of the tile? This would affect your area.

And maybe make the area a bit bigger so decorations that are outside the tile but overlap it also get removed.
I try bigger area but doesn't change anything.

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 3:48 pm
by CruWiT
eradicator wrote: Tue Aug 25, 2020 3:42 pm Destroying decoratives doesn't require control stage code. You can just change the prototype.
First mod portal search hit: https://mods.factorio.com/mod/CleanedConcrete
I know thats mod you right but I want create runtime settings for my mod. because sometimes I want on/off destroy decoratives.

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 4:05 pm
by eradicator
TilePosition

Coordinates of a tile in a chunk on a LuaSurface where each integer x/y represents a different tile. This uses the same format as Position except it rounds any x/y down to whole numbers.
Also you need to specify an absolute area on the surface. Not a bounding box relative to the tile.

Code: Select all

/c 

local function show(args)
  rendering.draw_rectangle{
    color = {r=1},
    width = 4,
    filled = false,
    left_top = args.area[1],
    right_bottom = args.area[2],
    surface = args.surface_index,
    time_to_live = 120,
    }
  end

script.on_event({
  defines.events.on_robot_built_tile,
  defines.events.on_player_built_tile,
  },function(e)    
    local surface = game.surfaces[e.surface_index]
    for _,TileAndPosition in pairs(e.tiles) do
      local pos = TileAndPosition.position
      local area={{pos.x,pos.y},{pos.x+1,pos.y+1}}
      surface.destroy_decoratives{area=area}
      show{area=area,surface_index=e.surface_index}
      end  
    end)

Re: on_player_built_tile error

Posted: Tue Aug 25, 2020 8:07 pm
by CruWiT
eradicator wrote: Tue Aug 25, 2020 4:05 pm
TilePosition

Coordinates of a tile in a chunk on a LuaSurface where each integer x/y represents a different tile. This uses the same format as Position except it rounds any x/y down to whole numbers.
Also you need to specify an absolute area on the surface. Not a bounding box relative to the tile.

Code: Select all

/c 

local function show(args)
  rendering.draw_rectangle{
    color = {r=1},
    width = 4,
    filled = false,
    left_top = args.area[1],
    right_bottom = args.area[2],
    surface = args.surface_index,
    time_to_live = 120,
    }
  end

script.on_event({
  defines.events.on_robot_built_tile,
  defines.events.on_player_built_tile,
  },function(e)    
    local surface = game.surfaces[e.surface_index]
    for _,TileAndPosition in pairs(e.tiles) do
      local pos = TileAndPosition.position
      local area={{pos.x,pos.y},{pos.x+1,pos.y+1}}
      surface.destroy_decoratives{area=area}
      show{area=area,surface_index=e.surface_index}
      end  
    end)
it's work now thanks.