Download

Getting exception thrown with InputComponent->GetAxisValue

Can anyone tell me why I am receiving the below exception thrown. I have the axis labeled properly in the inputs in project settings. There is no PlayerController taking control of the vehicle which is the only thing I can think of why the exception is being thrown.

It’s crashing because InputComponent is nullptr. That would happen if there was no PlayerController possessing the pawn. All you need to do is wrap the lines that use the input component in if (IsValid(InputComponent)) { ... }.

Hope this helps

1 Like

Thank you so much @NachoMonkey2 ! You were exactly right. I did an if check on the InputComponent, because like you said there is no PlayerController possessing the pawn so it was a nullptr. Everything is running smooth now!

Just so you know, the IsValid function is a member function of UObject, and it returns true if:

  • Is the object != nullptr?
    AND
  • Is the object not pending kill?

So, technically, using IsValid as opposed to a simple boolean check is a bit safer, as it will return false if the object is pending kill (being destroyed). It’s more convenient with Actors, as Actors can take a few frames to be completely destroyed after AActor::Destroy is called.