Please Help, Problems with change scene script

Godot Version

3.5

Question

hi there, nice to meet you all… firstly, im not a programmer…
i recently found godot, and very enjoying to learn about how to use it…but as an “non_coding” person, i often have a problems that i cant solve due to skill_issues…
like now ive made an project, (in my opinion) that got work perfectly like what i want to…i have 2 level scene, 1, title scene and 1 for game over scene…
i using basic code “get_tree().change_scene(”“)” to change between scene, but right after ive adding this script on Global.gd(autoload) ;

var stage_level = 0
var pos_player = false

onready var loading_scene = preload(“res://lvl/loading_screen.tscn”)

func _ready():
_reset_stage_level()

func _load_scene(current_scene, next_scene):
#menambahkan loading scene ke root
var loading_scene_instance = loading_scene.instance()
get_tree().get_root().call_deferred(“add_child”, loading_scene_instance)

#menentukan target scenenya
var loader = ResourceLoader.load_interactive(next_scene)
#cek kalau ada bug/error
if loader == null:
	print("error saat meloading level selanjutnya")
	return
	
current_scene.queue_free()

#delay sedikit waktu untuk menampilkan loading scene
yield(get_tree().create_timer(1.1), "timeout")

while true:
	var error = loader.poll()
	
	if error == OK:
		var progress_bar = loading_scene_instance.get_node("Control/ProgressBar")
		progress_bar.value = float(loader.get_stage())/loader.get_stage_count() * 100
		
		
	elif error == ERR_FILE_EOF:
		var scene = loader.get_resource().instance()
		get_tree().get_root().call_deferred("add_child", scene)
		loading_scene_instance.queue_free()
		return
	
	else:
		print("LONGEEEKK RUSAK JUO BAGHU")
		return
	
	yield(get_tree().create_timer(0.5), "timeout")

func _load_level_stage():
match stage_level :
1:
_load_scene(get_tree().current_scene,“res://lvl/lvl1.tscn” )
2:
_load_scene(get_tree().current_scene,“res://lvl/lvl2.tscn” )

ive got an issues with many method that i has implemented before,
like i only can load my save data once,
im using this script for my save_manager.gd;
var bisa_save = false
var save_game_path = “user://savegamecoy.dat”

func save_game():
var file = File.new()
var absolute_path = ProjectSettings.globalize_path(save_game_path)
if file.open(absolute_path, File.WRITE) == OK:
# Simpan data pemain
file.store_var(PlyrStt.player_health)
file.store_var(PlyrStt.player_max_health)
file.store_var(PlyrStt.player_exp)
file.store_var(PlyrStt.player_max_exp)
file.store_var(PlyrStt.player_level)
file.store_var(PlyrStt.player_lives)
file.store_var(PlyrStt.player_posisi)
file.store_var(PlyrStt.player_damage)
file.store_var(Global.stage_level)

	file.close()
	print("DATA TERSIMPAN!")
else:
	print("GAGAL MENYIMPAN DATA")

func load_game():
var file = File.new()
var absolute_path = ProjectSettings.globalize_path(save_game_path)
if file.file_exists(absolute_path):
if file.open(absolute_path, File.READ) == OK:
# Muat data pemain
PlyrStt.player_health = file.get_var()
PlyrStt.player_max_health = file.get_var()
PlyrStt.player_exp = file.get_var()
PlyrStt.player_max_exp = file.get_var()
PlyrStt.player_level = file.get_var()
PlyrStt.player_lives = file.get_var()
PlyrStt.player_posisi = file.get_var()
PlyrStt.player_damage = file.get_var()
Global.stage_level = file.get_var()
file.close()
Global._load_level_stage()
print(“DATA SUKSES DIBACA”)
else:
print(“GAGAL MEMBACA DATA”)
else:
print(“DATA TIDAK DITEMUKAN!”)

func delete_save():
var dir = Directory.new()
var absolute_path = ProjectSettings.globalize_path(save_game_path)
if dir.file_exists(absolute_path):
# Hapus file
var result = dir.remove(absolute_path)
if result == OK:
print(“DATA BERHASIL DIHAPUS”)
else:
print(“GAGAL MENGHAPUS DATA”)
else:
print(“DATA TIDAK DITEMUKAN”)

and this on load button:
func _on_load_pressed():
var file = File.new()

if file.file_exists(SaveManager.save_game_path):
	Global.pos_player = true

if get_tree().paused == true:
	get_tree().paused = false
SaveManager.load_game()
_close_menu()

help guys…i cant figured it out…why the second time I tried pressing the button,
it got error on Line “current_scene.queue_free()” on global.gd

what does the error say?

1 Like

its say 'Attempt to call function ‘queue_free’ in base ‘null instance’ on a null instance…

its refer to line ’ current_scene.queue_free() on 'func _load_scene(current_scene, next_scene) in my global.gd(autoload)

little did i know that script is make a main scene and adding new scene as a child, instead of change scene, like get_tree.change_scene() method that ive been using, so even method get_tree.current_scene.reload() didnt working either…
im following this tutorial btw…
https://youtu.be/5aV_GSAE1kM?si=Y2I049fvQ8Ghnqt7

Where is the method “_reset_stage_level()”?

A easy solution would be to just add a if-statement:

if current_scene:
    current_scene.queue_free()

EDIT: ^This syntax might not work for godot 3:

if current_scene != null:
    current_scene.queue_free()
1 Like

as u can see i have variable “var stage_level = 0”

that was a method ive made so i can track on what level i was,
im using it only for saving and data
file.store_var(Global.stage_level)
and
Global.stage_level = file.get_var

and
func _reset_stage_level():
stage_level = 0

only called in title_screen

ill try it…ty so much for responded my desperate call :slightly_smiling_face:

ill be damn…that one piece of code really fix it,
unbelievable…
ty so much,
but here is another problem arise…
my scene between level1 and level2 now is over lapping … :sweat_smile: what happens…

Can you explain more clearly what the problem is?

1 Like

when i press “load” button to load game, before u fix it, it will crash as u can see above…
after we fix it,
now no more crash/error, and the scene changing exactly to the scene we save our data…but in reality its not really changing ,
as our previous scene still there,
so its may look like in scene level 2 but, under the scene level 2,
we still have level 1 scene…
it makes player still can walk on level1 tilemap although we in level 2

1 Like

please bear with me,
english is not my language…
so sometimes, i cant describe my mind clearly…
forgive me…

1 Like

Put a print here:

print("Current-Scene: ", get_tree().current_scene)
print("Arg-Scene: ", current_scene)
if current_scene != null:
    current_scene.queue_free()
1 Like

its really difficult,
its still not fix it,
ill send u my project tomorrow so u can see it yourself,
but i warning u, my project is full with crap code and messy…because ive only been studying godot and gd script for a week…

My last message wasnt supposed to fix it, it was for debugging. can you show the ouput-tab?

1 Like

sure here it is…

what does “_close_menu” do?

For all intentions and purposes, please do the following:

  • Start the Game
  • Check in the editor, go to the Remote Tab
  • Check your SceneTree for where you are instantiating the levels and adding them to the SceneTree
  • Right now, I believe that during the transition of Level1 → Level2, you are not properly cleaning up your scenes so Level2 is instantiated and shown above of Level1 because you have two level children.

PS: If you use a computer then you should have a button that prints a screenshot of your screen. You can then crop out the portion that you want to show and then upload it.
Here are some guides for windows, mac and linux.

1 Like

Yes because get_tree().current_scene returns null. Otherwise it would queue_free the current_scene

1 Like

i suspect that too…
but due to skill_issues i cant solved it…