//Extra message send to server
SpawnCharacterMessage spawnCharMsg = new SpawnCharacterMessage();
spawnCharMsg.assetName = assetName;
spawnCharMsg.lastLocation = ZAccount.Instance.infoCharacter.lastLocation;
Debug.Log("Client send message " + spawnCharMsg.assetName);
//Send message to server
ClientScene.AddPlayer(ZMatchClient.Instance.m_Client.connection,0,spawnCharMsg,);
And on the server-side, it’s going to be like that :
NetworkServer.RegisterHandler(MsgType.AddPlayer, OnClientAddPlayer);
..
..
..
void OnClientAddPlayer(NetworkMessage netMsg)
{
var spawnPlayerMsg = netMsg.ReadMessage<SpawnCharacterMessage>();
var location = spawnPlayerMsg.lastLocation;
var assetName = "Warrior";
Debug.Log("Server : Custom add player message " + location);
//Find spawn point base on lastlocation
GameObject asset = Asset.GetCharacterAssetFromName(assetName);
GameObject player = Instantiate(asset) as GameObject;
NetworkServer.AddPlayerForConnection(netMsg.conn,player,0);
}
But the message content was null ( “location” variable null ).
So, am i wrong anything ? And if i would like to pass extra message via AddPlayer extraMessage param, how to do that ?
It is because the message data for AddPlayer is always of type UnityEngine.Networking.NetworkSystem.AddPlayerMessage
and your custom message is in msgData part.
example to use (send build-in integer message:
void OnAddPlayer(NetworkMessage netMsg)
{
AddPlayerMessage ap = netMsg.ReadMessage<AddPlayerMessage>();
NetworkReader reader = new NetworkReader(ap.msgData);
IntegerMessage im = new IntegerMessage();
im.Deserialize(reader);
}
I know my answer is probably to late, but maybe help someone else.
Hello @Tho654 ,
For someone are find same solution here that what i did.
Instead of calling :
//Send message to server
ClientScene.AddPlayer(ZMatchClient.Instance.m_Client.connection,0,spawnCharMsg,);
I was call :
//Send message to server
ZMatchClient.Instance.m_Client.Send(MsgType.AddPlayer,spawnCharMsg);
As ZMatchClient was declared as Singleton and NetworkClient m_Client, that just instead call built-in AddPlayer() function, i create new message base on MessageBase all call as custom message.
It will generate a warning but it working perfectly. Here that my full custom spawn message :
void OnGameSpawnCharacter()
{
//Load player prefabs from Asset
string assetName = ZAccount.Instance.infoCharacter.assetName;
string lastLocation = ZAccount.Instance.infoCharacter.lastLocation;
playerPrefabs = Asset.GetCharacterAssetFromName(assetName);
NetworkHash128 hashId = playerPrefabs.GetComponent<NetworkIdentity>().assetId;
//Register this prefabs with spawning system
ClientScene.RegisterSpawnHandler(hashId, OnSpawnCharacter, OnDespawnCharacter);
//Set client to be ready
ClientScene.Ready(ZMatchClient.Instance.m_Client.connection);
//Extra message send to server
SpawnCharacterMessage spawnCharMsg = new SpawnCharacterMessage();
spawnCharMsg.assetName = assetName;
spawnCharMsg.lastLocation = lastLocation;
spawnCharMsg.UID = Account.GetAccountUID();
spawnCharMsg.CID = Account.selectedCharacter.CID;
//Send message to server
ZMatchClient.Instance.m_Client.Send(MsgType.AddPlayer,spawnCharMsg);
}