how to detect crrent scene godot
func _process(delta float64):
var currentScene = get_tree().get_current_scene()
if currentScene != null:
print("Current Scene: ", currentScene.get_name())
else:
print("No scene currently active")
Explanation:
1. func _process(delta float64):
- This function is called every frame. It allows continuous monitoring and updating of the game state.
var currentScene = get_tree().get_current_scene()
- Retrieves the current scene that is active within the game using theget_current_scene()
method from the scene tree (get_tree()
).if currentScene != null:
- Checks if there is an active scene. IfcurrentScene
is not null (i.e., a scene is active), it proceeds with the next step.print("Current Scene: ", currentScene.get_name())
- Prints the name of the current scene usingget_name()
. This line will output the name of the active scene to the console.else:
- If there is no active scene (i.e.,currentScene
is null), this block executes.print("No scene currently active")
- Outputs a message indicating that there is no active scene to the console.