0 votes

I have this code:

get_tree().change_scene_to_file("res://scenes/core/Lobby.tscn")
await get_tree().get_current_scene().name == "Lobby"
get_tree().get_current_scene().add_player_to_lobby(id)

My Lobby.tscn looks like this:
Image of Lobby.tscn
and the script attached to the Lobby (root) node is:

extends Node

func add_player_to_lobby(id):
    print("Adding " + str(id) + " to the lobby")

The error says:
"Invalid call. Nonexistent function 'addplayerto_lobby’ in base ‘Control ()'.

And I'm not sure what is going on. It seems to me that I am in fact changing to the lobby scene otherwise the await would not pass, as the error is triggered by the "addplayerto_lobby" line of code. But somehow I'm not able to fetch the script, only the node? If it helps, the lobby script is a normal script and the one I mention above is a singleton (autoload and global variable)

Godot version 4.0.beta8
in Engine by (59 points)
edited by

1 Answer

+1 vote
Best answer

Put a print statement prior to the error line to see what scene you are actually getting:

await get_tree().get_current_scene().name == "Lobby"
print(get_tree().get_current_scene()) 
get_tree().get_current_scene().add_player_to_lobby(id)

Is Lobby a Control node?

by (372 points)
selected by

Okay, I put in a print statement and that says that its actually still in my MainMenu scene. But if I click the remote tab I can see it successfully switched to Lobby? Is await just not waiting long enough maybe? If so, how can I make it actually wait for the scene change to finish?

The call to change scenes is deferred as you already knew.
The new await keyword works with signals and won't work the way it is set up here.
await in the docs (found here):

Changing scenes is a tradeoff process and you should read this.
Using one of the methods in that link:
Preload the scene

var lobby_scene = preload("res://scenes/core/Lobby.tscn")

...

get_tree().change_scene_to_packed(lobby_scene)
var lobby = lobby_scene.instantiate()
lobby.add_player_to_lobby(id)

Thank you, preloading solved it.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.