Can't use GetWorld pointer code complete

Hi @sampattuzzi Because the errors that it was giving I went back a few videos to where Ben was explaining on Section 3 Lecture 78 - Using the DrawDebugLine

That segment was absolutely spot on, as the compiling hadn’t thrown up any errors. If my memory serves me correctly the next video Section 3 lecture 79 Line Tracing AKA Ray-Casting wasn’t a problem neither; however the next one after that Section3 lecture 80 Line SingleTracebyObjectType() was where it was throwing up the errors.

What I’ll do and since I am new to the Udemy course, later on this afternoon,if not the evening, I will get back to lecture 80 to show you the exact error messages in the output pane on both vS 2017 Community and Unreal Engine output! As I’m there within one lecture either 79 or if on 80 now, but I’ll check later on!

On a side note I’ll include BuildingEscape.h error and send to question why is it throwing up an error when that is being used, to ask where is that exactly being used if it’s only got one #include statement declared within it!

I’ll send those queries to you as soon as possible! Rest assured I’m absolutely loving the course very much. Its absolutely brilliant, as its exactly what I and many other disabled programmers were looking for!

I’ll send the info you requested in the coming hours so you can have a look and see where the code and why its throwing those errors!

Say hi to everyone

@Brandon_Fuller

Just for someone who wants a solved hunk of code here it is: My .cpp:`

#include “GameFramework/Controller.h”
#include “Engine/World.h”
#include “OpenDoor.h”
#include “GameFramework//Actor.h”
#include “BuildingEscape.h”

// Sets default values for this component’s properties
UOpenDoor::UOpenDoor()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don’t need them.
PrimaryComponentTick.bCanEverTick = true;
}

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

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

}

void UOpenDoor::OpenDoor()
{
// Find the owning actor
AActor* Owner = GetOwner();

// Create a rotator
FRotator NewRotation = FRotator(0.f, -60.0f, 0.f);

// Set the door rotation
Owner->SetActorRotation(NewRotation);

}

// Called every frame
void UOpenDoor::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

// Pool the Trigger Volume
// If the ActorThatOpens is in the volume
if (PressurePlate->IsOverlappingActor(ActorThatOpens))
{
	OpenDoor();
}	

}`

And my .h:

#pragma once

#include “Components/ActorComponent.h”
#include “CoreMinimal.h”
#include “Engine/TriggerVolume.h”
#include “OpenDoor.generated.h”

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
GENERATED_BODY()

public:
// Sets default values for this component’s properties
UOpenDoor();

protected:
// Called when the game starts
virtual void BeginPlay() override;

void OpenDoor();

public:
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;

private:
UPROPERTY(VisibleAnywhere)
float OpenAngel = 90.0f;

UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;

AActor* ActorThatOpens; // Remember pawn inherits form actor

};

What really got me was not putting () after GetWorld. I don’t know why it put only some parts in those grey boxes. Kudos.

Hey,
even though the OP has asked the question some time ago just a quick explanation of what might be wrong, if you can’t get this to work:

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

As mentioned in the videos these error appear in newer Unreal version because of IWYU. And the solution is to include ALL headers of things you use where you use them.
so you should look up in the code reference what you are using, you find the needed header files at the bottom of each page.
So for GetWorld() you will find out that you need GameFramework/Actor.h, and looking at the return type you will see that this is an UWorld Object which you can access with its method GetFirstPlayerController() both of which are declared in Engine/World.h so you will need this as well.

In the case of this course it will leave us with the following includes:

in .cpp
#include "OpenDoor.h"'
#include "Engine/World.h"'
#include "GameFramework/Actor.h"'

If you have the last two already included in the OpenDoor.h you should not have to include them again in your cpp but it won’t hurt you either. The same is of course true if you already have any other header file included that also includes world and actor.

Hope this clarifies the problem and the solution a bit

3 Likes

Anyone has a fix for the “CompilerResultsLog: Error: C:\Users\Peter\Repos\03_BuildingEscape\BuildingEscape\Source\BuildingEscape\Grabber.cpp(195) : error C3861: ‘GetWorld’: identifier not found”

Ive tried pretty much everything in this thread, included everything that even remotely makes sense. According to ViStu one GetWorld works fine, but the other is undefined. If i copy the same code to the broken GetWorld it still doesnt work.

Im at lecture 83, and so far it seems impossible to get by in its current form - even the fixes on this forum seems to be outdated. :confused:

2 Likes

BTW, dont worry about including too many includes. Most dev studios dont mind the compile time.

I have an extensive dev background, just running this course to keep sharp, and although its almost like a crash course, its well rounded.

https://www.linkedin.com/in/brian-bekian-337aa61/

Hey guys, I was wondering how to fix an issue im having with the code :

So it runs just fine, but there are two red lines under some parts of the OpenDoor.cpp file

Screenshot%20(53)

and

If anyone can help that would be appreciated

:slight_smile:

I think it’s just the intellisense being slow don’t worry

Oh ok thanks for clearing that up

Hi guys, to get working:
GetWorld()->GetFirstPlayerController()->GetPawn();

I needed to include:
“Engine/World.h”
“GameFramework/PlayerController.h”

Now working :).

2 Likes

Just thought it should be known that the VS addon Resharper makes this kinda thing a non issue. It magically knows when you need to #include something & gives you a button to do it automatically. I’m sure Visual Assist can handle auto includes too.

Though neither are free or particularly cheap…

i found a fix fdor this by installing the unrealVS extension

Hello !

I tried everything, still cannot make my code working again after using ActorThatOpens = GetWorld()->GetPlayerController()->GetPawn();

PLEASE HELP ME !!

Kind regards,
Konstantin

If anyone is having this problem, just put this in your cpp file

#include “Engine/World.h”

That is all you need, it worked just fine for me.

Thank you!

I feel like Ive tried everything. Debugging in unreal is very different than debugging in java using netbeans, both however are very frustrating. I’ve included all the various includes in different ways and yet nothing seems to be working in Xcode 8 using UE4 4.21.2

You ARE A LIFESAVER, THANK YOU!!!

Thank you so much. I am currently redoing this course using 4.23 and the way you explained this cleared up the only issue I was having during compile time. This helped a ton!

Privacy & Terms