Announcement

Collapse
No announcement yet.

Dynamic Casting Best Practice

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Dynamic Casting Best Practice

    In tutorials (even the high quality ones) it is common to see something like this
    Code:
        void AFPSObjectiveActor::NotifyActorBeginOverlap(AActor * OtherActor)
        {
             AFPSCharacter* OverlappedCharacter = Cast<AFPSCharacter>(OtherActor);
             if (OverlappedCharacter)
             {
                 //....
             }
        }
    but this is not very common in general games code (in my experience). Is this just tutorials skipping things for simplification or is this just the "UE4 way" of doing this kind of thing? Are there any common alternatives?


    #2
    ... general games code? what else would you do for a similar sort of operation?

    Comment


      #3
      Casting is very common.

      Comment


        #4
        Originally posted by eblade View Post
        ... general games code? what else would you do for a similar sort of operation?
        In other code bases I have worked on this situation is very rare (because inheritance was not used so often and when it was used it was in situations when the base class interface was sufficient).

        Comment


          #5
          When absolutely sure the pointer won't be null (or should never be) I just use ->IsA() instead; but dynamic casting is safer to use.
          | Savior | USQLite | FSM | Object Pool | Sound Occlusion | Property Transfer | Magic Nodes | MORE |

          Comment


            #6
            AActor is a very high-level class. It can be potentially be everything that is placed in the world.
            Casting is a necessity to determine it's actual type.

            However, there is the alternative of Components. You can just use GetComponentOfClass<T>() to retrieve a Component and use the Component to execute your logic.
            This is type-safe (but not null-safe of course). It is also questionable whether this is faster than casting (prolly not)

            Comment


              #7
              Originally posted by SausagesSizzling View Post

              In other code bases I have worked on this situation is very rare (because inheritance was not used so often and when it was used it was in situations when the base class interface was sufficient).
              The reasons for casting in UE4 is because Inheritance is heavily used, mainly due to Garbage Collection / Serialization. UObject is the base class for the majority of all objects within UE4, and thus you have to cast from that to something else.
              Able Ability System - A high performance, robust ability system for UE4. Now Available!

              Comment


                #8
                Originally posted by SausagesSizzling View Post

                In other code bases I have worked on this situation is very rare (because inheritance was not used so often and when it was used it was in situations when the base class interface was sufficient).
                so.. you're looking for an alternative that is more similar to something that doesn't apply at all? i'm very confused.

                Comment


                  #9
                  Thanks for all the replies, very helpful I'm just getting used to the unreal way of doing things I was just making sure because dynamic casting is quite slow for an operation that doesn't involve fetching memory so often avoided in performance sensitive code and too much dynamic casting can be a code smell. It sounds like here the benefits gained from garbage collection, serialization and common interface greatly outweigh this (minor) inconvenience.

                  Originally posted by eblade View Post
                  so.. you're looking for an alternative that is more similar to something that doesn't apply at all? i'm very confused.
                  Not at all, I'm just checking that I am not getting into bad habits early on As I said dynamic casting is often avoided where possible in the wider C++ community so I need to learn how to use Unreal and unlearn some of the things I "know". Since I'm new to the engine I sometimes get things to work but not sure if I am doing it the "proper" Unreal way or if I have just performed an ugly hack.

                  Thanks again!

                  Comment


                    #10
                    You're right that in normal C++, RTTI and casting are pretty performance heavy. However, UE4 has it's own reflection system so casting is much cheaper. You shouldn't need to worry about casting performance in UE4 (I have yet to run into a profile where casting was the bottleneck).
                    Able Ability System - A high performance, robust ability system for UE4. Now Available!

                    Comment

                    Working...
                    X