"Slowing Down" instantiation

Godot Version

v4.6.1.stable.official

Question

For context, I am currently making a room loading system for my 2D game. To save on performance, I am separating a tilemap into different images composed of 16x16 tile chunks, as some rooms can get quite big. Doing this in editor was expensive on filespace due to how Godot deals with resources that are not saved to files. Small rooms ended up being 2.4 MB, as opposed to about 60 KB when doing it on runtime.

I’m trying to preload rooms from adjacent rooms to lower loading screen time. I’ve gotten the chunk creation system working quite well. However, I need access to the tilemap of the room I’m trying to preload, and that means I have to load the surrounding rooms. I’m already using a ResourceLoader to create threads, but I’ve run into a problem when instantiating. I need to separate the tilemap from the rest of the scene, but using the normal instantiate() function to get access to editing the scene makes large lag spikes when loading larger rooms.

func get_tilemap() -> void:
	node = ResourceLoader.load("res://scenes/rooms/" + room + ".tscn").instantiate()
	var tilemap = node.get_node("Tilemap")

Is there a way to “slow down“ the instantiation function to allow it to run at a rate that will not lag the game, and allow it to be used with await? Or maybe some other solution that I can’t think of?

This sounds odd, are you using “Make Unique” on large resources such as images or tilesets? You can still save those files to separate resources, and binary resources will be smaller. If you save a .tres that stands for text resource, where a .res is a binary resource. Similarly scenes could be saved in a smaller binary as .scn, but this is almost never needed as the resources are what really take up space. There is no reason a .tscn file should take up megabytes.

Do you mean ResourceLoader.load_threaded_request and ResourceLoader.load_threaded_get? Your sample doesn’t match these function calls for background-threaded loading.

You could try to use the Godot’s Thread objects to pick out nodes from an instantiated object, your sample may work well with thread.start(get_tilemap) assuming you return the found tilemap.