I found an alternative route in the ActorHit challenge?

I mainly want to confirm if there would have been any side effect to using this instead of the route that was taught after the challenge to log the actor being hit by our Line Trace.

Below is a sample of the code I used when completing the challenge to output the hit actors:

	if (Hit.Actor != NULL)
	{
		UE_LOG(LogTemp, Warning, TEXT("Hitting: %s"), *Hit.Actor->GetName());
	}

Admittedly it seems messy, and part of it came to me as I’ve got about a decade experience with SQL so I’m used to the idea of using is not NULL type of scenarios in queries/codes.

What I found however is that it worked! I was able to compile this without any issues and it properly logged the proper objects that I was hitting with the ray trace and from what I could tell the if condition was protecting against any potential NULL pointers.

What I’d like to confirm though is if this is truly an alternative or if I had kept that code in place that it would have caused some sort of issue down the line?

PS Edit: Viewing the next video and completely got reminded that memory pointers when NULL would be nullptr (I’ve done some C++ in the past and just completely forgot about initializing pointers with nullptr) so technically that if statement likely isn’t working, but it still compiled. Because it’d never equal NULL in general then it was probably the same as if I just had the UE_LOG code by itself.

Still interesting that *Hit.Actor seems equivalent to Hit.GetActor() though when using a GetName() afterwards.

It’s fine and will work but nullptr should be used instead of NULL.

NULL is just a macro for 0 (I think technically it’s “implementation defined”).
nullptr was introduced in C++11 so before that NULL was used the to denote a null pointer.

nullptr was introduced because having 0 mean both an interger value and a null pointer had issues,
you can read about such in the proposal for nullptr here

https://wg21.link/n1601

Though with that said C++ is rather strict on being backwards compatible and goes to great lengths to avoid breaking changes so 0 can still be used to mean a null pointer today though you should definitely use nullptr.

In both cases * isn’t used to dereference the pointer, that’s what the arrow operator is doing. The following are eqivalent to your code and the lecture’s

*(Hit.Actor->GetName())
*(Hit.GetActor()->GetName())

As * has a lower precendence than . and -> (why the latter exists in the first place) the outer () are redundant.

Privacy & Terms