Help! my game freezes when instantiating a new scene

Godot Version

V4.2.1

Question

Hi All, New Godot user here, i have an issue where i have a scene in which the main character changes skins based on certain actions, in this scene the main character TSCN (Slime1) gathers Skeleton parts to become a whole skeleton, that’s when the (Skeleton1) TSCN is instantiated, the problem i have is that once i have gathered all the parts the and the Skeleton TSCN is called there is a freeze in the game for ~ 7 seconds - i do not have any related error messages in the debugger to explain this.
Please Help…

The script is as follows:

extends Node2D

@export var main_character_slime1_tscn: PackedScene
@export var main_character_skeleton1_tscn: PackedScene

var number_of_spawns = 0
var main_character_slime1
var main_character_skeleton1
var slime1_spawned = 0
var skeleton1_spawned = 0

var skeleton_parts_collected : int = 0

func spawn_main_character_slime1():
if (slime1_spawned == 0):
instantiate_main_character_slime1()

func _on_ribcage_body_entered(_body):
if (slime2_spawned == 0):
skeleton_parts_collected += 1
print ("number skeleton parts collected = “, skeleton_parts_collected, " out of 7”)
if (skeleton_parts_collected == 7):
instantiate_main_character_skeleton1.call_deferred()

func _on_pelvis_body_entered(_body):
skeleton_parts_collected += 1
print ("number skeleton parts collected = “, skeleton_parts_collected, " out of 7”)
if (skeleton_parts_collected == 7):
instantiate_main_character_skeleton1.call_deferred()
– # the above repeats for the other 5 parts until we have 7 parts collected

– # instantiate skeleton
func instantiate_main_character_skeleton1():
main_character_skeleton1 = main_character_skeleton1_tscn.instantiate()
get_parent().add_child(main_character_skeleton1)
main_character_skeleton1.position = main_character_slime1.position
print (“Skeleton 1 Created”)
skeleton1_spawned +=1
number_of_spawns += 1
main_character_slime1.queue_free()


Not sure, but if your main_character_skeleton1_tscn is quite large, then the instantiate() is what would pause.

Perhaps, preload the skelington and instantiate it and keep it invisible. Then just add it and show it when you have to.

1 Like

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