After 3 days tinkering with Unity networking wtih no networking knowledge

1    09 Jul 2019 21:22 by u/The_Venerable

I have finally added multiplayer to my RPG. It's barebones right now but every player that joins is assigned their name, model, and stats which is synced across all players. I knew networking was going to be difficult, but HOLY SHIT. This ups the difficulty up exponentially. Although now that I have a basic understanding of it, I hope future coding won't be as brutal. The only thing I worry about is losing touch with my code. Take this simple line for instance.

            GameObject playerModel;
            playerModel = Instantiate(model) as GameObject;
            playerModel.transform.position = player.transform.position;
            playerModel.transform.rotation = new Quaternion(0, 0, 0, 0);
            playerModel.transform.Rotate(0, 90, 0);
            playerModel.transform.SetParent(player.transform);

turned into

[Command]
 void CmdSpawnModel(string name)
    {
        playerName = name;
        GameObject[] players = GameObject.FindGameObjectsWithTag("Other Player");
        for (int i = 0; i < players.Length; i++)
        {
            if (players[i].GetComponent<Player>().playerName == playerName)
            {
                player = players[i].GetComponent<Player>();
            }
        }
        model = Instantiate(playerModel);
        model.name = player.name;
        model.transform.position = player.transform.position;
        model.transform.rotation = new Quaternion(0, 0, 0, 0);
        model.transform.Rotate(0, 90, 0);
        model.transform.SetParent(player.transform);
        model.tag = "Playermodel";
        NetworkServer.SpawnWithClientAuthority(model, player.gameObject.GetComponent<NetworkIdentity>().connectionToClient);
        RpcSpawnModel(playerName);
    }
[ClientRPC]
    void RpcSpawnModel(string nameOfPlayer)
    {
        if (hasAuthority)
        {
            players = GameObject.FindGameObjectsWithTag("Player");
        }
        else
        {
           players = GameObject.FindGameObjectsWithTag("Other Player");
        }
        for (int i = 0; i < players.Length; i++)
        {
            if (players[i].GetComponent<Player>().playerName == nameOfPlayer)
            {
                player = players[i].GetComponent<Player>();
            }
        }
        GameObject[] findalldefaultmodels = GameObject.FindGameObjectsWithTag("Playermodel");
        for (int i = 0; i < findalldefaultmodels.Length; i++)
        {
            if (findalldefaultmodels[i].GetComponentInParent<Player>() == null)
            {
                model = findalldefaultmodels[i];
            }
        }
        model.name = player.name;
        model.transform.position = player.transform.position;
        model.transform.rotation = new Quaternion(0, 0, 0, 0);
        model.transform.Rotate(0, 90, 0);
        model.transform.SetParent(player.transform);

with a bit more more code in other scripts to make it work right. What a bitch.

2 comments

0

Annotate!

Very cool, man.

0

Comment your code. Add explanations for why you did what you did, and perhaps add TODO: comments for future updates, sanitation, cheat detection, and up-scaling.

You could also put any duplicate code in it's own procedure or function, and call it when you need to. That way if you want to change or edit it, or only have to do it in one place.

You'll know you're a coder when more fun to code the game than it is to play it. Good luck!