Announcement

Collapse
No announcement yet.

Get gamemode from character?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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?
    Last edited by Graylord; 03-26-2020, 06:34 PM.

    #2
    Code:
    GetWorld()->GetAuthGameMode<ACustomGameMode>()

    Comment


      #3
      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:
      Code:
      TWeakPtr<ATribesmenGameMode> GMode = GetWorld()->GetAuthGameMode<ATribesmenGameMode>();
      But I get the following error:
      Code:
      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?

      Comment


        #4
        You declare a pointer with *
        Code:
            ATribesmenGameMode* GMode = GetWorld()->GetAuthGameMode<ATribesmenGameMode>();
            if (GMode)
            {
        
            }

        Comment


          #5
          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:
          Code:
          class ATribesmenGameMode* GMode;
          in my cpp:
          Code:
          GMode = GetWorld()->GetAuthGameMode<ATribesmenGameMode>();
          Do I not want to define it as a class "variable"? Tried just
          Code:
          ATribesmenGameMode* GMode;
          as well, like I've found in some examples, but then I just get an error that it's missing ; before *.

          Comment


            #6
            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.

            Comment


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

              Comment

              Working...
              X