check if current scene is

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

I was trying to make a current_level variable and was wondering how you would view which is the current scene and change a variable because of it? here, I have some code:

func _process(_delta):
if get_tree().get_current_scene().get_name() == "game":
	current_level = 1

I have this script on autoload also.

Thanks!!

:bust_in_silhouette: Reply From: jgodfrey

What you have looks reasonable, except that you probably don’t want to do it in _process, as that executes every frame. That said, I can’t really suggest an alternative place for the code without more details of what you’re really trying to do.

Also, note that your “scene lookup” code can be somewhat simplified to this:

if get_tree().current_scene.name == "game":

thanks for your reply, but the problem is that it doesn’t seem to be working. does it need to be game.tscn or the path of the scene, or something else? I’m really new to programming and can’t find an answer.

milkiscool | 2020-05-04 02:23

The name, as returned by that code, will be the same name that’s shown in the root node of the scene tree (for the current scene). You can see what that looks like just by printing it out like:

print(get_tree().current_scene.name)

jgodfrey | 2020-05-04 02:27

oooooooooohhhhhhhhh I understand now thanks!!!

milkiscool | 2020-05-04 02:30

1 Like