Download

Cannot cast from Widget to MyGameInstance?

I am currently trying to develop a Leveling Up system for our upcoming games, and figured the easiest way to do it would be to cast from the Widget where the player selects what stats they want to upgrade, directly to the MyGameInstance that is storing the stats. However, after linking it all together, the cast tells me ‘Stats’ does not inherit from ‘Button’ (Cast to Stats would always fail)

e4f7fd562dfaabffa4aa42c8ea6d19a4f35a10c4.jpeg

Does anyone know what I can do to fix this error?

Use get game instance as the object

If I understood correctly you are trying to cast a Widget into GameInstance. If this is the case you are way off and should probably invest some more time studying how casting works and how to cast objects in Unreal. Read here and here how casting works.

And to your problem. You first need to GetGameInstance and cast that to your custom GameInstance class (I assume the Stats is your custom GameInstance implementation). When you’ve casted that you can call get/set MaxHP for the casted object

But in general you don’t want to communicate to the game instance inside of widgets but rather from their owner controller / character BP.