Editor crashes when GetOwner() is moved

I have a strange issue that causes the editor to crash when ‘walking into the light’. Heh.

When OpenDoor() or CloseDoor() is called without ‘AActor* Owner = GetOwner();’ the editor will freeze for a moment, then crash. I have created the Owner variable in the header file, and all code has been edited to mirror that in the video (including adding the GetOwner line to BeginPlay()).

This is not a show-stopper, as I can just leave the snippet in the door functions, but I’d like to solve the issue. I’m sure i’m overlooking something small, but cant find it. Any help would be appreciated.

Working Code:

// Called when the game starts
void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();
	ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
}

void UOpenDoor::OpenDoor()
{
	AActor* Owner = GetOwner();
	Owner->SetActorRotation(FRotator(0.0f, OpenAngle, 0.0f));
}

Code that Crashes:

// Called when the game starts
void UOpenDoor::BeginPlay()
{
	Super::BeginPlay();
	AActor* Owner = GetOwner();
	ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
}

void UOpenDoor::OpenDoor()
{
	Owner->SetActorRotation(FRotator(0.0f, OpenAngle, 0.0f));
}

Could you post the rest of the function(s) you are talking about?

Just updated, thanks.

AActor* Owner = GetOwner();

Is creating a new variable in the scope of the function. I’m assuming you have AActor* Owner in your class which you’re trying to set in BeginPlay.

// Called when the game starts
void UOpenDoor::BeginPlay()
{
    Super::BeginPlay();
    Owner = GetOwner(); //no AActor*
    ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
}
2 Likes

That fixed it! It seems I was copy/paste happy instead of thinking about what I was doing. Thanks for the help!

I encountered the same error and my Unreal editor crashed as well. I find it quite interesting that Visual Studio did not pick up the error, although it would have picked that up without the heavy weight Unreal engine headers confusing it.

If I do encounter similar bugs in the future, what would be the best way to debug this? VS is unable to pick that up and Unreal crash log is not meaningful. Is there any suggested additional settings or tools?

Hi CY_Lim.

The reason why this isn’t showing up as an error in Visual Studio is because it actually isn’t an error. If you remember the previous section and our Bulls and Cows game, you might remember a brief discussion about the scope of a variable.

Here’s a link to another thread where we talked about a similar situation:

As far as how to watch for it in the future, only thing I can say there is to be mindful of where your variables are being defined.

You are a star, that has solved my problem as well. Can’t thank you enough :slight_smile:

Privacy & Terms