I’m trying to merge two C++ tutorials
I’m trying to add the second Tutorials growing function to the first but one line of code makes the engine crash on start
I’ve spent half the day trying to fix this but I haven’t been able to find anything that works yet. Can anyone help?
//cpp
// Called every frame
void ACollidingPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// Handle growing and shrinking based on our "Grow" action
float CurrentScale = OurVisibleComponent->GetComponentScale().X;
if (bGrowing)
{
// Grow to double size over the course of one second
CurrentScale += DeltaTime;
}
else
{
// Shrink half as fast as we grow
CurrentScale -= (DeltaTime * 0.5f);
}
// Make sure we never drop below our starting size, or increase past double size.
CurrentScale = FMath::Clamp(CurrentScale, 1.0f, 2.0f);
/*Crash Code*/ OurVisibleComponent->SetWorldScale3D(FVector(CurrentScale));
// Handle movement based on our "MoveX" and "MoveY" axes
if (!CurrentVelocity.IsZero())
{
FVector NewLocation = GetActorLocation() + (CurrentVelocity * DeltaTime);
SetActorLocation(NewLocation);
}
//.h
UPROPERTY(EditAnywhere)
USceneComponent* OurVisibleComponent;
// varibles
FVector CurrentVelocity;
bool bGrowing;
Inputs
void ACollidingPawn::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{
Super::SetupPlayerInputComponent(PlayerInputComponent);
// Particle
InputComponent->BindAction("ParticleToggle", IE_Pressed, this, &ACollidingPawn::ParticleToggle);
// movement
InputComponent->BindAxis("MoveForward", this, &ACollidingPawn::MoveForward);
InputComponent->BindAxis("MoveRight", this, &ACollidingPawn::MoveRight);
InputComponent->BindAxis("Turn", this, &ACollidingPawn::Turn);
// Grow
InputComponent->BindAction("Grow", IE_Pressed, this, &ACollidingPawn::StartGrowing);
InputComponent->BindAction("Grow", IE_Released, this, &ACollidingPawn::StopGrowing);
}
void ACollidingPawn::StartGrowing()
{
bGrowing = true;
}
void ACollidingPawn::StopGrowing()
{
bGrowing = false;
Would be good to know the exact error message.
Did you add the code that actually creates the component “OurVisibleComponent”?
There is a part in the “Player Input and Pawns” turtorial you posted where the constructor of the Pawn is filled with:
OurVisibleComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("OurVisibleComponent"));
OurVisibleComponent->SetupAttachment(RootComponent);
You definitiely need this code, otherwise you’ll get an access violation exception for accessing nullptr.
well I’ve put the code in it stopped the game from crashing on start so thanks for that. But the grow function isn’t working pressing F (what i have it bound to) doesn’t trigger. And yes i have the function and input bind named the same. is there a quick fix to this or will i have to rewrite or rearrange the code
?
Are StartGrowing and StopGrowing UFUNCTION()?
Yes the other Inputs work and for the Grow functions i don’t know if they are called or not. I’m trying to find out if they are getting called. by calling UE_LOG on input but I’m having a bit of trouble trying to implement it. it’s either not compiling or crashing
this is what I’ve tried so far
void ACollidingPawn::StartGrowing()
{
bGrowing = true;
UE_LOG(LogTemp, Warning, TEXT(“Pressed!!!”));
}
void ACollidingPawn::StopGrowing()
{
bGrowing = false;
UE_LOG(LogTemp, Warning, TEXT("Released!!!"));
}
This one i tryed making a debug function
//cpp
void ACollidingPawn::StartGrowing()
{
bGrowing = true;
DebugStartGrowing();
}
void ACollidingPawn::StopGrowing()
{
bGrowing = false;
DebugStopGrowing();
}
//Movement Functions End
void DebugStartGrowing() {
UE_LOG(LogTemp, Warning, TEXT("Pressed!!!"));
}
void DebugStopGrowing() {
UE_LOG(LogTemp, Warning, TEXT("Released!!!"));
//.h
void DebugStartGrowing();
void DebugStopGrowing();
first one crashes UE and second one gives me error
Show us the input part of the code.
No there not
i just added what you said
UFUNCTION(); void StartGrowing(); void StopGrowing();
How about the other inputs? Do they work?
How do you know StartGrowing and StopGrowing are not called? Are you logging or printing something?
Ok. Are you really sure you set “Grow” input in the Action Mapping and not in the Axis Mapping and also spelled right?
well this is what i have as far as i can see its fine
InputComponent->BindAction(“Grow”, IE_Pressed, this, &ACollidingPawn::StartGrowing);
InputComponent->BindAction(“Grow”, IE_Released, this, &ACollidingPawn::StopGrowing);

You need to add “ACollidingPawn::” in between “void” and “DebugStartGrowing” like so (in your .cpp):
void ACollidingPawn::DebugStartGrowing(){
UE_LOG(LogTemp, Warning, TEXT("Pressed!!!"));
}
Same is true for the second function “DebugStopGrowing”. It is also missing the “ACollidingPawn::” prefix.
And make sure that the two functions are properly defined in your .h file too.
Ok grow function works inputs are being called. and I think I can work that out later but what I’m confused about now is that the code that fixed my problem of crashing on play initially doesn’t work consistently. It works when I first put the code in but the next day when i compile and press play it crashes again with no changes to the fix code. and then I have to go back to a version before putting the fix code in using VCS software. Can anyone explain why its doing this ?