0

I'm working on a unity3d project.

Most of my code is loosely coupled to the engine i'm working with. Meaning To move a player i will have an InputProxy, which is Engine dependant, then some engine independent code like InputController, InputManager to eventually call a PlayerManager saying 'MoveForward(Player)' in this case being loosely coupled is not really an issue. Because i will have an EnginePlayer implementation, where the MoveForward call we be translated in relevant engine code.

Now i have the following situation: I want to spawn the avatar of my player in the world over the network.

Apart from a whole lot of other calls this ends in NetworkProxy.SpawnAvatarOnNetwork(INetworkPlayer). You can find the full code below, but the part my question is about this

`var avatarPrefab = player.Client.Player.Avatar.ToEngineAvatar() as GameObject;`

Is this good practice, providing casts in my framework to 'the engine version' or would you do this in another way?

This is the code of SpawnAvatarOnNetwork:

public void InitializeNetworkAwareAvatar(INetworkPlayer player)
    {
        if (player == null)
            throw new ArgumentNullException("avatar"); 

        var uLinkNetworkPlayer = (uLink.NetworkPlayer)player.EngineNetworkPlayer;

        var avatarPrefab = player.Client.Player.Avatar.ToEngineAvatar() as GameObject;

        var appendLoginData = false; // TODO Integrate login data
        object[] initialData = appendLoginData ? uLink.Network.loginData : new object[0];

        uLink.Network.Instantiate(uLinkNetworkPlayer, 
                                    avatarPrefab,
                                    avatarPrefab,
                                    avatarPrefab,
                                    player.Client.Player.Avatar.Position,
                                    player.Client.Player.Avatar.Rotation,
                                    0, //Magical number - No different network groups at the moment
                                    initialData
                                  );
    }
  • 1
    Question: with how many other engines besides Unity are you working with? It sounds like you're trying to do the right thing (loose coupling) in the wrong environment. There really aren't that many other C# engines, nor reasons to use them instead of or together/alternating with Unity. Yet you're going to introduce a lot of overhead and complicate the code-writing process. – CodeSmile Aug 01 '14 at 14:13
  • Thanks for the feedback, none actually. It's out of habit when i code that i don't want to be depending on a third party framework. I believe you're making a fair point regarding Unity (Although Mono and XNA or the first things i think about, but can't make a strong argument) but i believe my case still stand when it comes to the network engine, for example i am now using uLink by MuchDifferent, but could change to Photon on a later stage. That's why i'm passing my own implementation of INetworkPlayer and not uLink.NetworkPlayer – Chaverbeke Aug 01 '14 at 15:33
  • I just remembered another reason why i am doing this: Unit testing, if i have for example a player class that is inheriting from Monobehavior i can't use my 'new' keyword for initialisation and that's not playing nicely together with Moq either – Chaverbeke Aug 01 '14 at 15:54
  • Apparently i am doing what this blog post is saying: http://blogs.unity3d.com/2014/06/03/unit-testing-part-2-unit-testing-monobehaviours/ – Chaverbeke Aug 01 '14 at 16:03
  • For unit testing and networking I can understand it, though for networking a common wrapper class (or two, three) seems to suffice in most cases. – CodeSmile Aug 01 '14 at 17:48
  • Alright, i'll continue like this then. Thank you for your feedback! – Chaverbeke Aug 02 '14 at 12:25

0 Answers0