Godot Version
4
Question
I am making a multiplayer game in godot 4. So I have two rpc functions, one for starting a game and one for ending a game. Scene1 is the actual game scene and scene 2 is the game end screen. I call the start game rpc at the start of the game and the end game rpc at the end of the game. For some reason, when I call the end game rpc, it gives the error:Invalid call. Nonexistent function ‘rpc’ in base ‘Nil’. Also, when I print scene1 and 2, scene1 returns null but scene2 is fine. Why is that?II set scene1 in the startgame rpc
func StartGame():
self.hide()
scene1 = load("res://scenes/rooms/Main_menu.tscn").instantiate()
get_tree().root.add_child(scene1)
print(scene1)
@rpc("any_peer","call_local")
func GameFinished():
scene2 = load("res://scenes/rooms/game_finished_screen.tscn").instantiate()
print(scene1,scene2)
#get_tree().root.add_child(scene2)
func _on_host_pressed():
peer = ENetMultiplayerPeer.new()
var error = peer.create_server(port,2)
if error != OK:
print("cannot host" + error)
return
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer)
print("Waiting for players")
func _on_join_button_down():
peer = ENetMultiplayerPeer.new()
peer.create_client(Address,port)
peer.get_host().compress(ENetConnection.COMPRESS_RANGE_CODER)
multiplayer.set_multiplayer_peer(peer)
print(peer)
func _on_create_room_button_down():
StartGame.rpc()
print(peer)
func _process(delta):
if Global.player_health <= 0:
print("game finished")
GameFinished().rpc()`