Releasing Physics Object

When asked to attempt to release object I came up with the attached solution and it seems to work perfectly, I am curious if this is valid or if there could be some errors i am ignorant to here?

This is the instructors answer here

If you GetPhysicsHandle returns nullptr then you would dereference a null pointer which is bad. The check for GrabbedComponent is unneeded as ReleaseComponent already does that.

A couple comments on your code.

  1. Don’t use NULL, use nullptr as that underline is most likely telling you. With that said pointers are convertible to bool, null pointers are false, anything else is true so you can just write
    if (GetPhysicsHandle())
    
    This reads lime natural English to me. “if I have a thing” instead of “if I don’t have a null pointer” which is something positive but worded negatively (i.e. null) can be a bit confusing at times.
  2. The statement within the if should be indented. New scopes should always be indented and an if statement introduced a new scope.
  3. It’s Unreal’s convention to always use braces, even for single statements. This helps prevent real bugs.
1 Like

Thank you for the detailed reply, I appreciate your insights.

1 Like