drawing map from text file

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By OPMRCL

i try to draw a map from a text file. For that read the text file line by line into a dictionary.
Each character in the text file represents a tile. Reading the dictionary works fine, but for some reason it doesn’t draw the tiles.

func drawMap(map):
for key in map.keys():
	var line = map[key]
	for sub in line:
		var s = line.substr(sub,1)
	
		if s.match("2"):
			$Background.set_cell(sub*32,key*32,$Background.find_tile_by_name("brickFore"))
			
		if s.match("0"):
			$Background.set_cell(sub*32,key*32,$Background.find_tile_by_name("alienDirtFore"))
			
func readMapFile(mapSrc, map):
var file = File.new() 
if file.file_exists(mapSrc):
	file.open(mapSrc, File.READ)
	var index = 0
	while not file.eof_reached():
		var line = file.get_line()
		map[str(index)] = line
		index += 1
	file.close()

i would be very grateful, if sombody could help me.

Can you provide you map-file, and a screenshot of you Node-Tree?
Do you call drawMap after reading?
You can set breakpoints, and look at the variables. Maybe they are not what you are expect them to be. The debugger is your friend :wink:

EDIT:

Are you sure you want

set_cell(sub*32, key*32, ...

and not

set_cell(sub, key, ...

?

whiteshampoo | 2020-05-25 11:29

thanks a lot for all your tipps. I figured out, what my problem was. It’s now working.

OPMRCL | 2020-05-25 21:17

:bust_in_silhouette: Reply From: njamster

Replace this

for sub in line:
    var s = line.substr(sub,1)

by this

for sub in line.length():
    s = line[i]