Announcement

Collapse
No announcement yet.

Global variable

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

    Global variable

    Hi
    I'm new with Unreal engine and I don't know how to use a global variable..
    I just want to share a data(variable) between blueprints but I cant ,
    so I've searched on a google got some tips and none of them worked..
      please help me..

    #2
    Create a GameInstance blueprint (for example, named "MyGI"). Then in Project settings > Maps & Modes, set GameInstance to be the one you just created.

    Then on your GameInstance blueprint create the variables you want to be global. Then from any other blueprint call 'Get GameInstance" then pull out that pin and "cast to MyGI." Then you can access your global variables and they will persist between level loads.
    Procedural, modular, on-the-fly animation - iTween For UE4
    - Actors - Components - UMG - Ease In - Ease Out - Path-constrained Animation - $0

    Comment


      #3
      I do the same thing as Jared Therriault, except I use my game mode for most of my global variables. My game mode is called "MyShooter", so anytime I want to get/set a global variable, I use a CastToMyShooter node with a GetGameMode attached, and then I get/set whichever variable/variables I want to change. This can be called from...I want to say anywhere that has a blueprint..ie. can be called from actors/pawns/widgets/HUD.

      There are different ways to pass variables between blueprints, but I think this is the best way if you are new to UE4.

      Comment


        #4
        Originally posted by samnater View Post
        I do the same thing as Jared Therriault, except I use my game mode for most of my global variables. My game mode is called "MyShooter", so anytime I want to get/set a global variable, I use a CastToMyShooter node with a GetGameMode attached, and then I get/set whichever variable/variables I want to change. This can be called from...I want to say anywhere that has a blueprint..ie. can be called from actors/pawns/widgets/HUD.

        There are different ways to pass variables between blueprints, but I think this is the best way if you are new to UE4.
        The game mode only exists on the server though.

        Comment


          #5
          Just read a bit on gamestate vs. gamemode, looks like you're correct DjSt3rios...I think however, for a single player game it would not matter which you use (gamestate/gamemode) for variables. Does that sound right?

          Comment


            #6
            Originally posted by samnater View Post
            Just read a bit on gamestate vs. gamemode, looks like you're correct DjSt3rios...I think however, for a single player game it would not matter which you use (gamestate/gamemode) for variables. Does that sound right?
            Yes, that is correct

            Comment


              #7
              But when both methods work same way (from implementing that variable functionality perspective) it is better to use "proper" way. Some day you may decide to add multiplayer, so why not stick to convention and design of engine from beginning. That also teaches correct behavior for future projects that may be multiplayer.

              Comment


                #8
                Originally posted by samnater View Post
                Just read a bit on gamestate vs. gamemode, looks like you're correct DjSt3rios...I think however, for a single player game it would not matter which you use (gamestate/gamemode) for variables. Does that sound right?
                In my example I suggest GameInstance. The reason is just that GameInstance is never destroyed as long as the game persists so the variables are truly global. GameMode requires you to load variables from a save game when loading a new level. In that case you could forgo GameMode altogether and just use the SaveGame. Of course you never have to worry about that if you use level streaming or never load another level. In the end it really depends on your type of game and personal preference - but I prefer GameInstance
                Procedural, modular, on-the-fly animation - iTween For UE4
                - Actors - Components - UMG - Ease In - Ease Out - Path-constrained Animation - $0

                Comment


                  #9
                  Originally posted by Jared Therriault View Post
                  In my example I suggest GameInstance. The reason is just that GameInstance is never destroyed as long as the game persists so the variables are truly global. GameMode requires you to load variables from a save game when loading a new level. In that case you could forgo GameMode altogether and just use the SaveGame. Of course you never have to worry about that if you use level streaming or never load another level. In the end it really depends on your type of game and personal preference - but I prefer GameInstance
                  This is very true. UE4 has done this right. I make this a place to store a lot of variables that affect the game itself and I put everything neatly in their own categories. When it comes to objects having their own configurations....like what clothing items are being worn and items in inventory....etc. I use a save on them individually.

                  Format of the Save is => Name<UID>_Clothing Name<UID>_Inventory Name<UID>_Stats
                  UID is a random Int32 from 1 to 2,000,000,000 that every 'Live' object receives in their properties blueprint. This is so I do not have to rely upon the UE4 ID and can search IDs of objects based upon this ID which is easily grabbed in an array.

                  Comment

                  Working...
                  X