Juice-Tin
(Juice-Tin)
1
Hi, I have an overlap function that returns
class AActor* otherActor
The problem is when I try to cast it, it ALWAYS succeeds.
ASpline* spline = (ASpline*)otherActor;
Even when otherActor is a cube, this cast succeeds and then gives me all bogus values. There’s no NULL, nullptr, errors, crashing, etc.
Is there a better way to cast or check if the cast was a success?
You can use the Cast macro:
ASpline* spline = Cast<ASpline>(otherActor);
As far as I know this is the suggested way.
Juice-Tin
(Juice-Tin)
3
That properly returns nullptr on incorrect casts, thanks!
Just FYI, Juice-Tin, (ASpline*)otherActor is old, C-style casting, and the cast will ALWAYS be performed and will be invalid if you cast the wrong types. For any Unreal types, you should use Cast<> as staticvoidlol suggested. For non-Unreal types, I’d recommend using these instead of C-style casting: http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used
Or, better yet, try to avoid casting as much as possible
Juice-Tin
(Juice-Tin)
5
Yeah, ‘OnComponentBeginOverlap’ passes a generic actor so I’ve got no choice but to cast.
But thanks for the additional info!
Awesome link Mothership.Entertainment!
I’m no expert on this, but is the cast in Juice-Tin’s example always succeeding because in effect the const_cast is the first cast that succeeds?