What Is A Game Instance And Why You Need One - Store and set variable info between UE4/UE5 levels

Поділитися
Вставка
  • Опубліковано 3 лют 2025

КОМЕНТАРІ • 55

  • @commandingsteel
    @commandingsteel Рік тому +3

    Thank you Sir!...took me a little bit to integrate it into my project, but now I'm well under way into having a functional game instead of just a level
    cheers

  • @studiobaxter1242
    @studiobaxter1242 Рік тому

    Wow, thank you. Been building my game for a year as a total beginner. Only ever had one level until last week, but was a bit lost as to how to progess. Just got something to save between levels and am feeling elated! Subscribed.

  • @IrocZIV
    @IrocZIV 2 роки тому +6

    I like that you explained the why, and showed some issues that you might experience. It is a bit long, feels like it would have been a good livestream, and then a more concise video could be made from it after you worked out the bugs in the presentation. Thanks for the video.

    • @GDXR
      @GDXR  2 роки тому +3

      If this was a live stream it would have been much longer lol.

  • @I-MM-O-R-T-A-L
    @I-MM-O-R-T-A-L Рік тому +1

    did i ever told you how much i love you? You are helping so much, great work, great explanations.... continue as you do!

  • @stoneageprogrammer432
    @stoneageprogrammer432 Рік тому

    Hey this is a cool video and I've subbed you. I've been studying GameInstance lately, but I'm still very new to GameDev and Unreal. I'm making a game with multiple levels, and discovered that the gun that I found in one level does not transfer into the next level. So I discovered GameInstance, but I still don't know how to code that. I got a note from another dev who described the logic. Here is what he wrote: "Gameinstance is like this. 1. You create a Gameinstance. 2. You set in Project Setting to use your Gameinstance. 3.Before you switch a level you save all what you want keep to the Gameinstance. 4. After you switched the level. You call the Gameinstance and load all the things which you saved in the Gameinstance back to the new level. I don't know how you make an inventory System but I think they use data tables. Lets make an example with your gun. Lets say step 1 and 2 you did. Step 3 is : Save the gun mesh in your GameInstance as a gun mesh variable. 4. You are switching the level. In the new level you need set your gun mesh. So you call your Gameinstance, find the saved gun mesh and use this saved gun mesh to set the gun mesh in the new level. Now your new level has the gun from before. I am not sure if this is the best way but as a start point it should work."
    Like I said, I don't know how to turn that into Blueprints. Do you think you might do another tutorial demonstrating this procedure?

  • @jazzypizzle
    @jazzypizzle 10 місяців тому

    Super understandable and well explained. Thank you sir!

  • @TheD3vilHimself
    @TheD3vilHimself 2 роки тому

    Bro, Nice Shirt! I see you are a man of class and style.

  • @FlockersDesign
    @FlockersDesign 11 місяців тому

    Its funny when he says his is just from the top of my head
    When he reads the documentation from Unreal them self about GameInstance😂😂

    • @GDXR
      @GDXR  11 місяців тому

      The examples are off the top of my head.

  • @TheJesusjones
    @TheJesusjones 2 роки тому +1

    Amazing video Jonathan - very well explained and demonstrated!

  • @StormFrontGame
    @StormFrontGame Рік тому +1

    thankyou, this has helped alot!!

  • @sams_3d_stuff
    @sams_3d_stuff 2 роки тому +1

    Hey! Love your videos!

    • @GDXR
      @GDXR  2 роки тому +1

      Glad you like them!, Thank you.

  • @katiekaliber
    @katiekaliber Рік тому +2

    Oke i was good with everything until you started doing the initialize in the instance and then told us to call it in our player blueprint while doing something different yourself, i can't for the life of me figure out what you mean by that because i can't call upon that custom event from my player if made in the instance. Could you explain what you meant?

  • @amitbeharier5698
    @amitbeharier5698 Рік тому

    Amazing. Thank you!

  • @chasesommer
    @chasesommer Рік тому +1

    If the variables are not showing up at 10:00, try setting Health and Stamina as text instead of integers. That's working for me now in 5.2. I'm sure there's a better way and would love some feedback!

  • @sebastianosorioarango6270
    @sebastianosorioarango6270 Рік тому +1

    I dont understand the minute 19:00, i have to create a custom event in the GI or in the ThirdPersonCharacter BP?

    • @emptybruh
      @emptybruh Рік тому

      If you are going the custom event rount, you make the event inside tpcharacter, attach everything you want to that event than cast to tpcharacter bp inside instance and run said event on Event Init

  • @Pierraxl
    @Pierraxl Рік тому

    Why don't you just plug the "As game instance " from the cast directly into the Set integer ? Instead of creating a variabale et get it again right after ? thanks

  • @mch43856
    @mch43856 2 роки тому

    really helpful thank you!

  • @jimmypi2035
    @jimmypi2035 2 роки тому

    Great tut, thank You. May I ask, what if we store some values/variables in actors and pawns that are dynamically spawned as the game progress? How to cast to those objects even if they are not in the scene yet :| :??? hlp

  • @CronicGaming94
    @CronicGaming94 Рік тому

    I'm only having an issue with one item. Which is the first person rifle component. Everything else seems to transfer fine but the rifle does not. Any idea how to fix this? The bHasRifleVariable is still stored when i changed levels but the rifle itself is gone.

    • @epic-quest-tales
      @epic-quest-tales Рік тому

      You need to store the rifle as actor object reference, or enum from where you chose the actor to spawn.

  • @rossedwards5724
    @rossedwards5724 2 роки тому

    A very clerar tutorial, thank you

    • @GDXR
      @GDXR  2 роки тому

      Glad it was helpful!

  • @DimitryArsenev
    @DimitryArsenev Рік тому +1

    I need more when 1 Game Instance. I cant make all logic in one class. How its can be fixed?

    • @GDXR
      @GDXR  Рік тому

      you can only have one game instance, you will need to implement a character possessions system and look into the Player Controller Class

    • @DimitryArsenev
      @DimitryArsenev Рік тому +1

      @@GDXR
      No thanks.
      Example - i make logic for sql database - its not Player logic.
      Now i just use static classes - i init it in game instance class.
      Also i think possible make many Game Instances - its subcomponents logic, but i not try it.

    • @ionthedev
      @ionthedev Рік тому

      ​@@DimitryArsenev unless you customize the engine, you can't have more than one game instance, as each time a packaged application made in in unreal is ran, the game instance is created on the opening of the application and is destroyed on closing the application. That is just how unreal works
      For your use case, which I've done something similar with Mongo, is to just write the data directly to the database instead of storing it in the game instance directly.

    • @DimitryArsenev
      @DimitryArsenev Рік тому

      @@ionthedev
      I just use UGameInstanceSubsystem, its way to have unlimited game instances.

  • @FDOF_Dev
    @FDOF_Dev 2 роки тому +1

    Question... why not just reference your game instance at begin play and then get that reference, and pull off of it and setting the desired variables directly as they change? And then you could use those variables from the game instance directly for your binds in your widget, right?

    • @FDOF_Dev
      @FDOF_Dev 2 роки тому

      Oh thanks BTW. The part about how event dispatchers and how using an object reference in the game instance instead of checking from each chest, solid advice. Well explained.

    • @GDXR
      @GDXR  2 роки тому +5

      So this isnt a bad way of thinking about it however it can cause issues. For example if your game instance loads first and you then your setting variables it can cause other things refrencing it to pull null values because they havent been set yet. For example UI.
      Is your player spawns and has a ui set on begin play. the UI can comunicate to the game instance. But if the player spawns, cast to the game instance and then sets a variable. It's possible your UI has already spawned before the game instance sets the variable. So if the UI is trying to access something that hasnt been set yet. It will break. You need to consider the load order of things.

    • @FDOF_Dev
      @FDOF_Dev 2 роки тому

      @@GDXR oh okay. Cool. Thanks for the heads up.

    • @katiekaliber
      @katiekaliber Рік тому

      @@GDXR Thank you, that makes a lot of sense.

  • @LDT7Y
    @LDT7Y Рік тому +1

    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.

  • @KavanBahrami
    @KavanBahrami 2 роки тому

    25:10 reminding you to link the event dispatcher video in the description ;)

    • @GDXR
      @GDXR  2 роки тому

      Thank you.

  • @gordon7936
    @gordon7936 Рік тому

    Is casting to game instance good idea If I need to have variables that i might change every frame? I'm told casting is not great for performance, so what's better way to keep global variable, even per level, not the whole game?

    • @GDXR
      @GDXR  Рік тому +2

      So the game instance is where you will want to keep global variables, however these can also be stored in Structs to stop the game instance having to much to manage. For the casting, casting to the game instance isnt bad, it always exhists so the game instance cast will never fail. however seting variables inside the game instance on tick/ever frame is a bad idea, You should only send information when you really need to and its unlikely that the game instance needs to know this amount of data.

    • @gordon7936
      @gordon7936 Рік тому

      @@GDXR Thank you for your answer. For what I know I still have to keep the struct as a variable in some place. I hoped there's some way to have a global variable, like the material parameter collection, that I can easily access anywhere, but I see that's not possible with pure blueprints.

  • @jaybenny5477
    @jaybenny5477 2 роки тому +1

    hi im a student and preparing for a competition about vr development(make a scene that has interacitve functions,character,animation,level designs ) ,sametime, has a UI platform . The subject is about learning English,also my idea is to grab the letters from one box to combine a word on a screen as a beginner idk how long it would cost (i must finish it in 1 mouth) and is there any courses help me study this ?

  • @OpenMawProductions
    @OpenMawProductions Рік тому

    So, if you're making something like a first person shooter, you would have to save:
    Player health, player armor, player inventory, current weapons that have been picked up, current ammo for each weapon, current ammo in each weapons magazine (if they have that)... All in the game instance? Doesn't this start to get a little... Insane? Or am I just not understanding this correctly.

    • @GDXR
      @GDXR  Рік тому

      No, your correct. You add all the variables you plan to save inside your game instance is it's a persistant actor (Not destroyed between levels). Then inside the game instance you have a create save game object. Which you then pass the variables through to. But yes, with blueprints, this get's, Insane.

  • @thereanimator6072
    @thereanimator6072 2 роки тому

    Great video! My player has 18 variables I need to store between levels. Do I need to create a custom event for each variable or can I use a sequence perhaps? Thank you

    • @GDXR
      @GDXR  2 роки тому

      If you have multiple variables, all 18 should be stored in your game instance. From there you can create a system so the actor on begin play casts to the game instance (Same code as the thumbnail) and then sets the variable you need. Your actor should only grab the variable they need.

  • @chas3818
    @chas3818 2 роки тому

    Thanks for the vid, got it working with my health which is great.
    How does this work with Actor Variables? For context I have a sword blueprint being saved as an actor variable thats being attached to my player, and I followed the same sort of set up as the health where it calls the event dispatcher to update when you pick up a sword but no luck, returns is not valid so the variable isnt loading on begin play

  • @rubenminasyan9801
    @rubenminasyan9801 2 роки тому

    thanks so much

  • @ogradus
    @ogradus 2 роки тому

    I could be wrong, but isn't the PlayerController also persistent also?

    • @GDXR
      @GDXR  2 роки тому +1

      Player controllers are persistant between levels but in most cases are only used when your dealing with multiple players/characters/possesion.
      The game instance is normaly used by devs doing single player content.

    • @ogradus
      @ogradus 2 роки тому

      @@GDXR that is a relief! There are so many tools and it's hard to know what is good for certain things. It makes more sense to use a game instance, but I suppose I can make this work. I'm making an RTS and I've been wondering if I can save all the stats/locations of selected units by using object references, which I've stored in the player controller. I'll let you know how it goes. Thank you for your response!

  • @lz4090
    @lz4090 2 роки тому

    Very helpful. Could you also do a game mode tutorial? Thank you

  • @ROSALIEIK
    @ROSALIEIK Рік тому

    dont get it

    • @GDXR
      @GDXR  Рік тому

      If you have a variable that needs to be stored between level loading. Then you need a game instance to store the values.
      Let's say your player has been damaged and there health is currently 32%. If you load a new level there health value will reset to default 100%.
      So storing the health inside the game instance means, when you load the next level. There health will stay at 32% and won't be reset.