Crash when possessing pawn

Did you check with an “IsValid” Node if the Character you try to possess is actually, well, valid?

Could you try to give the Target-input on your Possess node the output of GetPlayerController?

Hello, I have created a character using C++. The code written is for VR. It has basic movement feature and also grab certain objects. Then I tried, using blueprint, to possess a pawn.

But for some reason it crashes. If I run the same code but instead of possessing from my VRCharacter I posses from some other pawn, then possession is successful. Why does that happen? What can be the possible reasons for it? Any suggestion would be helpful. If code is needed I can provide that too.

Regards

Tested it with “IsValid” on your suggestion. Still crashing.

the code is in Controller class. Do you want me to try it in this class or move the code to GameMode?

Hey,

Can you post the crash log?

Thanks.

Here it is:-

MachineId:0A5724804E422E69B5DD0FA03D52BB7B
EpicAccountId:542acf2ff56347d3b52f5c554f92ed63

Access violation - code c0000005 (first/second chance not available)

[rest attached as text][link text][1]

That’s OK. In the future you can upload the .txt files instead of pasting the text in them, if you want.

Can you post the following function:

AVRCharacter::MoveForward()

Hey,

Can you post the following function?

AVRCharacter::MoveForward()

Thanks

Well you were right on spot. I recreated the class again. Added parts of code to see where error is.

void ATestForPossession::MoveForward(float Value)
{
	/*FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X);
	AddMovementInput(Direction, Value);*/
}

void ATestForPossession::MoveRight(float Value)
{
	/*FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y);
	AddMovementInput(Direction, Value);*/
}

MoveForward and Move Right are creating the problem. Though I am unable to understand why.

Edit: I commented them because those lines are what were crashing the engine when possessing.

Hey,

Can you try this?

 void ATestForPossession::MoveForward(float Value)
 {
	if( Controller )
	{
		FVector Direction = FRotationMatrix( Controller->GetControlRotation( ) ).GetScaledAxis( EAxis::X );
		AddMovementInput( Direction, Value );
	}
 }
 
 void ATestForPossession::MoveRight(float Value)
 {
	if( Controller )
	{
		FVector Direction = FRotationMatrix( Controller->GetControlRotation( ) ).GetScaledAxis( EAxis::Y );
		AddMovementInput( Direction, Value );
	}
 }

Also, to explain what is going on:

You are trying to use “Controller” when it might not exist, resulting in a Access Violation; in “easy” terms, a Access Violation is when the program during runtime tries to use a nullptr (a pointer with an empty value). When this happens, the program will terminate with the error you are getting.

In the future, I recommend checking to make sure any pointer you are using that may not be assigned yet or may lose an assignment. As such:

if( MyPointer )
{
    // Pointer Exists
}
else
{
    // Pointer doesnt exist
}

Oh wow thanks a lot. And I feel kinda stupid :P. I messed up at a very basic level. Guess that’s what you get for not programming for a while. This has been a good lesson.

As an alternative, the way input is done for most generic characters, is as follows:

[.h]

void Forward( float Amount );
void Right( float Amount );

[cpp]

void MyCharacter::Forward( float Amount )
{
	if( Amount != 0.f )
	{
		AddMovementInput( GetActorForwardVector( ), Amount );
	}
}

void MyCharacter::Right( float Amount )
{
	if( Amount != 0.f )
	{
		AddMovementInput( GetActorRightVector( ), Amount );
	}
}

Good luck with your project.