Get gamemode from character?

Hi, sorry for posting rather rudimentary questions, but my search results are getting flooded with blueprint rather than c++.

I wish to get my active gamemode and trigger a function in it from my character, is there a function or pointer that already gets or holds a reference to the gamemode?
If not, how do I get it?


Thanks! I’m starting to get something going with this, but I’m a bit rough on pointers and casting still.

I got this so far in making my own pointer:

TWeakPtr<ATribesmenGameMode> GMode = GetWorld()->GetAuthGameMode<ATribesmenGameMode>();

But I get the following error:

no suitable constructor exists to convert from "ATribesmenGameMode *" to "TWeakPtr<ATribesmenGameMode, ESPMode::NotThreadSafe>" Tribesmen X:\Unreal\Unreal Projects\Tribesmen\Source\Tribesmen\TribesmenCharacter.cpp

How do I make a pointer towards an instance of a custom class?

You declare a pointer with *

    ATribesmenGameMode* GMode = GetWorld()->GetAuthGameMode<ATribesmenGameMode>();
    if (GMode)


Thanks! I got it working perfectly on its own.

However I tried to make the pointer globally accessible, and it literally crashes my project on load. (It builds though)

In my header:

class ATribesmenGameMode* GMode;

in my cpp:

GMode = GetWorld()->GetAuthGameMode<ATribesmenGameMode>();

Do I not want to define it as a class “variable”? Tried just

ATribesmenGameMode* GMode;

as well, like I’ve found in some examples, but then I just get an error that it’s missing ; before *.

9 out of 10 crashes are nullptr related so you are probably calling the function before any World is present or you use the variable before it has been set. Whenever you declare a pointer you then need to ensure that it is set so it is often easier to just get it from somewhere else whenever you need it unless if you need the variable very frequently like on tick.

Thanks for the tip! I fixed it by assigning it in beginplay rather than the constructor.