Announcement

Collapse
No announcement yet.

Transfer UMG input text to other levels?

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

    Transfer UMG input text to other levels?

    Hi all,

    I have a high score widget. I want to collect the users name. Am I correct that I use input text in the UMG widget for this?

    If so, how do I capture and send this data to the rest of the game? Right now I have the user name collected in a menu level with widget, but when I load the first level it goes away. The only built in events in the UMG are onTextChanged and OnTextCommited, but these don't fire when I click on the "open level" button. Other than a save game, is there a way to push/retain a variable from a menu screen level to when the actual game level is loaded?

    I have gone down the rabbit hole and cobbled together some gross looking wires, and still not got the functionality I need. I am sure I am missing something.

    Please help! Thanks in advance.

    ~ Moojamboo

    #2
    You need to store stuff like that in GameInstance class as that hangs around between level loads.

    Comment


      #3
      GameInstance is only persistent while you run the game. For score keeping you'd really need to write the info to disk.

      In content browser, create a Struct called PlayerScores. Edit it so it has a string called Name, and an integer called Score.
      In content browser, create a SaveGame class called YourSaveGame.
      Edit the YourSaveGame class: add a struct variable of PlayerScores, then click the array button (so you can save an entire array of data).
      In content browser, create a YourGameState class.
      In the world settings, make sure your GameMode uses YourGameState.
      Edit YourGameState, in the eventgraph, on beginplay fire a Does Save Game Exist? with a slot name, such as slot1
      Branch, if true, fire a Load Game From Slot (slot1) ... if false, fire a Create Save Game (slot1).
      For both, promote and set variable YourData (of type YourSaveGame).

      Now all you need to do is tell your UMG widget to send the player input string (if it's not nothing) and score integer to the GameState and make a PlayerScore struct, which you commit using SetArrayElement to YourSaveGame struct array.
      There are several ways to send variable values between UMG widget and another class or actor. (Look up Blueprint Communication). From UMG you can Get Gamestate, cast to YourGameState, then call a CustomEvent or function in it and pass in the variables as inputs.
      And on Load Game From Slot, you can lookup YourGameState>YourData>PlayerScores array, and get the array entry with the highest score, or print the entire array to a text slot of some kind.

      Remember it can be helpful to make variables you'll pass here and there to be Editable and Exposed on Spawn, whatever helps them to be visible and changeable from elsewhere. And compile and save before looking them up in another blueprint.
      Remember you may need to delete from /GameProject/SaveData/Saves/ your .sav file if you later make some project change which disrupts the current values in it during development. Say, if you add an extra feature to the struct you want to save.


      FYI -- TappyChicken in the learning content includes a demo of saving/loading data such as a score.
      Last edited by tomofnz; 12-14-2015, 07:02 AM.

      Comment


        #4
        @Anadin & @tomofnz - thanks for your help. Now that I know that the variable is NOT hanging around between levels, I can go from there.

        Isn't it weird that a variable put in the HUD for example (in the menu) isn't available in the game level when it is using the same HUD? From a 50,000 perspective I would have thought that if I cast and set a variable in the HUD from my menu widget, it would be still be there in the next level as it is the same HUD? Is it NOT there as the next level is resetting everything, and drawing a new HUD? Something like that?

        Happy Monday!

        Comment


          #5
          Yup, I expect it creates a new HUD once the level is loaded. Not a lot is persistent between level loads. Streaming levels can be used to avoid that happening though.

          Comment


            #6
            I will add

            1. how can you make a loading x, y, z page between levels?
            2. In Unity y have DontDestroyOnLoad, is there a similar thing in UE4, or how you need to handle this?

            Comment

            Working...
            X