Announcement

Collapse
No announcement yet.

Instantiating a class blueprint as object variable in GameInstance

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

    Instantiating a class blueprint as object variable in GameInstance

    Hi, I am starting to learn UE4 and Blueprints, but seem to be stuck at a pretty primitive level.

    What I am trying to do is essentially to create a class blueprint to hold some data, which I can persist in GameInstance and serialise in a save game.

    As a simple test I have created a Class Blueprint called 'PlayerData' derived from Object, which has two string variables - 'PlayerName' and 'PlayerClass'.

    I have created a MyGameInstance derived from GameInstance and given it an object variable SomePlayerData of type 'PlayerData', then set the Game Instance type on my test level to 'MyGameInstance'.

    I now want to be able to access my PlayerData from the test Level Blueprint, but can't figure out how to actually instantiate it.

    On Begin Play I get a reference to the game instance, cast it to MyGameInstance and set it as a variable 'TheGameInstance' in the level blueprint. I then try to access TheGameInstance->SomePlayerData so that I can set some dummy values for PlayerName and PlayerClass, but it is not a valid object... presumably because it has never been created. I cannot find any way to instantiate the PlayerData class so that I can store it in TheGameInstance, though.

    Click image for larger version

Name:	PlayerDataNotValid.JPG
Views:	1
Size:	139.7 KB
ID:	1141816

    The result when I run the level is 'NO PLAYER DATA'.

    If I were writing code I would add something like SomePlayerData = new PlayerData() to the MyGameInstance constructor, but I can't see any equivalent in Blueprints.

    I assume that I am conceptualising something wrongly, but going through a multitude of tutorials hasn't really helped me to understand what I am not understanding.

    The basic idea is to use the blueprint 'PlayerData' to encapsulate some common data between the game instance and the save game, so that serialising it is easier. If I set up MyGameInstance with variables of built in types (i.e. string variables 'PlayerName' and 'PlayerClass') I can set them, but then I have to replicate the same variables in MySaveGame and copy each value individually to serialise them, which sounds like a maintenance nightmare.

    If non-actor class blueprints can't be instantiated there wouldn't seem to be much point in having them, so I assume there is some way to do so?

    #2
    Actually there is no built-in way to instantiate non-actor blueprints and i believe someone from Epic advised against doing this (somewhere on the answerhub).
    It is possible with this plugin but there are some downsides to it. You can't create a blueprint based on non-actor blueprints and you're likely to run into some editor crashes.

    Can't you just use your custom GameInstance for what you're trying to achieve?

    Comment


      #3
      I can populate MyGameInstance with lots of strings and ints etc, but then I will have to duplicate everything in a MySaveGame class to persist it, and wire up load/save for each property to copy data between them - I was hoping for a more encapsulated system, where I could define what I mean by 'PlayerData' once and use it in multiple places - e.g. possibly have multiple instances of it for multiple players. Storing everything in MyGameInstance seems rather like putting everything in global variables in code, very inflexible.

      If you can't instantiate it, is there every any point creating a class blueprint derived directly from Object?

      Comment


        #4
        Well the simplest solution seems to be to just put the player data into MySaveGame, store a reference to the MySaveGame in MyGameInstance, and access the player data via that. Certainly leads to the cleanest/simplest blueprints in my slightly more real-world project, anyway.

        It does seem a bit odd to keep a copy of a save game in memory for working data, but since it is supposed to be persistent data I suppose there is a sort of logic to it. It's probably still going to get very messy when the amount of data increases, and certainly if I want more than one player active... but hopefully a better way to do things will occur to me as I get more familiar with the system.

        Comment


          #5
          this is an old thread but it is the first result on google when I searched about this, I could not believe you coudn't simply construct an object an I kept looking, you can just use construct object: https://docs.unrealengine.com/en-US/...***/index.html

          In my case I defined a Team class that derives from object, and I want to have that class for each team on the game:
          Click image for larger version

Name:	Capture.PNG
Views:	381
Size:	42.2 KB
ID:	1775816

          Comment

          Working...
          X