Announcement

Collapse
No announcement yet.

Getting access to the GameMode

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

    Getting access to the GameMode

    How do I get access to the GameMode class I have created from other classes?

    For example, imagine you have this:

    h...

    Code:
    UCLASS(minimalapi)
    class AMyGameGameMode : public AGameMode
    {
    	GENERATED_UCLASS_BODY()
    public:
    	FPointSystem* PointSystem;
    };

    cpp...


    Code:
    AMyGameGameMode::AMyGameGameMode(const class FPostConstructInitializeProperties& PCIP)
    	: Super(PCIP)
    {
    	// set default pawn class to our character
    	DefaultPawnClass = AMyGameCharacter::StaticClass();
    	HUDClass = AMainHUD::StaticClass();
    	PointSystem = new FPointSystem();
    }
    How do you then go and access the AMyGameGameMode object...

    I had a total guessage at it (Obviously it wrong):

    Code:
    AMyGameGameMode* GameMode = Cast<AMyGameGameMode>(AMyGameGameMode::StaticClass());
    GameMode->PointSystem....
    I see that they seem to be referenced as if they are static classes... How does this work, I find the lack of static declaration confusing if so...

    I also tried this:

    Code:
    TSubobjectPtr<AMyGameGameMode> GameMode;
    GameMode = PCIP.CreateDefaultSubobject<AMyGameGameMode>(this, TEXT("GameMode"));
    But that crashes the loader
    Last edited by jimmyt1988; 09-09-2014, 03:52 PM.

    #2
    of course that is wrong, StaticClass is to get the Class for comparaisons and spawning, to get the gamemode, you do this:
    Code:
    AMyGameMode * mymode= Cast<AMyGameMode>(GetWorld()->GetAuthGameMode());
    the GetWorld()->GetAuthGameMode(); function gets the current gamemode in any AActor, keep in mind, this wont work on clients on multiplayer game, as clients in a multiplayer game cant access the gamemode becouse the only instance is in the server.
    UDK and UE4 programmer and Unreal engine 4 betatester. Currently working on commercial VR games for PSVR.
    Deep knowlegde of C++ and blueprints. Open to freelance work.
    Games released, Deathwave(Steam), VRMultigames(Steam), DWVR(Steam,Oculus,PSVR):
    http://store.steampowered.com/app/463870
    http://store.steampowered.com/app/500360
    http://store.steampowered.com/app/520750

    Comment


      #3
      I just tried this before you posted (I thought I was on the right wave length!) but it throws an error:

      Code:
      	AMyGameGameMode* mode = Cast<AMyGameGameMode>(GetWorld()->GetAuthGameMode());
      
              // Unhandled exception at 0x00007FFCFEB08FC0 (UE4Editor-Engine.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000120.
      I'll keep at it... Most likely something silly i'm doing now... however, if you do know... deffo tell me Thanks buddoss!

      Might be worth noting i'm trying to access it from within the HUD class I created

      Click image for larger version

Name:	Untitled.png
Views:	1
Size:	42.9 KB
ID:	1056476

      Related posts: https://answers.unrealengine.com/que...ss-instan.html
      Last edited by jimmyt1988; 09-09-2014, 04:36 PM.

      Comment


        #4
        Looks like those objects are not in the current scope. Perhaps add a member to your HUD to hold a pointer to (or copy of) the game mode and fill it before entering the current state so you have data to look at.

        Comment


          #5
          Isn't this what I have?... You can see GameMode is of Type ATofuGameMode... and it's a property of HUD:


          Code:
          /**
           * 
           */
          UCLASS()
          class TOFUGAME_API AMainHUD : public AHUD
          {
          	GENERATED_UCLASS_BODY()
          public:
          	TSubobjectPtr<UHUDElementComponent> TofuPoints;
          	virtual void DrawHUD() override;
          	ATofuGameGameMode* GameMode;
          };

          Comment


            #6
            Then somehow it's not being set. Look at your screenshot - it's NULL....

            I see in your HUD constructor you try to get the game mode and set it - my guess is that it doesn't exist at that point.

            Comment


              #7
              Yes, that's correct. It's not there to grab yet... Hopefully someone knows why Thanks for your time though matey

              Okay, I hadn't quite grasped what vblanco was saying as it's a new part for me... but apparently it's not working because I am working on client... which I assume means.. my pc? rather than a server... I'll take a look at these PlayerState and GameState thingies... Any other parent declaration place?
              Last edited by jimmyt1988; 09-09-2014, 05:58 PM.

              Comment


                #8
                Is your HUD object global? If so, perhaps you can have the game mode register itself with your HUD when it comes up.

                Comment


                  #9
                  You should never try to access objects in the constructor because, as VegasRich pointed out, there is a chance that they haven't been constructed yet. What you should do is override the BeginPlay function and set the pointer there.

                  Comment


                    #10
                    Kumppi... The Hud doesn't have a BeginPlay, are you thinking of StartPlay with the GameMode? If so, I have moved into this now:

                    Doesn't make a difference unfortunately... but thanks for the warning.


                    Code:
                    ATofuGameGameMode::ATofuGameGameMode(const class FPostConstructInitializeProperties& PCIP)
                    	: Super(PCIP)
                    {
                    	// set default pawn class to our character
                    }
                    
                    void ATofuGameGameMode::StartPlay()
                    {
                    	DefaultPawnClass = ATofuGameCharacter::StaticClass();
                    	HUDClass = AMainHUD::StaticClass();
                    }

                    Comment


                      #11
                      Code:
                      void ATofuGameGameMode::StartPlay()
                      {
                      	DefaultPawnClass = ATofuGameCharacter::StaticClass();
                      	HUDClass = AMainHUD::StaticClass();
                              HUDClass->GameMode = this;
                      }
                      maybe? Hopefully?

                      Comment


                        #12
                        Unfortunately that will not work bro... Once again though, cheers for having a try!!!

                        Comment


                          #13
                          Isn't AHUD derived from AActor? If so it should have the BeginPlay function. Weird. Well game mode definitely has it.

                          Comment


                            #14
                            Kumppi, you're right!!! It does end up deriving from AActor... Thanks for slapping me down like the noob I am, hangon lemme see if this works now..

                            Well looooooky here... It no longer crashes... Now I've got to get this GameState working instead which I didn't realise I was supposed to use instead.
                            Last edited by jimmyt1988; 09-09-2014, 06:52 PM.

                            Comment


                              #15
                              hey jimmy, GameMode only exists on the server, so first of all make sure you only call it on the server and use the command that vblanko showed you. Don't use it in the constructor like you're doing since it might not exist yet there, use it in the BeginPlay. I hope it works out for you

                              Comment

                              Working...
                              X