Implementing Interfaces in c++?

Hey all, I can’t seem to figure this out. I have a BaseTest class which is inheriting from my FunctionInterface class. I want to implement the functions I defined in BaseTest in a class called MasterCube. Any thoughts on this would be awesome. I think I’m close, but this is preventing me from moving forward and I need to make progress soon.

in MasterCube.h

ABaseTest* BaseTestptr;

IFunctionInterface* Functions = Cast<IFunctionInterface>(BaseTestptr);

It compiles fine, but I always crashes on Engine launch

You should provide more code for what you’re trying to accomplish, but if you’re trying to implement a parent interface-child class type relationship, look into this article:

I think you might be confused about what an interface is, unless I’m missing some UE4 specific thing.

I’m pretty sure this is what I’m looking to do. I know that what I really want is a component-entity type pattern, but because of Unreal’s syntax/macros, I’m pretty sure that this is the way to implement that kind of structure? I could be wrong since I’m new to C++, and Unreal’s API is bonkers to me.

I found this: Interface vs Component implementation - C++ - Unreal Engine Forums which makes me think that I need to create a component based system, but even that I’m unsure of how to do.

Any help would be great…

Could you provide more detail in what you’re trying to accomplish? And what is your language background? Maybe we can find some common ground to figure this out.

TBH I don’t know much coding at all. A little python from a few years ago, but that’s about it. Logically, I have an easy time grasping concepts, I’m just too new to really understand them well yet.

For this particular use case though, I need to be able to define a whole mess of functions (that, like the component method, can be broken into subsets like a movement component, animation component, etc). Those functions will be called by many different classes, and will use them in different ways. So lets say ClassA and ClassB both take the function Move(); to move, but ClassB and ClassC have the function DoSomethingElse();. I want both Move(); and DoSomethingElse(); to be components so they’re reusable and interchangeable.

Was I correct with what I was trying to do with the interfaces, or is there a better way to implement that kind of component-entity logic?

Oh okay, you’re definitely on the right track. The thing is that you’re not going to be doing this with functions, I mean you could, but it would be a janky work, and it would be counter-intuitive to how UE4 and c++ works.

Taking your same idea, you would achieve this component based system by using classes that implement these features and communicate with an owner class.

A perfect example is the MovementComponent class. It implements some basic movement features, and several classes derive from it, such as CharacterMovementComponent, and extend it’s functionality.

Then in your character class, or whatever class contains the component, you use an interface/base class to change the properties of it.

Hopefully this helps explain a little bit of the idea behind things. I would definitely recommend digging further into it to get a better understand, especially c++ OOP and the component design pattern.

Hey nukeydukey69-

The callstack you posted appears to call your MasterCube constructor ( AMasterCube::AMasterCube()) and then crash after calling GEngine::AddOnScreenDebugMessage(). Did you add an include statement for Engine.h ( #include "Engine.h") in your class so that GEngine can properly be read by the engine? Additionally, Can you post the constructor from your MasterCube class for more information.

Here’s a pastebin of all the files involved. I stripped them of erroneous info so it only has what applies to this problem.

The cause of the crash is the if/else statement in your AMasterCube constructor. The use of GEngine specifically is the issue b/c GEngine is null when the game is not running. You should be able to work around this by checking if GEngine exists before using it. A simple solution would be to nest your current if/else into another if statement that specifically checks for GEngine.


Doug Wilson