HTF do I? Use the GameInstance Object in Unreal Engine 4

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • How do I use the GameInstance Object in Unreal Engine 4?
    Source Files: github.com/MWa...

Komentáře • 82

  • @xylvnking
    @xylvnking Před měsícem +1

    another night i find myself up at 2am watching your tutorials. thanks man

  • @brunomilcos
    @brunomilcos Před 7 lety +24

    I love your tutorials, always very clear and to the point! Thanks!

    • @tailwhipolaf
      @tailwhipolaf Před 4 lety

      I was going to same the same thing lol

  • @projectlamentation4093
    @projectlamentation4093 Před 4 lety +3

    Wow this was a very thorough, easy to understand tutorial. You are really good at explaining things!

  • @metafuel
    @metafuel Před 4 lety +1

    You rock Mathew. I hope you and your loved ones are safe.
    Thank you for sharing your knowledge in such a brilliant and concise manner.
    Much love in crazy worldwide quarantine times.

  • @FitGameDeveloper
    @FitGameDeveloper Před 5 lety +1

    Thanks! Your tutorials are always the most-helpful on any topic they cover. Having the debug buttons to test the variable getting/setting was critical for my problem troubleshooting.

  • @MattJSowards
    @MattJSowards Před 4 lety +1

    is there a way to rename an existing game instance, i want to parent one but they're both identically named the same. any solution
    i renamed it by right click rename with no errors but it still shows original game instance name when going to add it and same issue/

    • @TorQueMoD
      @TorQueMoD Před 3 lety

      If you hover over the two GameInstances, the only one that's actually the root GameInstance from the engine, will have a long description as a tooltip. Any custom game instances will only show the name of that GameInstance as its tooltip. This is how you can tell them apart if they're named generically. You always want to parent from the root GameInstance when you make your own.

  • @terraint3697
    @terraint3697 Před 6 lety +3

    Thank you as always for the straight to the point information about what I am looking to learn! Amazing!

  • @iangriffiths2483
    @iangriffiths2483 Před 5 lety +2

    Cheers for this video Mathew, been hitting my head against the wall all day looking to transfer data between two game modes.

  • @mryonmusic253
    @mryonmusic253 Před 4 lety

    Dude. Thank you. You saved me from insanity. Not sure how I missed the GameInstance setting.

  • @chaotickreg7024
    @chaotickreg7024 Před 2 lety

    This is an important video. I think the easiest way to think about the Game Instance is it's your "Save File." If the game needs to check your wallet or game progress then it will check your game instance in much the same way it would check a save file.
    Save games should probably be saved as a separate file but that ruins my metaphor.

  • @DevinDTV
    @DevinDTV Před 7 lety +8

    Two possibly related questions: Why store things in the player blueprint at all instead of just directly in the game instance? What if you have multiple players loading new levels, how do you store their stats individually? I love your videos btw.

    • @baronvonbeandip
      @baronvonbeandip Před 2 lety +2

      It's the same idea as public and private functions. If the information is shared between objects (and is persistent), store in the instance. If they are only referenced by the player (and are transient), store it on the player blueprint.

  • @samarmstrong7374
    @samarmstrong7374 Před rokem

    Thanks Mathew, you are an excellent teacher

  • @LeonHeytens
    @LeonHeytens Před 2 lety

    Just what I needed

  • @VinayakRawas
    @VinayakRawas Před 3 měsíci

    8 Years Ago.... isnt he The Messiah we Talked about

  • @NinjaThug
    @NinjaThug Před 3 lety

    Awesome info, as always!

  • @platinum3d77777
    @platinum3d77777 Před rokem

    You SAVE MY LIFE, AGAIN!!!

  • @cosmotect
    @cosmotect Před 2 lety

    Now to understand why you would use game instance over storing things in the game mode

  • @NashMuhandes
    @NashMuhandes Před 8 lety +2

    Hi! Very helpful video. Can you do a WTF and/or HTF for things like GameState, PlayerState and GameMode? Thanks for your amazing channel!

  • @thankyou991
    @thankyou991 Před 2 lety +1

    Thank You

  • @myunglee8163
    @myunglee8163 Před 6 lety

    Your tutorial is awesome. Really. Thanks a lot and please keep on going!!

  • @mantronicsenterprises
    @mantronicsenterprises Před 7 lety +1

    Thank you very much for this, it has made Game Instances much clearer, and how to use them now makes more sense.
    You mention at 6:40 that you are not using the appropriate way to have the values automatically save and load the player health, do you have another tutorial on the "correct" way to set this up?

    • @Novalis1504
      @Novalis1504 Před 7 lety +4

      He always has to get the game Instance and cast it into a GameInstance Object.
      A better way would be to store a Reference to that GameInstance at BeginPlay or the Construction of the Widget and then use this Reference to access the variable inside the GameInstance.
      with a library example: He is going into the library to learn something about history. Everytime he wants to do some research for a specific question. He goes to the shelf, searches for the book (get Game Instance), makes sure this is really the book he wants (cast to Game Instance), uses this book for his one question and puts the book back into the shelf, and immediately forgets where the book is standing. So everytime he wants to do some research, he would have to go through this process.
      With a reference to this book (or Game Instance), you don't have to validate, that this object/book/Game Instance is really the one you need.
      Maybe that's what he means. I hope I was able to help you out :)

  • @powerpc6037
    @powerpc6037 Před 4 lety

    So it's best to use this instance to store all your data you wanna keep during the entire game? Stuff like player's health, mana, experience, level, his entire inventory, quest progression, bosses/monsters killed which aren't meant to be spawned yet and alot more. But that's ALOT of data to store and set back every time you change a level. Why not simply store all that data there permanently and keep it only there? No need to swap back and forth all that data. The inventory can be 150 slots big and there may be over 1000 quests which all have different goals, states and stuff. That would make a huge function to transfer data all the time.

  • @MrManet
    @MrManet Před 3 lety

    You're a life saver, thanks!

  • @RyanSturtevant
    @RyanSturtevant Před 8 lety +1

    Thanks for all of your great videos! I wanted to ask if you thought this was the right work flow if you have various items in different levels. I'm thinking about an RPG, all of those lootable containers. Would you really make a bool for each in the game instance. If not, someone could just come back to the level and the container would be back. How would you approach the problem on many things to track when streaming in and out many levels?

    • @MathewWadsteinTutorials
      @MathewWadsteinTutorials  Před 8 lety +1

      Honestly I wish I knew a "good" answer to this but I have never created a game with such large scope before. If I was approaching something like that it would depend on how large it would be and how I would design it. Let us say you have an RPG with streaming levels (say caves or dungeons) and the layout is fixed along with chests. Each chest would probably pull from a loot table to determine what can drop which means something somewhere would keep track of what chest has which loot table. I wouldn't see a big issue with some form of a master save file that keeps track of things looted like in a key:pair system such as the chest ID and if it was looted or not. All of this could be in a separate file, like a CSV or Data Table, to allow you to easily control what is where.
      As for saving and loading and looting, if your save game object contained an array of structs (your key:pairs) that was simply added to each time you looted a chest you could use this as a reference when you created the level for spawning "unlooted" chests to prevent chests from being looted twice. By only adding in the key:pair when they loot you can restrict your file size to only being as large as the player needs as they play.
      Is this the right idea? I honestly don't know. Would I do it like this the first time? You bet I would. If something did not work out right or broke I would see what I could to do fix it and learn along the way =) Actually now that I read this over your save game object wouldn't even need a key:pair or struct. A simple array that has the chest ID (which should be unique) that you update whenever your player loots would solve the issue as it's either in the array (looted) or not in the array (unlooted).

    • @RyanSturtevant
      @RyanSturtevant Před 8 lety

      Wow that's an epic answer. Thanks for taking the time. I do have loot list system for lootable actors. I guess the way to go is just an array with the ID's. Would not be too hard. Somehow I hoped that Unreal would just save the state of the level automatically on unload or there would be some kind of option. Not a bad feature request.
      More annoying is coming up with the logic to run at begin play in the level BP for all things that could be changed. I guess that's not too complicated... Still it all adds up.

    • @MathewWadsteinTutorials
      @MathewWadsteinTutorials  Před 8 lety

      I am sure you could write a plugin or something similar to do what you want, I know there are plugins on the Marketplace that can do things like a save state. It's a lot of work but might be fun to try if you have the time =)

  • @Odb718
    @Odb718 Před 4 lety

    Thank you for making these.

  • @MegaCevapcic
    @MegaCevapcic Před 8 lety

    Damn you really explain things well :)
    A disclaimer before the question: All my coding experience comes from the 2 weeks Ive been messing around in UE Blueprints, which is why some stuff flies right through my stupid head.
    Now for my big-ass question: Ive made a "master blueprint" character with variables from a Structure (which contains integers, text and a boolean).
    In the Construction Script of that blueprint Ive made an elaborate web that sets all these variables based on a Random Stream, which gets a random seed every time the Construction Script is called(was apparently necessary because the spawned child characters would have the same seed).
    In the Gamemode BP I spawn childs of "master BP" on EventBeginPlay which spawn perfectly (all variables get set randomly etc.) every time I play the level.
    My question is basically will all the actors created get their variables "reset" because the Construction Script will fire off again when I reload the level? I was told I wouldnt have to worry about the Construction Script once I hit play -.-
    If I collect all spawned character via "get all actors of class" in an array I guess I could set them in the Game Instance so they get stored no matter what I do and update them in Game Instance every time their variables change, or do you think its best I just trash everything in the Construction Script and just make a CustomEvent called "Birth" which will set all the variables once the child character "leaves the womb" :P
    I guess this is more a question about the Construction Script than game instance and sorry for the text wall, I usually prefer asking through comments than opening forum threads, I hope you dont mind :)
    In any case keep up the good work, your videos are great.

    • @MathewWadsteinTutorials
      @MathewWadsteinTutorials  Před 8 lety +2

      +MegaCevapcic The construction script is basically called one time when the item is created. If you are creating them in the editor the construction script is called then. If you reloaded the map with this character in it they would not get the construction part called again due them already being in the map and having it done already.
      If you create them at runtime, as in spawn an actor, then the construction script will be called in for that one time.
      So yes you don't have to worry about the construction script once you hit play *if* those things are already in the level.
      Depending on what you want them to do should decide if you use the script or not. Traditionally the construction script would be used for things in the editor, such as randomizing a look of a building during map building or some other such thing. Have a "Birth" event or function that is called on begin play for the actor might be better if you plan on spawning these during gameplay.

  • @BlogTornado
    @BlogTornado Před 6 lety +1

    Hello Mathew. Thank you for all these amazing tutorials!!! I just had a question on how do Game instance and Game save communicate with each other. I know that Game instance likes to collect information but what's not clear to me is how do you get Game Instance information to be saved in to Game save. That would be Score text in particular. Would there be FTW tutorial on these as well?

  • @ErikRoberts1981
    @ErikRoberts1981 Před rokem

    Hi Mathew, this seems to be more or less what I'm looking for. However, instead of setting health, I'm trying to set the "play to" score where the player can set the desired score that they want to play up to. Is there a better way to do this instead of how you've done it above?

  • @Manim8
    @Manim8 Před 3 lety

    Is there an absolute need for Game Instances? I've been doing everything in the GameMode and SaveGame and all is working well: saving all values during 'Load next Level' stage etc. So is a Game Instance essential or optional?

  • @rahultandon9749
    @rahultandon9749 Před 2 lety

    superb

  • @LDT7Y
    @LDT7Y Před rokem

    How big should the game instance file ideally be at maximum (for a data heavy rpg, specifically)? I'm a bit concerned (perhaps unneccessarily) that my game instance is already about 20mb and I haven't even added most of the 400+ quests to it yet.

  • @khydrain
    @khydrain Před 5 lety +1

    I don't know what I would if you haven't done those videos.

  • @vaughanmacegan4012
    @vaughanmacegan4012 Před rokem

    I am a noob in that I have done some small courses but still don't understand a lot of things. Now in this tutorial you have a bluse line running off the Generic Cast to a spot called "target". I don't have anything like that in my Set box. What am I doing wrong?

  • @primitivepatterns
    @primitivepatterns Před 3 lety

    really caught off guard when yr intro audio didn't play as usual lol

  • @MartKart8
    @MartKart8 Před 2 lety

    All I'm finding is videos on health when it comes to game instance and nothing else, would of been good if it was a collectable?
    Sure the health could be good for going to a next level, but when I turn the game off I might want my health to be back at the top and collectables I have kept on me?

  • @inchjan3203
    @inchjan3203 Před 6 lety

    man your tutorial are very good and very well explained. Can i request one tutorial i didnt find it on any of the tutrials! How can i get text on editable text box like for example you want to make it as speed of all the AI or getting it as number of spawn in all target you want to spawn it. ?

  • @jiwik731
    @jiwik731 Před 10 měsíci

    Cant find the answear or i am just asking google wrong questions but if i will save data of parent into the game instance will these data be saved also for a child or i have to make separate values in instance for each child? Each child will have different values of these variables.

  • @jorhoto7064
    @jorhoto7064 Před 5 lety

    Lets say you are working in an RPG game plenty of levels. Does it mean you need to be constantly storing/loading all your actors instance values? :S

  • @proudarts9127
    @proudarts9127 Před 6 lety

    If you were holding what levels have been completed, will you have to save the Game Instance to disk, would it be better to use the Save Game?

  • @GloriaAlMetalOldSkull
    @GloriaAlMetalOldSkull Před 2 lety

    hi bro how can i keep the objects obtained in the level before to a new level ? thank you so much.

  • @DragonFang253D
    @DragonFang253D Před 11 měsíci

    Can we get a version that is "the appropriate way of doing this"

  • @emilybrear2601
    @emilybrear2601 Před 6 lety

    In the "get health" part of the blueprint I run into an error on the last "set" node. It won't let me cast a character object reference to a game instance object reference. Implemented the source file and it still threw an error. Think UE4 ver 18 doesn't like it...

  • @setams4957
    @setams4957 Před 3 lety

    you were so usefull thank you

  • @ozcanozteke284
    @ozcanozteke284 Před 3 lety

    With the hope of someone seing this: I have created an array to save chosen player classes in a lobby. Players travel to a new level after selecting their characters, the level uses a new game mode. It seems like the length of the array is alright. but if I print out the contents, it's all blank. Is there something special with arrays? Thanks!

  • @hallo-welt
    @hallo-welt Před 6 lety

    Excellent thanks

  • @angrybonobro8711
    @angrybonobro8711 Před 6 lety

    Usefull tuto !

  • @RWThurman
    @RWThurman Před 3 lety

    You're as handy as a pocket on a shirt!

  • @user47033
    @user47033 Před 4 lety

    Can you get the BP Game Instance in C++? Or do I need to recreate the instance in C++?

  • @legacystudio1119
    @legacystudio1119 Před 5 lety

    Thank you!

  • @umbakarnask3844
    @umbakarnask3844 Před 6 lety

    Sorry but something is wrong. Probably old version of the engine because I cannot accomplish what you did with "Cast to FrameworkCharacter"

  • @maj2369
    @maj2369 Před 3 lety

    make this with actor please im stuck, when i load my other level the reference is destroyed

  • @StormyDoesVR
    @StormyDoesVR Před 5 lety

    Tried to use this to talk between a persistent level and a sublevel in the Robo Recall mod kit, unfortunately the mod kit doesn't allow other instances. oof. xD

  • @IoSkyBoyoI
    @IoSkyBoyoI Před 2 lety

    But what about booleans?

  • @DimitryArsenev
    @DimitryArsenev Před rokem

    Its looks unusable. I need more classes to store variables between levels. Example: quests - for store quests, items for store items, and etc. Some components for Game Instance, but it not support components.

  • @windflier2169
    @windflier2169 Před 7 lety

    So if I make a RPG game, When I open a new level, I need to store all of things(Like inventory's item,Character state, Character info,etc) into gameinstance, then get it out? This is so complex……

    • @MathewWadsteinTutorials
      @MathewWadsteinTutorials  Před 7 lety +1

      There are certain things that are part of the engine and around all the time and other things that only exist when created and needed. Maps/Levels destroy almost everything when they are loaded and unloaded so you need a way to store things on a map change if needed. The Game Instance is an object that is created when the game starts and then destroyed when it ends so it is around all the time and is a great place to store things that you want to persist between loading different maps. An alternative to this would be to use the Save Game and save anything you want to disk before you load the new map as well.

    • @j.r.vosovic1347
      @j.r.vosovic1347 Před 7 lety

      I am also using an RPG system and would love some help on this. The example I think is not up to date for 4.15+. Any chance you could walk through Item/equipment saving?

  • @itanshi
    @itanshi Před 3 lety

    Oddly your game project file has been stuck on 39% for some time now. even so, thanks for the great tutorial. Ill just retry it. eh, its stuck
    guess it needs an update for 4.26
    edit: ok i restarted pc, it loaded ok

  • @xavierloux
    @xavierloux Před 7 lety

    Thanks

  • @theshrk8977
    @theshrk8977 Před 6 lety

    thanks

  • @moviemonster99
    @moviemonster99 Před 7 lety

    Sir. I wold like to ask how to reset my coin count back to 0 after restarting a game. I have a PLAY button i my main menu that only opens first level but the coin count does not reset back to 0. My coin bank is stored in my game instance. Could you please explain what i need to add on Play button pressed to open level with reset (00) text coin widget. Sir you are the Best. Thank you.

    • @MathewWadsteinTutorials
      @MathewWadsteinTutorials  Před 7 lety +1

      The Game Instance is persistent, it will keep all of the data until the game is closed. The same way you increase your coin count in the game can be used to reset it back to 0. You need to set the Coin Count value in your Game Instance back to 0 whenever the main menu is loaded, or even when you hit the play button.

    • @BlogTornado
      @BlogTornado Před 7 lety

      Sorry I already have fixed the problem exactly the same way you suggested with Coin Count value. Thank you for that! How ever these reset now creates new problem. The Coin Count value reset deletes the Highest Coin Count . How can I set up my Coin Count Widget in Main Menu to show only The highest Coin Count without being reset on button pressed. The reset, resets the Game Instance along with the highest score. Mr. Wadstein Please ignore the question below. Could you please explain to me how to keep the Highest Coin Count in Coin Widget without being reset on PLAY. Thank you Sir!!

    • @MathewWadsteinTutorials
      @MathewWadsteinTutorials  Před 7 lety +1

      I am assuming you have 2 variables so this should not be much of an issue. Your game Instance can hold both if you want. Coin Count and say Highest Coin Count should not both get reset if you are just resetting one of them. I don't know how you are resetting them but simply setting your Coin Count back to 0 and not touching the Highest Coin Count should leave the Highest alone.
      Is your Widget not showing the Highest Coin Count and maybe showing the Coin Count?

    • @BlogTornado
      @BlogTornado Před 7 lety

      You Sir are exactly right. I do have Coin Count and Coin Bank. Coin Bank is stored in the Game Instance and yes I'm only resting Coin Bank on button pressed with Set Coin Bank (0). So in my Coin Widget I should in Main Menu I should set my Coin Widget in Biding with Coin Count instead of Coin Bank. I think? Please correct me if I'm wrong but I'm gonna try it and will let you know if it worked. Thank You Sir!!

    • @BlogTornado
      @BlogTornado Před 7 lety

      Sir. Wadstein. I did as you suggested it's working OK. I added new Integer (HighestCoinCoins) and replaced with Coin Bank. What I did is add another invisible coin on top of other. Main Menu has it's own Coin Widget and Levels have their own. So visible coin goes to the levels Coin Widget and invisible one goes to Main Menu Coin Widget. So everything OK there except that lets say I collected 10 coins they go to main menu and then I play again and reset the coin in the level and when I collect lets say 5 more then on reset they are added to the 10 coins. So coins from the new game started are now added on top of the 10 ones stored in the Game instance. They should not be added but instead added only if I surpass the 10 coins collected. Sorry I'm kinda complicating here. Cant quite visualize whats going on here. If you could Please shed some light on these Sir. I would greatly appreciated. So how would you set it up if you had one coin on top of each other one going to the coin count and another going to another Coin widget in main menu. So I'm only erasing coins inside the levels on button pressed and not the one in Main menu Which is also stored inside Game Instance. Sorry about the long explanation. Thank You.