CodeX
Published in

CodeX

Object-Oriented Programming (OOP) vs Data-Oriented Tech Stack (DOTS) in Unity

I’m sticking with OOP. At least for now.

If you have been developing using Unity for quite some time, you should have heard of DOTS by now. It is a fundamental change in Unity’s architecture and it also signals a change in how developers think about code and data. This data-oriented design (DoD), an opposite approach to the long-time default standard in many industries object-oriented design enables developers to take advantage of the multicore processors and has proven to increase performance in applications. However, one thing to note is that DOTS is not a direct opposite of OOP because DOTS in Unity encompasses multiple technologies that deliver the data-oriented approach.

Photo by Joshua Aragon on Unsplash

Now, in Unity, DOTS consists of several components working together:

  • The Entity-Component-System (ECS) framework
  • C# Job system for multithreading
  • Burst compiler for optimized native code

DOTS >> OOP

One huge advantage of DOTS compared to OOP is the way data is handled in Unity. Data structures are handled such that cache misses are avoided for the most part and this results in increased speed and efficiency. The separation between data and implementation also makes data organization much more efficient for CPU to access data more easily. Let’s see this in the image below.

The conventional way of the object-oriented approach is structured around creating objects and adding both data and functions to them. To solve the problem of objects belonging to or deriving from the same parent class, OOP concepts like inheritance and encapsulation provide a solution but this comes with a cost of hierarchical restrictions that DOTS fundamentally does not have.

Reasons I’m still sticking with OOP (For now)

Although OOP here does look like it has a cleaner and simpler version of the code, parallelization is hard to achieve with OOP and multicore processors are superbly useful specifically in games or huge softwares. Even with that said, I’m still sticking with OOP for now and I will continue to create classes and put data and functions within them. Few reasons include:

1. Most of the DOTS packages in Unity are still in preview/experimental.

The fact that there are many packages under DOTS in Unity that are still in preview or under the experimental stage gives a hard time to a lot of developers. My advice is that if you are not really good at code, you might want to stick with OOP for now and avoid the hassle of changing your lines of code frequently. Still, it’s worthy to note that Unity moving in this direction is a right step in my opinion. It’s just not the time yet.

2. DOTS has a steep learning curve and I would want to build a complete game using OOP before switching entirely to a different tech stack.

Sample of OOP

I started off Night at the Clownville with the conventional way of OOP approach before I tried my hands on DOTS in Unity. I have to say, it seemed like I entered a whole new universe (Check out the universe below) the first time I tried it. The coding style was vastly different from what has been my longest coding style to date and it simply is a new pattern. Until I can find the pattern, or at least learn a consistent pattern after the packages are stable, I’m sticking with OOP.

Sample of DOTS

3. Unless it is a huge game, sticking with OOP might be the fastest way to develop. Only use DOTS if there are performance bottlenecks unsolvable by OOP.

Closely related to the point above, OOP is the fastest way to develop my game currently since the learning curve is relatively less steep. Undoubtedly there is still a lot to learn about OOP for me, but what I can see is that it might slow me down the moment I need to read into where I can swap my old code out for the new ones. A very important thing to remember is that OOP is not obsolete at the moment and it has successfully been used in countless games/software. DOTS is here to stay but OOP might just still be the default.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store