Ok, I could do most of my stuff and it's working. Still I used Custom Packets for this and maybe this is now the problem I encouter: When players are sending that a player representation should be created for them, only the host is able to use them (i.e. TNObject.isMine is false for other players except the host).
As I'm sending only the data of the player and send create the GameObject out of the data, how can I assign the GameObject to the creating player? Can I do something like this:
private void OnPlayerCreate(Packet response, BinaryReader reader, IPEndPoint source)
{
var playerId = reader.GetInt32();
var newPlayer = Instantiate(playerPrefab);
newPlayer.GetComponent<TNObject>().playerID = playerId;
}
I tried that but it's not working sadly, though the host now only has control over one player. So the question is how could I assign (correctly) the playerId to the TNObject manually and should I do it this way? I thought about using the CreateEx function, as this would assign the playerId automatically as I understand. I would just have to give the same parameters to this function then as I'm giving in my own custom request.
Update:
I changed my code to use the CreateEx function, but still I have the problem that only one player can control the objects. Furthermore now I have the problem that only the last object could be controlled.
For creating the player I use this function of my BattleMgr (doesn't have a TNObject component assigned as I think I don't need one on that):
private void CreatePlayer(Player playerData, string side, string characterName)
{
Debug.Log("create player for side: " + side);
TNManager.CreateEx(1, true, playerMarker.cachedGameObject, side, characterName, );
}
[RCC(1)]
private void CreatePlayerForClient(GameObject prefab, string battleSideString, string characterName)
{
Debug.Log("createplayerforclient");
var newPlayerMarker = Instantiate(playerMarker) as GameObject;
newPlayerMarker.transform.parent = battleElementsParent;
}
For moving the player I use this function in my PlayerMarker MonoBehaviour script (this one has also a TNObject to it assigned):
public void OnGUI()
{
float xPos = 100;
if (battleSide == BattleSide.Left)
xPos = Screen.width - 100f;
if (tno
.isMine && GUI
.Button(new Rect
(xPos,
350,
100,
50),
"up: " + tno
.ownerID)) {
MovePlayer(1);
}
if (tno
.isMine && GUI
.Button(new Rect
(xPos,
400,
100,
50),
"down: " + tno
.ownerID)) {
MovePlayer(-1);
}
}
private void Update()
{
if (tno.isMine)
playerMarker.position = playerPosition;
}
private void MovePlayer(int positionChange)
{
if (tno.isMine)
{
Debug.Log("move player to: " + positionChange);
var writer = TNManager.BeginSend(Packet.MovePlayerRequest);
writer.Write(positionChange);
TNManager.EndSend(true);
}
else
{
Debug.LogError("object is not mine. ismine: " + tno.isMine);
}
}
private void OnMovePlayer(Packet response, BinaryReader reader, IPEndPoint source)
{
if (response == Packet.MovePlayerResponse)
{
var positionChange = reader.ReadInt32();
playerPosition += positionChange;
}
}
The strange thing is that I now can only move one of the PlayerMarkers, but I assumed that I would either control both PlayerMarkers (if both players joined) or none.