Game Instance Cast in blueprint constructor failed

I’m trying to use a variable from MyGameInstance in a blueprint constructor however the cast is failing. I have MyGameInstance set in the Maps & Modes so I’m not to sure as to why it’s not working. Any suggestions or ideas to get this to work?

Trying to use persistent values to change default values on newly created objects.

This should be working as is.

  • Are you sure you have got the correct Game Instance assigned in Maps & Modes? I know you said you did, but hey, who knows.
  • How and when are you spawning this object? Is it manually placed in the level or spawned from class with a node?
  • Do you actually see the Print String spit out Cast Failed message?

are you running this script from a blueprint actor? also when testing are you actually playing the game? the game instance shouldnt exist until the game is launched so if your dragging around in the viewport it shouldnt work as intended as there is no game instance.

ThompsonN13 is right.

  • the Game Instance does not exist outside of PIE - Play In Editor, that’s why it fails and is expected
  • if you place an object in the level manually, its Construction Script will not fire when you PIE; also as expected
  • if you want to give a manually placed object access to the Game Instance, you can do it in that object’s Begin Play event, for example. There’s more than one method to do it.
  • what you’ve demonstrated above should work just fine for any object spawned through a Spawn Actor From Class node

I’ve reset Maps & Modes several times and even checked the DefaultEngine.ini to see if I had that bug where it is not renaming itself in the ini. MyGameInstance appears to be set both places.

The first object I’m placing manually inside the level and plan on spawning others after Runtime.

Yes the Print String displays Cast Failed. When I manually move the object via dragging the “Cast Failed” is spammed.

Thank you for answering that. I’m able to access the data in the game instance when the game runs using Begin Play. Just can’t do it manually in a constructor in the editor as it doesn’t exist.

  • Are you sure you have got the correct Game Instance assigned in Maps & Modes? I know you said you did, but hey, who knows.

That line work for me. Just remember when you migrate folder to your project in my case I was linked the Game Instance to that folder. So, the only thing that I needed to do was go to the Maps & Modes and reassign the Game Instance in that folder again. I was struggling during hours until a get this treat. Thank you so much the fix was very easy.