Can't use Cast() or dynamic_cast in this case?

Hi!
I’m very new to Coding in UE so please bear with me as i feel i’m missing something terribly obvious.

I am attempting to down cast an object of mine during runtime. This object is not derived from any UE class.

From my understanding these are my hypotheses :

As RTTI is turned off in the engine, i can’t use dynamic_cast. ( and i can’t turn it on ? )

As i am not deriving from any UE class, my class isn’t registered in the reflection system, therefore i can’t use Cast(). This is for simplicity and to avoid as much overhead as i can, but if there is an efficient way of doing this i’m all ears!

My question is : am i mistaken in thinking neither option is available to me? What can i do?
I’m guessing i’ll throw an enum into the base class, test that and static_cast accordingly.

The code looks something like this :

MyBaseClass* baseActor = GetMyBaseClassActor();

MyDerivedClassA* derivedActorA = Cast<MyBaseClass>(baseActor);
/////////// OR ///////////
MyDerivedClassA* derivedActorA = dynamic_cast<MyBaseClass*>(baseActor);
if(derivedActorA) { ... }

MyDerivedClassB* derivedActorB = dynamic_cast<MyBaseClass*>(baseActor);
if(derivedActorB) { ... }

Thank you very much for your time!

Thomas.

Hi there!

You wrote:
“I’m guessing i’ll throw an enum into the base class, test that and static_cast accordingly.”

That is a solution I’ve employed on multiple occasions to great effect!

I made a pure C++ UI system (not UClass) this way!

UE4 Forum Link with Videos

#Pure C++ Base Class .h

//Is Checks

FORCEINLINE bool IsPanel() const
{
	return (VictoryType == EVictoryElements::VictoryPanel);
}
FORCEINLINE bool IsWindow() const
{
	return (VictoryType == EVictoryElements::VictoryWindow);
}

//As Casts
FVictoryPanel* 				AsPanel();
FVictoryWindow* 			AsWindow();

#.cpp

//Panel
FVictoryPanel* FVictoryHUDElementHighest::AsPanel()
{
	if(!IsPanel()) return NULL;
	//~~~~~~~~~~~~~
	return static_cast<FVictoryPanel*>(this);
}
//Window
FVictoryWindow* FVictoryHUDElementHighest::AsWindow()
{
	if(!IsWindow()) return NULL;
	//~~~~~~~~~~~~~
	return static_cast<FVictoryWindow*>(this);
}

Needed to wrap this up, so i used something similar to this. Thanks for the input!

you can try static_cast