Hi everyone, I have a question about networking and I was hoping If you could help me. I´m trying a multiplayer game and it´s working fine, but now I want to “broadcast” a message between the players. Like sending a JSON or simply sending a string. I was checking the documentation but there is not an example about sending and receiving messages.
Currently this is my code for each player instance :
I´m trying to send a message (which is incrementing a number) when the player jumps. So if the P1 jumps, then the mySyncedValue varible of the P2 should be 2 and again. But is not changing.
Do you want to receive this information on all instances of player? Or on the replicated player on other clients?
If you want to synchronize some room state, you could potentially make a GameManager class with sync fields and hook into PlayerSync to set a referce to itself in all spawned players.
This way all players could call API on the GameManager and that could have simple SyncFields which should make your life much much easier
Hi , thanks for you answer. What do you men whit the replicated player on other clients ? Initially I want to receive this information on all instances of player.
The game manager should start listening to the events onEnable and stop onDisable. That’s because if you would want to destroy the gameobject (game manager) or load another scene, without the stopListening call, it could cause errors.
It’s the exact same as subscribing to OnRoomJoined and other events.
In this context, you don’t need any send or listen subscribes, because the suggestion is to use syncFields, which only have the onValueChange method. (see samples or documentation).
I´m following this example. How do I set GameManager variable on the player with the variable on the handler if in PlayerSync the Player is a prefab ?
Look. This is my set up :
My GameManager object with a GameManagerNetworking script
export class GameManagerNetwork extends Behaviour{
@syncField()
num : number = 0;
update(): void {
console.log(this.num);
}
}
About this step : handle the player, get the player script and set GameManager variable on the player with the variable on the handler
I understand that, I have to add a line @serialiazable(GameObject) gameManager !: GameManager but how do I find that object from my Player Prefabs since PlayerSync?
Refer to the SpawnHandler.ts AND SpawnHandler.cs in the FirstPersonController sample (released) to understand how to handle a spawned instanced by PlayerSync.
So, you just need to declare a method that accepts GameObject and hook that in the inspector. No need for editing the .cs counterpart or to mark your handler as nonSerialized.
Hi again your comments were too useful, thanks for the help. Now , I’m trying to send some data when another player enter or left the room. I’m trying the enter player → send data first. This is my current code and the problem is that when the second player enters, the secondPlayer.localNumeber should be 2 but it doesn’t change. Hope you can help