9

Note that there is nothing wrong with multiple inheritance. The diamond inheritance issue is... a problem. Plain multiple inheritance with no common bases is quite useful. That said, inheritance at all is generally the wrong approach unless you're implementing functional interfaces. Many class hierarchies, including your own, end up looking too much like ...


5

obsessed with OOP What you've described isn't OOP. It's an abuse of inheritance, which is frequently attributed to OOP, but isn't an inherent part of the paradigm. :) My question is, what is the cost of using polymorphism in this way? Virtual function calls are slower than regular calls, and much slower than directly accessing data members, but are not ...


5

No. Grass, Sand and Water aren't different TYPES of tiles, they are different tiles. i.e. class Tile { private: Sprite sprite; bool collidable; bool flamable; bool walkable; public: Tile(Sprite s, bool col, bool flam, bool walk) { this.sprite = s; this.collidable = col; this.flamable = flam; this.walkable = walk; } bool ...


3

Both your possible solutions are unnecessarily complicated or brittle: generally, ill-advised. Polymorphism Your question title is Make the components of an ECS polymorphic but then you don't want to use polymorphism since you state, I basically just want a clean solution. It must not use polymorphism, it should solve the above problem in a neat ...


3

Dependency Injection is able to solve this problem. A top-level object, rather than implementing or overriding a specific process, defines that it requires an object capable of fulfilling it. It also defines what that object is supposed to do, i.e. it defines the component's interface. Then smaller, simpler, and independently implemented objects must ...


3

First of all, CEntity should not have an Intersect method at all. This is simply not its functionality, in your case you are going towards a god CEntity class that knows about everything, when you should usually prefer minimal interface. When designing a class I always strive for these two principles; Single Responsibility Principle Liskov Substitution ...


2

After some research, i found these related posts that helped me out: Generic Singletron Monobehaviour Class Singletrons in Unity But their codes seems to have some overkill features. Cleaning it up a bit, i ended up with this code. I noticed that my difficult with the generic elements was not using the where clause to restrain the derived class. [Code for ...


2

It was an old version of Unity API where you used to be able to access GameObject.rigidbody through the property by default. Now this possibility is considered obsolete, but it is partially preserved for backward compatibility (it called legacy API). Therefore, the compiler says about ambiguity. To resolve the problem, call your variable different from '...


2

Similar to the answer to the question referenced by Philipp, you might consider using a general system of events and flags for every item: CanEquip, OnEquip/OnDeEquip CanConsume, OnConsume IsQuestItem, OnCompleteQuest etc. This is a much more flexible approach as it allows items to serve multiple purposes as would be the case in, for example, the dry ...


2

You aren't using generics correctly. public override float Consider<T>(BaseAiContext context, BaseAction action, T value) { health = value as float; return EvaluateValues(health / maxHealth); } This function takes a generic type T but expects T to be a float. If the function only works if T is a float, the function should not be generic. It's ...


1

I'll just cover the is_key_pressed I proposed in comments. You could have a list of input handlers strategies. It would be a list of key-value pairs. Where they key is… well, a key, that could be pressed. And the value is what happens when the key is pressed. You would have the type of the values be IInputStrategy (or IHandleInput using lolcat interface name ...


1

The OOP way to do this, would be to give the class wall a method handleCollision(ball) (although instead of ball, you might want to use the base-class of ball which represents any object which has a velocity and can bounce off of things). And because you have both horizontal walls and vertical walls which do different things when a ball collides with them, ...


1

In Unity, I'd generally recommend working with the principle of composition over inheritance. In this case, if player characters, enemies, and environment objects all need to take damage, then damage-ability doesn't say much about what the thing is, it's a behaviour each of these things has. So we can model it as a component attached to GameObjects of all ...


1

The way I approached this was to use a constexpr string-hash which I apply to my components at compile-time to assign each of them a unique id. I essentially do this with the use of a macro where I provide a generated UUID string that gets hashed and a name to my macro. Under the hood, this macro adds some static methods allowing me to fetch the the ...


1

First of all, there is no such thing as a good pure ECS. Every ECS system is different, and all of them have their own advantages and disadvantages. Problem #1 I'd go with the following setup. SpriteRenderSystem would have access to an array of SpriteComponents and TransformComponents. Internally, it would also have an array of SpriteRenderNodes with one ...


1

You'll probably have to look into PropertyDrawers to get what you want. These are custom property drawers for Unity. The Unity inspector doesn't know how to render a lot of things by default and it may render things in a way that's not suitable to you, or doesn't have the information you need. Depending on the features you need, you might need to expand ...


1

Ok, as I said in my comment above, I have come to believe my problem is a genuine bug in Unity 4.x. [EDIT] It is still a bug in Unity 5.x. I couldn't wait anymore and I just hacked a non-polymorphic solution. I'm going to not green-check this answer in case some hero out there magically comes up with the right solution, but for the intrepid internet ...


1

C++ is powerful, but it's verbose. If what you want is lots of small polymorphic classes that are all different, then yes, it's going to take lots of source code to declare and define. There's nothing to do about it, really. Now, as ltjax said, what you're doing here isn't exactly polymorphism, at least for the code you've provided. I can't see a common ...


1

I would look into component design http://gameprogrammingpatterns.com/component.html http://www.gamasutra.com/blogs/MeganFox/20101208/88590/Game_Engines_101_The_EntityComponent_Model.php It uses wrapper classes and loads them with reusable components. This has several advantages. First is "flattens" out an inheritance tree. Say you have a vehicle ...


1

It really sounds to me like you want to use multiple inheritance to mix together different pockets of functionality to form something new. While dependency injection can be useful in an inheritance hierarchy, a better option would be to form aggregate object compositions. Using a component based architecture is a simple and natural way to piece together ...


Only top voted, non community-wiki answers of a minimum length are eligible