Player Does Not Open Door

Sorry for the length of this post, but I figured it’ll be helpful to know what I’ve done to try to solve this.

My problem is that the door does not open when the player walks through the pressure plate. From the debugging I’ve done, it’s because GetPawn() is returning empty to the ActorThatOpens variable and results in a false when it evaluates the if statement within TickComponent that calls the OpenDoor function. I provided an image of my .cpp and .h file.



I came to the conclusion it was GetPawn() because I created a temporary global variable, OpeningAgent (line 9 of cpp), for debugging purposes to see which part of the dereferencing chain on line 32 was returning empty. I then log if the variable is empty (lines 38-41). After making OpeningAgent a APlayerCharacter pointer and assigning it as GetWorld()->GetFirstPlayer(), it does not log my warning (line 33). I then made OpeningAgent a APawn pointer and assigning it as shown in the lecture, it made a log of my warning. Same when I made OpeningAgent a AActor pointer.

I looked at an old topic (Can't use GetWorld pointer code complete) which was very similar to my problem and tried what user Psvensso used which was the GetPawnOrSpectator() function. This does not log my warning, but it does not open the door either when I move the player inside the pressure plate. I even placed the Player Start within the pressure plate and the door still does not open. Most user’s issues were resolved because of the header files included within this lecture, which is what I already have included in my cpp.

I tried another way to get the player pawn by using UGameplayStatics::GetPlayerPawn(GetWorld(), 0) (which is why there’s an extra include statement on line 5). Otherwise, I made sure I included the header files in the same order as in the lecture to avoid any issues there. I made sure it wasn’t anything wrong with the actual door opening because the door still opens correctly when I assign the DefaultPawn as the ActorThatOpens within Unreal as was done in lecture 108.

I don’t know if this will help any, but I am using Unreal v. 4.25.

I’ve spent hours figuring out why this is the case to no avail. Any help would be appreciated.

Thanks for the detailed investigation. I suspect you are simulating rather than playing. The pawn isn’t spawned if you simulate.

Some comments

Why global?

This wouldn’ cause a runtime error.

Yep, that was the issue. I was simulating. I must’ve accidentally pressed Alt + S at some point.

I could’ve made my variable a member in the header, but I wanted to avoid longer compile times when I had to change the variable within my .h.

Thank you, Dan.

I wasn’t suggesting that. I was suggetsing a local variable

APawn* OpeningAgent = ...

When I was initially trying to solve this, I did make it a local variable. Then I realized I wanted to use that variable in TickComponent, but because of it being a local variable in BeginPlay(), it goes out of scope.

I could’ve assigned my variable to ActorThatOpens and keep using ActorThatOpens in TickComponent, but I probably would’ve kept it the way it is to deal with my variable directly.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms