Search code examples
oopoptimizationabstraction

Is it a good idea to build an API over a game engine's API for the sake of portability?


Let me give some background first for context:

I was interested in building a game with the intention of it being modular. This started as a project in Unity3D. As I was coding the game very closely to Unity3D's API, I wondered, Should I have built my own API for the game in case I wanted/had to move off Unity3D in the future? (to another game engine, or better yet, to use OpenGL/DirectX directly)

I was then interested in moving my game over to Unreal Engine 4, so I tried my best to abstract my game, where most of it explicitly uses Unity3D's API into its own API. An example of a class that still depends on Unity3D is my Player class (GitHub)

    //Vector and GameObject are both Unity3D classes
    private Player(PlayerID id, Vector position)
    {
        GameObject playerPrefab = Resources.Load<GameObject>("Player");

        playerObject = GameObject.Instantiate(playerPrefab, position, Quaternion.identity) as GameObject;
        playerObject.AddComponent<UnityObjectScript>().Bind = this;
        playerObject.name = "Player " + id.ToString();

        coreObject = playerObject.transform.FindChild("Core").gameObject;
        bodyObject = playerObject.transform.FindChild("Body").gameObject;

        players[(int)id] = this;

        UnityLink(playerObject);
    }

Long story short, I gave up abstracting what I already made and decided to rewrite it. Anyways, the point of all this is:

Is it worth it to build my own API over an API that already exists (the game engine)?

While I feel as if there isn't any other alternatives to achieve my 'modular' vision of the game, my main concerns of making an API is:

  • Is the overhead penalty worth it (if any)? Or rather,

  • Is it 'overkill' to have an API built over another API in my case?


Solution

  • Should I have built my own API for the game in case I wanted/had to move off Unity3D in the future?

    You should, if you want to move off your existing game engine in future ;) You should not if you don't ;)

    But adding abstraction layer will certainly slow things down. The degree of slowness depends on your API design.

    Is it 'overkill' to have an API built over another API in my case?

    If you don't want to move to another game engine in future - yes.