x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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: http://www.tutorialspoint.com/cplusplus/cpp_pointer_to_class.htm 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?

Product Version: UE 4.10
Tags:
more ▼

asked Jan 25 '16 at 09:10 PM in C++ Programming

avatar image

Graylord
1 3 8 8

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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); 
      }
     
  }
more ▼

answered Jan 25 '16 at 10:22 PM

avatar image

Bariudol
576 20 12 27

avatar image staticvoidlol Jan 26 '16 at 06:16 AM

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.

avatar image Graylord Jan 26 '16 at 01:47 PM

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

avatar image Bariudol Jan 27 '16 at 03:24 AM

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().

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question