x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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.

Product Version: Not Selected
Tags:
more ▼

asked Dec 30 '14 at 05:26 AM in C++ Programming

avatar image

TomBBonin
8 1 2 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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

https://forums.unrealengine.com/showthread.php?12837-Introducing-my-Victory-HUD-Elements-System!-Dynamic-HUD-System-With-Animation&p=83240&viewfull=1#post83240

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);
 }
more ▼

answered Jan 01 '15 at 07:15 PM

avatar image

Rama
9.9k 430 316 1041

avatar image TomBBonin Jan 04 '15 at 03:01 PM

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

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

you can try static_cast

more ▼

answered Apr 14 '17 at 02:43 AM

avatar image

huangchuo
1 1

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question