Godot Version
4.2
Question
In my online game I am making a chat system. Each online player in the game has a label, send button, and line edit node. Every time the line edit is edited and var called chat_message. When the send button is pressed it calls an RPC that sets the label ($Chat) to chat_message, but for some reason the game always has this happen on the other player. Code:
func _on_line_edit_text_changed(new_text):
chat_message = new_text
func _on_send_pressed():
rpc("update_chat", chat_message)
update_chat(chat_message)
$CanvasLayer/LineEdit.clear()
print("SENT")
@rpc( "any_peer", "reliable") func update_chat(message):
$Chat.text = message
I also have a multiplayer sycronizer that syncs the Chat label’s text.
This is my world spawner script if needed:
extends Node2D
var player_scene = preload("res://Scenes/Character/player.tscn")
var players = 0
@onready var host_manage = $LOBBY/HostManagement
@onready var game = $GameManager
func _ready():
Network.game = self
create_player(Network.unique_id)
func create_player(id):
var p = player_scene.instantiate()
p.name = str(id)
p.global_position = Vector2(0, 0)
p.set_multiplayer_authority(id)
add_child(p)
rpc("add_with_host", p)
add_with_host(p)
@rpc("any_peer", "reliable") func add_with_host(player_in):
if Network.unique_id == 1:
print("I am host")
game.add_player(player_in)
func _physics_process(delta):
if Network.unique_id == 1:
host_manage.visible = true
host_manage.MOUSE_FILTER_STOP
else:
host_manage.visible = false
host_manage.MOUSE_FILTER_IGNORE
func _on_start_pressed():
#print("Start")
rpc("start_game")
start_game()
game.starting_game()
@rpc("any_peer", "reliable")
func start_game():
$StartTimer.start()
host_manage.visible = false
host_manage.MOUSE_FILTER_IGNORE
$LOBBY.visible = false
ItemGained.game_started()
func _on_start_timer_timeout():
ItemGained.attacking()
game.attacking_enabled = true
Thanks for helping me.
