Can't use GetWorld pointer code complete

urc_s03_unreals_playercontroller

#1

Hey guys as the name suggests whenever I use GetWorld-> the code complete doesn’t come up. It also shows up as underlined in red stating “Pointer to incomplete class type not allowed”. HOWEVER it still compiles/builds successfully. So it still works. Is it possible I need another GameFramework include to fix this?

here is how I have written the function containing the GetWorld.

// Called when the game starts
void UOpenDoor::BeginPlay()
{
Super::BeginPlay();

//find 
ActorThatOpens = GetWorld()->GetFirstPlayerController()->GetPawn();
//GetWorld()->

}


Default include
#2

I have the same problem. The Unreal Engine has changed since the tutorial and there are some header files to include:
#include “Engine/World.h”
#include “GameFramework/PlayerController.h”

I wish they would document those changes in the videos


#3

Awesome thanks!
I thought that would be the case just didn’t know where to look.


#4

had the same problem. yeah they didn’t say that in the video. but thank you for the solution.


#5

Could you please explain where you can find that you need GameFramework/PlayerController.h in order to make GetWorld() to work properly? We have found the reference to Engine/World.h under the API Reference to UWorld::GetWorld(), however we can find no reference to GetWorld() that implies that PlayerController.h is a requirement. Thanks ahead of time for your answer to this and any help you may be able to offer us.


#6

Add this to your cpp file:
#include “GameFramework/PlayerController.h”
#include “Engine/World.h”


#7

Actually, you only need one header file.

#include “Runtime/Engine/Classes/Components/ActorComponent.h”


#8

For those who are wondering how to find out what is required yourself, here’s how I did it (I’m learning all this too, so there might be a better way):

As others have mentioned, finding you need Engine/World.h is easy when you look up GetWorld(). And that is all you need for GetWorld() - you will notice no underline if you just type GetWorld(). You can even do GetWord()->GetTimeSeconds() for example.

GetWorld() will become underlined when you type “GetFirstPlayerController()->” indicating, to me at least, that there’s the problem. Looking up GetFirstPlayerController() shows the header file is still Engine/World.h… BUT the underline didn’t actually come up until I typed the “->” - ie. accessing what GetFirstPlayerController() points to.

So… as you can see what GetFirstPlayerController() returns from the documentation (a pointer to APlayerController), by dereferencing with “->” we are now looking at a APlayerController. Look that up and you’ll see GameFramework/PlayerController.h as the header.

Hope this helps :slight_smile:


#9

I’ve found it in unreal engine API reference. Just google “Unreal engine getfirstplayercontroller”. You should find this page -> https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UWorld/GetFirstPlayerController/index.html and on the bottom is “Header file” column.
As you can see when you hover over GetFirstPlayerController() function it returns pointer to APlayerController object. Google that and you will again get to the UE API Reference and on the bottom is your header file.


#10

Which cpp file exactly? I’ve tried adding it to every cpp file in the project, but nothing works.


#11

add this:

#include “Components/ActorComponent.h”

to your door’s header. This is a consequence of a recent change to UE4, where you now only include the components you need.


#12

I can’t seem to get this working, no matter what I try. I keep getting an error “pointer to incomplete class type is not allowed”

In my .h, I’ve got:
#include “CoreMinimal.h”
#include “Engine/TriggerVolume.h”
#include “Components/ActorComponent.h”
#include “OpenDoor.generated.h”

and in my .cpp I’ve got:
#include “OpenDoor.h”
#include “GameFramework/Actor.h”
#include “Engine/World.h”

If I list the Engine/World.h in the wrong place, suddenly my UCLASSes go wrong, so I’ve discovered that the order of these includes is very important, so I’m guessing I’ve got these out of order, or I have something in my .cpp which should be in my .h file. Can anyone advise?
thanks


#13

Here’s what I have in my door script:

.h
#include “Components/ActorComponent.h”
#include “OpenDoor.generated.h”

.cpp
#include “BuildingEscape.h”(only thing in there is #include Engine.h)
#include “OpenDoor.h”

shouldn’t matter, but you may be getting some bizarre behavior from including the same items multiple times. A lot of the headers #include the same components inside of them. Hard to say. I’ve run into the same problem where it seems to matter what order your #include other files. Including only the headers you need is something they just implemented, so maybe it’s a little buggy?


#14

Never got the GetPawn to work, it always returned null (or well it got undefined).
Tried all variations of imports, im assuming its something to do with the fact that
GetPawn is on GameFramework/Controller and not on the PlayerController.
Anyway, workaround for me was to use the GetPawnOrSpectator. It returns the first pawn or spectator, and atleast for me its always the default_pawn beeing returned.

#include "OpenDoor.h"
#include "CoreMinimal.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"


// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
	PrimaryComponentTick.bCanEverTick = true;
}

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

Sidenote:
If you run/play as Simulate you get the spectator pawn instead of the default pawn (as expected).

@sampattuzzi Perhaps you could give this some attention. :slight_smile:

Engine v4.17.2


#15

Does it work if you include GameFramework/Controller?


#17

I retried on my current codebase (I´m at Lecture 83 now) so i have another Default Pawn. GetPawn() seems to works as expected now. If its the switch in BasePawn im not sure, could also have been a VisualStudio debug issue (seems to be plenty of those).

Currently working:

#include "OpenDoor.h"
#include "CoreMinimal.h"
#include "Engine/World.h"
#include "GameFramework/PlayerController.h"

// Sets default values for this component's properties
UOpenDoor::UOpenDoor()
{
	PrimaryComponentTick.bCanEverTick = true;
}

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

and in the header file

#pragma once
#include "BuildingEscape.h"
#include "Engine/TriggerVolume.h"
#include "Components/ActorComponent.h"
#include "OpenDoor.generated.h"

#18

Ok so I have found a solution to this problem after it bugged the S out of me.

This is what you do. Go the file explorer and find your project folder.

Now delete as many of the following folders as you see: Binaries, build, intermediate, saved.

Now in the same folder where you see your unreal project file with the unreal logo. Right click and generate visual studio files.

After its done. Open the unreal project. Now go to your visual studio file and assuming that you have all the correct #includes, GetWorld() will now work.

Regards


#19

I’m having problems when the GetWorld() method is being called, as a side note when I try and compile BuildingEscape.h the compiler states it can’t find BuildingEscape.h, is that what you were explaining about Visual Studio being buggy or is it the Epic Games Unreal Engine version 4.18 to be at fault. I’ve had to comment out BuldingEscape.h for the time being, because it’s just one statement being used within it, is that a good idea?


#20

Hi @sampattuzzi

Excellent work run by the team at Udemy, the best tutorial for online learning!

I’ve got a quick question. With the recent update 4.18 from Epic Games, it seems to be throwing me back two errors when using GetWorld() and the FCollisionQueryParams() methods, I’m stuck at section 3, lecture 80 in trying to understand why it’s throwing me those errors. I’ve asked @Psvensso a question regarding the header file BuildingEscape.h and was wondering if its the recent update by Epic that could be the cause, can that be looked into by the Udemy instructors and update the video accordingly? If I go any further into the video lectures will that cause the demo to crash and burn unsuccessfully! Afraid, I’m very VERY afraid!

Hope to hear from @Psvensso or your good self in the coming days

@Brandon_Fuller


#21

What errors is it giving? Do they give you any clues as to how to solve the issue?