IsInputKeyDown Crashing

My problem is when i start the game in in editor with one player
APlayerController* pc = Cast(GetController()); is not NULL but it i add another player that becomes NULL i need it to work so i can get Input from the spacebar i need to check when the key is down and when it does it launches that player in the air. I also am not sure how to do this individually for each player i have the code that does the air launching action in the Tick() function i am doing this in C++ not using blueprint except the character because im using the Third person template.

post the code thats causing the crash.

void ATowerCharacter::Tick(float DeltaSeconds) { APlayerController* pc = Cast(GetController());
if (!pc) return;

if (pc->IsInputKeyDown(EKeys::SpaceBar))
{
	if (fuel >= 0)
	{
		fuel--;

		FString SFuel = FString::SanitizeFloat(fuel);

		GetCharacterMovement()->Velocity += FVector(0, 0, 14);

		if (GEngine)
		{
			GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::, SFuel);
		}
	}
	else
	{
		GetCharacterMovement()->Velocity += FVector(0, 0, 0);
	}
}

if (fuel < totalFuel && GetCharacterMovement()->IsMovingOnGround())
{
	fuel++;

	FString SFuel = FString::SanitizeFloat(fuel);

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::, SFuel);
	}
}

}

any ideas?

I can’t think of anything that would cause a crash from this code other than if you’re not including Engine.h (Which I believe that has been fixed already). Could you provide me with all of the code in the project or the project itself so that I can put this through the compiler and debug it? It would be the easiest way to find the issue.

jetpack cpp : http:///3Bx0rBpH

jetpack h : http:///DZhqUAPG

Character cpp : http:///GjmmTLRe

Character h : http:///AUui6dDF

im using the third person template and jetpack is a C++ component to attach to players back on spine3 bone.

Can you explain the process needed to get the crash and when exactly the crash occurs? Does it require adding a second player during gameplay as you mentioned in the original question or is it when pressing a certain key?

Are you, by any chance, making a Network Game and trying to get the second Controller on Client site? Because clients only have an instance of their own Controller. If you want to use the Controller of another Client, you need to either call the code on that Player or let the Server change replicated things on his Controller.

If you experience an Access violation, try to make sure all your pointers aren’t NULL by using an If(ThePointerYouNeedToCheck) and only work with it inside this if.

im starting the 2nd player in editor by changing the in editor player count on the drop down for the play button and as soon as i do that an access violation popsup and visual studio brings me to the engine code where isinputkeydown is defined

Hi xESWxDEATHx,

The crash is being caused by the if statement itself passing a null value into IsInputKeyDown. To be specific, the tpc->CastToPlayerController returns a null which leads to this. The way that we worked around this was to make a GameMode that had TowerCharacter (the class, not the blueprint) as the default pawn class and then cast to the PlayerController outside of the function like so:

APlayerController* MyPlayerController = Cast<APlayerController>(tpc);
	if (MyPlayerController)
	{
		if (MyPlayerController->IsInputKeyDown(EKeys::SpaceBar))

This places the result of the cast into a PlayerController pointer then checks to make sure that it isn’t null. After that, it does what you were doing in your original code. Checking to see if it is null first is vital, as when I tested this in Dedicated Server mode, the PlayerController cast was failing for the first 12 ticks, so the crash would still occur if it was allowed to try IsInputKeyDown again.

Hope this helps!

so how would i set the default pawn to the character class instead of the blueprint?

If the blueprint is also called TowerCharacter, when you go to set the Default Pawn Class, it should list TowerCharacter there twice. If this is the case, you can either take a guess or rename the blueprint so that they are separate. Either way, the class itself should be listed there.

i can’t set the default pawn in the editor as the gamemode is in C++ how would i do it in there?

You can set the default pawn class in the constructor for the gamemode with the following syntax:

DefaultPawnClass = ATowerCharacter::StaticClass();

so i did that and now it is bringing me to the same player input access violation even when i change the player count to 1

Have you changed your code for the portion that was causing the crash? If so, can I see your new jetpack.cpp file?

Hi xESWxDEATHx,

Do you still require assistance? If so, please let me know and I’ll be happy to continue assisting you with this issue.

Hi xESWxDEATHx,

We haven’t heard from you in a while. I’ll be marking this issue as resolved for tracking purposes, if you are still experiencing this issue or require any assistance, please comment and the question will reopen.

Have a nice day,