Gridmap in another thread

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

I created a new thread for background loading, but when I try to run a large loop setting cells for grid map, my game crashes

please show us your code

whiteshampoo | 2020-05-26 07:59

Start loading script

func _start(_param):
	map.create_map()
	
	loading.hide()
	queue_free()
	thread.wait_to_finish()

func _ready() -> void:
	loading = main.instance_loading()
	thread = Thread.new()
	thread.start(self, "_start", null)

Map’s create_map function

func create_map(size: Vector3 = Vector3(128, 1, 128)) -> void:
	loading.total = int(size.x * size.y * size.z)
	loading.progress = 0
	for x in size.x:
		map_data.append([])
		for y in size.y:
			map_data[x].append([])
			for z in size.z:
				map_data[x][y].append([])
				map.set_cell_item(x, y, z, 0)
				loading.progress += 1

thisischico | 2020-05-26 08:06

mh, i’ unsure… maybe you shoud swap

queue_free()
thread.wait_to_finish()

to

thread.wait_to_finish()
queue_free()

(not tested, just a guess)

whiteshampoo | 2020-05-27 09:03

In addition to gridmap, I still have a lot of things loading, and they work fine

thisischico | 2020-05-28 11:27