Page 1 of 1

Help for noob with .print (or get_tiles?)

Posted: Fri Aug 25, 2017 7:42 pm
by Berkys32
Hello!
Im trying to learn LUA for 2 days, since I found project to work on and somebody told me some basics. I was able to write some really basic codes (about pollution vallues, coords, and so).
Now I made
this
(Its command for getting coords of all chunks).
This prints me much more lines than I can see (as I have many chunks generated on map).
But what I cant understand is why
this
gives me only few names of tiles and not name of every tile of every chunk...

Re: Help for noob with .print (or get_tiles?)

Posted: Fri Aug 25, 2017 8:03 pm
by keyboardhack
I added some comments that explains what the code does.

Code: Select all

/c 
for chunk in game.surfaces[1].get_chunks() do --For all chunks in the map
	--x, y here is the top right position of the chunk
	local x = chunk.x*32
	local y = chunk.y*32
	
	--Print name of tile at position x, y
	local TileName = game.surfaces[1].get_tile({x, y}).name
	game.player.print(TileName)
end
It goes over every chunk and only prints out a single tiles name which is located at the top right of the chunk.
To fix it you need to also iterate over every single x and y position inside the chunk.

Code: Select all

/c 
for chunk in game.surfaces[1].get_chunks() do --For all chunks in the map
	for x = chunk.x * 32, chunk.x * 32 + 32 do
		for y = chunk.y * 32, chunk.y * 32 + 32 do
			local TileName = game.surfaces[1].get_tile({x, y}).name
			game.player.print(TileName)
		end
	end
end

Re: Help for noob with .print (or get_tiles?)

Posted: Fri Aug 25, 2017 8:09 pm
by Berkys32
You solved my another trouble I was trying to get, but what I thought was different:
When I use game.player.print(TilePos), I get much more positions than names when I use game.player.print(TileName). I consider that in both cases there are same amount of tiles, so it should give same amount of prints...

EDIT: Cant even get your improved version working. Even after removing comment Im getting one printed "out-of-map" and then Error: LuaTile API call when LuaTile was invalid...

Re: Help for noob with .print (or get_tiles?)

Posted: Fri Aug 25, 2017 9:04 pm
by Nexela
*.print() has built in spam protection and that is why you are getting fewer print statements. You will either need to use log(), game.write_file(), or print to the console with just print() to bypass these protections. Or appened an incrementing variable to your *.print()

Re: Help for noob with .print (or get_tiles?)

Posted: Sat Aug 26, 2017 12:05 pm
by Berkys32
Thank you Nexela. It was my first thought, but I didnt find anything about it...

Shouldnt code look like this, if chunk.x and chunk.y gives top right corner?

Code: Select all

/c for chunk in game.surfaces[1].get_chunks() do
   for x = chunk.x * 32 - 32, chunk.x * 32 do
      for y = chunk.y * 32 - 32, chunk.y * 32 do
         local TileName = game.surfaces[1].get_tile({x, y}).name
         game.player.print(TileName)
      end
   end
end