Search tile around player position

Place to get help with not working mods / modding interface.
vtx
Fast Inserter
Fast Inserter
Posts: 150
Joined: Tue Jun 28, 2016 9:48 am
Contact:

Search tile around player position

Post by vtx »

From the lua api

game.player.print(tostring(game.player.surface.get_tile(1, 1).collides_with("player-layer")))

To make it work I remove game :

player.print(tostring(player.surface.get_tile(1, 1).collides_with("player-layer")))

When I use something like 2,2 and higher it didn't work. What I do wrong ? To be more accurate it only work once I can't use it more than one time.
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3752
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Search tile around player position

Post by DaveMcW »

If you call the function with numbers, it uses map position.

To check next to the player, call it with the player position and an offset.

Code: Select all

/c game.player.print(tostring(game.player.surface.get_tile(game.player.position.x+1, game.player.position.y).collides_with("player-layer"))) 
vtx
Fast Inserter
Fast Inserter
Posts: 150
Joined: Tue Jun 28, 2016 9:48 am
Contact:

Re: Search tile around player position

Post by vtx »

Thanks for the offset hint.

Sorry I forget to mention I did that in a mod environement.

More specific inside scenario file in on_player_created event.

Still on testing how lua work before dive in code itself.

Code: Select all

  player.print("1,1")
  player.print(tostring(player.surface.get_tile(player.position.x+1,player.position.y+1 ).collides_with("player-layer")))
  player.print("2,2")
  player.print(tostring(player.surface.get_tile(player.position.x+2,player.position.y+2 ).collides_with("player-layer")))
  player.print("3,3")
  player.print(tostring(player.surface.get_tile(player.position.x+3,player.position.y+3 ).collides_with("player-layer")))
  player.print("4,4")
  player.print(tostring(player.surface.get_tile(player.position.x+4,player.position.y+4 ).collides_with("player-layer")))
  player.print("5,5")
  player.print(tostring(player.surface.get_tile(player.position.x+5,player.position.y+5 ).collides_with("player-layer")))
  player.print("6,6")
  player.print(tostring(player.surface.get_tile(player.position.x+6,player.position.y+6 ).collides_with("player-layer")))
Will display

Code: Select all

1,1 
false
2,2
3,3
4,4
5,5
6,6
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Search tile around player position

Post by Nexela »

You are coming up against spam protection
Since "false" would be printed x number of times in a period it only shows once. However the code does run correctly.
vtx
Fast Inserter
Fast Inserter
Posts: 150
Joined: Tue Jun 28, 2016 9:48 am
Contact:

Re: Search tile around player position

Post by vtx »

Oh my god!!! Thanks !

I didn't know there spam protection for print. I'll dive directly into code then.

Is there some tool to help debug code or is just code and hope for the best ?

I know the F4-F7 but not that it'll bring me the information I want or I oversee something.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Search tile around player position

Post by Nexela »

vtx wrote:Oh my god!!! Thanks !

I didn't know there spam protection for print. I'll dive directly into code then.

Is there some tool to help debug code or is just code and hope for the best ?

I know the F4-F7 but not that it'll bring me the information I want or I oversee something.
You can use the log() function to print directly to the factorio-current log file
or
game.write_file() to output to a file in script-output directory
Both of those options are not affected by any spam protection
vtx
Fast Inserter
Fast Inserter
Posts: 150
Joined: Tue Jun 28, 2016 9:48 am
Contact:

Re: Search tile around player position

Post by vtx »

Thanks a lot for the help!
Post Reply

Return to “Modding help”