Help for noob with .print (or get_tiles?)

Place to get help with not working mods / modding interface.
Berkys32
Fast Inserter
Fast Inserter
Posts: 113
Joined: Mon Mar 07, 2016 9:17 am
Contact:

Help for noob with .print (or get_tiles?)

Post 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...
keyboardhack
Filter Inserter
Filter Inserter
Posts: 478
Joined: Sat Aug 23, 2014 11:43 pm
Contact:

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

Post 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
Waste of bytes : P
Berkys32
Fast Inserter
Fast Inserter
Posts: 113
Joined: Mon Mar 07, 2016 9:17 am
Contact:

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

Post 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...
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

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

Post 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()
Berkys32
Fast Inserter
Fast Inserter
Posts: 113
Joined: Mon Mar 07, 2016 9:17 am
Contact:

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

Post 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
Post Reply

Return to “Modding help”