Announcement

Collapse
No announcement yet.

How to create global variable?

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

    How to create global variable?

    When the player reaches the end of my level (Simple paper 2d side scroller game) the level complete widget pops up and asks them to Continue or Quit back to the main menu. If they continue I would like for the game to load the next level for them. But can't for the life of me figure out how to do this without having a global variable. I was going to have it so when they overlapped the trigger box, it would set the global variable to the next levels name, then when they click on the Continue button it would just load the level from the variable.

    #2
    If you require global variables, store them into a specific class you know will always exist, and cast to that class. A commonly used placed to store "global" variables is via a custom gamemode blueprint, since a gamemode will always exist and there will always be only one by design. Or you can create an empty actor and store them there too, you just have to remember to always include one in each level/scene and cast to it. Since it's not a gamemode you can't simply use the Get GameMode ->Cast To MyGameModeCustom type deal. But in either construction or BeginPlay you can add a GetAllActorsOfClass class:MyCustomGlobalVariableStoringBlueprint, get index 0, set variable reference MyCustomGlobalVariableStoringBlueprintRef, and then all you need to do is get your referenced variable.

    One advantage of having it in a custom "empty actor" is you can keep your gamemode clean of clutter, if for example, depending on the map you use a different gamemode class but want to store the same global variables. On the flip side, you can make a custom gamemode class, store all the global variables you know you are going to use all the time there, and then create child blueprints of that class for different gamemodes. Since all of the different gamemodes are children of your first custom gamemode, they will all have the ability to store and use your "global" variables. Result being knowing each of your custom gamemodes all inherit your global variables from the parent class, while being clutter-free.
    Indie Dev - Whiskey Cat Softworks
    Currently developing a colony survival game!

    https://twitter.com/WSoftworks https://www.facebook.com/WhiskeyCatSoftworks/

    Comment


      #3
      Originally posted by Deathstick View Post
      If you require global variables, store them into a specific class you know will always exist, and cast to that class. A commonly used placed to store "global" variables is via a custom gamemode blueprint, since a gamemode will always exist and there will always be only one by design. Or you can create an empty actor and store them there too, you just have to remember to always include one in each level/scene and cast to it. Since it's not a gamemode you can't simply use the Get GameMode ->Cast To MyGameModeCustom type deal. But in either construction or BeginPlay you can add a GetAllActorsOfClass class:MyCustomGlobalVariableStoringBlueprint, get index 0, set variable reference MyCustomGlobalVariableStoringBlueprintRef, and then all you need to do is get your referenced variable.

      One advantage of having it in a custom "empty actor" is you can keep your gamemode clean of clutter, if for example, depending on the map you use a different gamemode class but want to store the same global variables. On the flip side, you can make a custom gamemode class, store all the global variables you know you are going to use all the time there, and then create child blueprints of that class for different gamemodes. Since all of the different gamemodes are children of your first custom gamemode, they will all have the ability to store and use your "global" variables. Result being knowing each of your custom gamemodes all inherit your global variables from the parent class, while being clutter-free.
      Appreciate it, the empty actor worked like a charm.

      Comment


        #4
        You can use the GameInstance. It persists from Start of the Game to the Termination.

        Comment

        Working...
        X