Hot answers tagged

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 ...


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 ...


4

The simplest solution I can think of is to create a virtual function for whenArrivedAtFinalWaypoint, call that from update, and implement that for the child classes.


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

You're doing it wrong. Making a class for each level? No, levels are data, not code. Make a generic Level class, and then load .level (or whatever) files into it. The files then should just have a list of all the resources that need to be loaded, a list of events/scripts, and so on. Why? Because then you can make a separate level editor, that creates the ...


3

The problem seems to be that if i create a list of Tweens, then call update, it uses Tweens implementation of Tween.update() and not it's inherited children, I don't know how/if it's possible to allow this... Of course it's possible, that's the whole idea of polymorphism. You just need to ensure two things: 1) Mark the Update method as virtual in your ...


2

If by "if the final waypoint is reached, then do class specific action" you mean just calling the "endFunction" then yes, polymorphism will do the rest. That is, if you implement "endFunction" in every subclass or have a default one in the superclass.


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 ...


2

Your entities need ID's in some form or another, if you want to persist them to disk, or pass them over a network. Thus, what you probably want is not to get rid of ID's, but to dynamically generate them so you don't have to set them manually in the code every time you create a new Entity-class. I've done this in my latest C# project by creating a special ...


2

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 ...


1

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 ...


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

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

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 ...


1

I use IDs (for entities, not components), though I could do without them if I didn't want weak referencing for purposes of persistence + lazy loading. Mine do not defeat polymorphism as you describe, though. Try having your IDs provided by a universal registry and accessed via a standard interface.



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