Loading models at game startup

Godot Version

4.2.stable

Question

I have a game where I need to buy items such as PC components, but if I create the first item after starting the game, the game hangs for a second while loading the model. I want to load models when I start the game, so if I create an item, it doesn’t load model and the game doesn’t lag.

items_manager.gd

extends Node

@export var items_spawnpoint : Marker3D

var selected_category = null
var selected_item = null

func create_item(item):
	var item_scene = load(item["scene"])
	var item_instance = item_scene.instantiate()
	item_instance.model = load(item["model"]) #"item["model"] is path to model.tscn
	item_instance.rating = item["rating"]
	add_child(item_instance)
	item_instance.transform.origin = items_spawnpoint.transform.origin
	print(str(item))

there always this lag when loading big scene
more noticeable for lower specification devices

the lag usually occur the first time you .load() it

mean, that it only lagging when creating an object for the first time, if I create the same object for the second time, it will not drop framerate, I wanted to ask if there is a way to load all the models when starting the game to avoid loading them when creating the game.

there is, so you make the loading scene and list all the scenes and load from the list
remember this is just load, not instantiate and add_child()

there is also different between basic .load and ResourceLoader.load_threaded_request(_scene_path,"",true)

the latter method is quicker load in terms of time vs basic .load

remember that if you use shader in your scene, that scene will need a flash, meaning create all the scenes that need to be flashed with instantiate and add child, then wait for 0.1 seconds, then queue free it. This to ensure the next time user trying to make the same scene with shader, it can smoothly shown in the game.
why “flash” the shader scene? because it looks like shader will start compiling after it’s visible in game. these whole things about shader compilation first lag has a pull request the: Ubershaders and pipeline pre-compilation (and dedicated transfer queues). by DarioSamo · Pull Request #90400 · godotengine/godot · GitHub that try to fix the lag, but not yet implemented to any version

1 Like

thanks, i made a little script which preloading all scenes

func preload_items():
	for category_data in items_data.values():
		for item_data in category_data:
			var scene_path = item_data["scene"]
			var model_path = item_data["model"]

			if scene_path not in item_scenes:
				var scene = load(scene_path)
				if scene:
					item_scenes[scene_path] = scene
				else:
					print("Error: Failed to preload scene:", scene_path)

			if model_path not in item_models:
				var model = load(model_path)
				if model:
					item_models[model_path] = model
				else:
					print("Error: Failed to preload model:", model_path)

you can also try using its own thread for preloading the scenes/items

var thread_:Thread=Thread.new()
thread_.start(preload_items)
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.