Expression must have a class type, creating a class reference

I’m currently trying to setup a reference to my Gamemode in one of my classes:

class.h

class UBrawlerGameMode *GameMode;

class.cpp

#include "BrawlerGameMode.h"


void ABrawler_Character_Player::BeginPlay()
{
	ABrawlerGameMode* GameMode = (ABrawlerGameMode*)GetWorld()->GetAuthGameMode();
	Super::BeginPlay();
}

For some reason this:

void ACharacterBrawler::SomeFunction()
{
    BMovementComponent->AddInputVector(FVector(0, 0, -1) * GameMode.Gravity, true); //This line
}

Gives me an error that “GameMode” needs a class type? I really don’t get what it means, any clues?

I’m guessing I have to use the member access operator in some way, but I’m unsure of the syntax.

I’ve been looking at this example: Pointer to C++ Classes
and tried GameMode->Gravity, but then it gives me a “Pointer to incomplete class” error? But as you can see, it’s defined in the header?

I see a couple of errors in the code:

On BeginPlay you are declaring and defining GameMode again. You should define the GameMode you already declared in the header file in the constructor of your character class

ABrawler_Character_Player::ABrawler_Chararacter_Player()
{
         GameMode = Cast<ABrawlerGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
}

GameMode is a pointer, so you should be getting its members by using →

Anyways I would recomend you to not declare a GameMode variable on your header and just creating a pointer when needed. In your case:

 void ACharacterBrawler::SomeFunction()
 {
     ABrawlerGameMode* GameMode = Cast<ABrawlerGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
     if (GameMode != NULL)
     {
       BMovementComponent->AddInputVector(FVector(0, 0, -1) * GameMode->Gravity, true); 
     }
    
 }

Also, if you really want to declare it in your header, you should use the UPROPERTY() decorator, otherwise you will run into Garbage Collection issues.

Thanks, now I understand how declarations works and using pointers a lot better.

But I tried doing the cast in your first example (since I am going to use the variable in multiple places, I don’t want to redo the variable all over the place), but when I do that, the project crashes on loading. Suggestions?

This is what it looks like at the moment:
Brawler_Character_Player.h

UPROPERTY(EditAnywhere)
class UBrawlerGameMode *GameMode;

Brawler_Character_Player.cpp

#include "BrawlerGameMode.h"

ABrawler_Character_Player::ABrawler_Character_Player()
{
	GameMode = Cast<ABrawlerGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
}
ABrawler_Character_Player::SomeFunction()
{
     BMovementComponent->AddInputVector(FVector(0, 0, -1) * GameMode->Gravity, true);

}

Also, if I try to create a pointer on the fly without declaring anything as in your example, I get these errors:

Error (active) identifier “ABrawlerGameMode” is undefined Brawler c:\Users\Gray\Dropbox\Brawler\Project Files\Brawler\Source\Brawler\Brawler_Character_Player.cpp 76

Error (active) identifier “GameMode” is undefined Brawler c:\Users\Gray\Dropbox\Brawler\Project Files\Brawler\Source\Brawler\Brawler_Character_Player.cpp 76

Error (active) no instance of overloaded function “Cast” matches the argument list Brawler c:\Users\Gray\Dropbox\Brawler\Project Files\Brawler\Source\Brawler\Brawler_Character_Player.cpp 76

Yes, I was able to reproduce the error. Sorry for the delay in responding. As a work around you could make a function to get the gamemode when needed. Something like this (this is assuming you have declared the gamemode object in your header file)

In your .h file

ABrawlerGameMode* GetGameMode();

in your .cpp file

ABrawlerGameMode* ABrawler_Character_Player::GetGameMode()
{
    if (GameMode != NULL) return GameMode;
    else
    {
       GameMode = Cast<ABrawlerGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
       return GameMode;
    }
}

So your call in SomeFunction() would be something like this

BMovementComponent->AddInputVector(FVector(0, 0, -1) * GetGameMode()->Gravity, true)

Hope this helps. It seems that you cant define it in the constructor since GameWorld() is null. You might want to try defining the GameMode variable in PostInitializeComponents().