0
\$\begingroup\$

I'd like to understand whether the object-oriented paradigm applied to game programming is bad, or whether it's just an urban legend.

A friend of mine is developing a game with HTML5 and Javascript using data-oriented paradigm, and he's trying to convince me about the issues of the object-oriented design.

His best example is this:

"If you have a Tree (a static object in the game) and a Warrior (an object which can perform some action, like movements, attacks..) and suddenly you decide to put in your game a Warrior Tree, how do you do it by object-oriented?"

He is right or he is underestimating the "power" of the object-oriented paradigm?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ There's no need to ultimately choose one way over another. Each has its strengths and they should compliment each other being used in appropriate situations. \$\endgroup\$ Commented Oct 26, 2014 at 9:14
  • \$\begingroup\$ Yes, obviously. But "i want" ad answer to the "Tree Warrior" xD \$\endgroup\$ Commented Oct 26, 2014 at 9:22
  • \$\begingroup\$ You may find the answer you seek here: cowboyprogramming.com/2007/01/05/evolve-your-heirachy \$\endgroup\$ Commented Oct 26, 2014 at 13:10
  • 3
    \$\begingroup\$ OOP doesn't restrict you to inheritance, in a sense OOP != inheritance. Composition is often more useful in cases where you need attributes from two existing classes. Remebmer that classes are just blueprints for your desired objects. To create a TreeWarior, use composition and Interfaces like IPlant and IFight to delegate the needed methods from each one. It is not more difficult. \$\endgroup\$ Commented Oct 26, 2014 at 13:42

2 Answers 2

0
\$\begingroup\$

If your programming language supports multiple inheritance, you can inherit from both the Tree and the Warrior and get a TreeWarrior. C++ supports multiple inheritance:

class TreeWarrior : public Warrior, public Tree{
    ...
};

In Java you can't do this since it doesn't support multiple inheritance. The problem with programming languages like JavaScript is that there is no type safety.

I don't think object oriented programming languages are a limitation. In fact, JavaScript is an object-oriented programming language. I think your friend was thinking of Java as the limitation. Although there are tricks to simulate multiple inheritance in Java, I agree that it is a bit limited.

To answer the question about if he is underestimating the power of the object-oriented paradigm, I think yes. Just think of any AAA game. Most of them are programmed using C++.

\$\endgroup\$
7
  • 2
    \$\begingroup\$ Though inheritance used like this can cause huge problems. \$\endgroup\$ Commented Oct 26, 2014 at 11:20
  • \$\begingroup\$ @Ben, do you mean the diamond problem? \$\endgroup\$ Commented Oct 26, 2014 at 11:24
  • \$\begingroup\$ Interfaces can help a lot when multiple inheritance unavailable/unsuitable. \$\endgroup\$ Commented Oct 26, 2014 at 11:24
  • \$\begingroup\$ @user1754322 Yeah. So, entity-component is used a lot in it's place. Not that inheritance is impossible to do, it's just not as simple as you make it appear in your example. \$\endgroup\$ Commented Oct 26, 2014 at 11:26
  • \$\begingroup\$ @Ben I agree. Those kind of problems will force you to do a hard work on the design part. \$\endgroup\$ Commented Oct 26, 2014 at 11:34
3
\$\begingroup\$

It's not that object oriented paradigms are bad for game development, they do the job just fine. Though, there are downsides to doing everything with a typical OOP mindset.

1) Inflexible, you can't reassemble behaviors and properties of your game objects on runtime as they are tied to fixed class hierarchies.

2) Slower unless you mix OOP with data oriented design. Putting your data in classes potentially loose around the heap is a lot slower to process, while keeping all data of a given domain in a contiguous chunk of memory will provide a lot faster iteration times.

Other than that, if you want to make a full game in the OOP paradigm, feel free to, it has been done countless times before from indie to AAA games and it worked. Chances are you will come to the same conclusions as everyone else and end up switching to data oriented design in full scale too. With this, I mean mostly going into the Entity-Component-System paradigm, as it seems to be the golden standard for today's AAA development.

I personally use a fully fledged ECS setup, and then have a thin layer of OOP referencing entities and components through handles, so I have utility methods to control each type of entity and its data. It works like a charm.

Check the ECS posts here: http://bitsquid.blogspot.pt/ I found them to be really GOOD in many ways for modern development.

\$\endgroup\$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.