how to thread a chunk loader

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

hey I have this code here for my spatial node to load gltfs and id like it to run on a thread so the player movement occurs separate from the chunk loading. Therefore, the player wont have to wait for the chunks to load in order to move, which causes, for lack of a better term, lag. when I implement threading though, everything works fine until the resource loader is called to load the gltf. how would I fix this?

    extends Spatial


# Declare member variables here. Examples:
# var a = 2
# var b = "text"

var chunkThread
var mutex
var camera
var oldChunk = -1
var mapX = 0
var mapZ = 0
var offsetX = 0
var offsetZ = 0
var consoleLogger = 0
# Declare member variables here. Examples:
# var a = 2
# var b = "text"
func _input(event):
	if event is InputEventKey and event.pressed:
		if event.scancode == KEY_A:
			mapX += 0.1
			offsetX += 0.1
		if event.scancode == KEY_W:
			mapZ -= 0.1
			offsetZ -= 0.1
		if event.scancode == KEY_S:
			mapZ += 0.1
			offsetZ += 0.1
		if event.scancode == KEY_D:
			mapX -= 0.1
			offsetX -= 0.1
# Called when the node enters the scene tree for the first time.
func _ready():
	camera = get_node("KinematicBody/camDir")
	var ocean = ResourceLoader.load('ocean.gltf')
	ocean.instance()

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	var currChunk = int(mapX/16) + 6 * int(mapZ/16)
	var currX = int(currChunk)%6
	var currZ = int(currChunk/6)
	if (oldChunk != currChunk):
		for x in get_children():
			x.transform.origin.z -= 2*offsetZ
			x.transform.origin.x -= 2*offsetX
		var xNeighbors = [-1,0,1,-1,0,1,-1,0,1]
		var zNeighbors = [-1,-1,-1,0,0,0,1,1,1]
		var loopIndex = 0
		var chunksToLoad = []
		var chunksToRemove = []
		var chunkNeighborsX = []
		var chunkNeighborsZ = []
		for x in xNeighbors:
			var neighborChunkX = currX + xNeighbors[loopIndex]
			var neighborChunkZ = currZ + zNeighbors[loopIndex]
			if (neighborChunkX < 0 || neighborChunkZ < 0 || neighborChunkX > 6 || neighborChunkZ > 6):
				loopIndex += 1
				continue
			var neighborChunk = neighborChunkX + 6*neighborChunkZ
			var chunkExists = ResourceLoader.exists('chunk0' + str(neighborChunk) + '.gltf')
			if (chunkExists):
				chunksToLoad.append(neighborChunk)
				chunkNeighborsX.append(xNeighbors[loopIndex])
				chunkNeighborsZ.append(zNeighbors[loopIndex])
			loopIndex += 1
		for x in get_children():
			loopIndex = 0
			var chunkAlreadyLoaded = 0
			for y in chunksToLoad:
				if (x.name == 'chunk0'+str(y)):
					chunksToLoad.remove(loopIndex)
					chunkNeighborsX.remove(loopIndex)
					chunkNeighborsZ.remove(loopIndex)
					chunkAlreadyLoaded = 1	
				loopIndex += 1
			if (chunkAlreadyLoaded == 0):
				if ('chunk' in x.name):
					chunksToRemove.append(x.name)
		loopIndex = 0
		for x in chunksToLoad:
			var chunk = load('chunk0'+str(x)+'.gltf')
			print('hello')
			var chunkInst = chunk.instance()
			chunkInst.set_name('chunk0'+str(x))
			call_deferred('add_child',chunkInst)
			chunkInst.get_node('BC'+str(x)).create_trimesh_collision()
			var xTranslate = chunkNeighborsX[loopIndex]*16 - mapX - fmod(mapX ,16)
			var zTranslate = chunkNeighborsZ[loopIndex]*16 - mapZ - fmod(mapZ, 16)
			chunkInst.transform.origin.x += xTranslate
			chunkInst.transform.origin.z += zTranslate
			
			loopIndex += 1
		oldChunk = currChunk
		for x in chunksToRemove:
			get_node(x).free()
		offsetX = 0
		offsetZ = 0

What is the problem with the resource loader? Is there an error that’s given?

Ertain | 2021-05-16 04:54