Another Cast variables between widget and blueprint Question

Hello,

I’m trying to cast variables between a blueprint and an UI-widget. I’ve already tried all possibilities how to achieve that, but none of it was succesfull. My Casts allways fail.

In detail:

  • Created UI-Widget blueprint
  • Made own GameMode blueprint and there I creat the UI-Widget and push it to viewport
  • Created pawn widget called sphere-blueprint with appropriate logic for my game
  • Try to cast sphere-blueprint variables from UI-Widget, so I can push them into UI text-boxes using bindings on them

I already studied a lot of the documentations and youtube tutorials around the web to solve my problem, but still can’t get it working on my side.

I’ve attached some screenshots of my current blueprints etc.
Btw: I get no compiler errors, it just deosn’t work.

Would appreciate it when someone could point me in the right direction! :sweat_smile:



the problem is that you created a variable of class ‘Sphere Blueprint’ in your widget but didn’t assigned anything to it so basically you are casting a variable with nothing on it.

You must store the spawned sphere object inside the widgets variable before casting it :slight_smile:

2 Likes

And if you stored the correct type, there’d be no need to cast in the first place.


But. Perhaps there is a better way to organise it all rather than rely on the Game Mode. Since we want to read the sphere-bluerprint’s data, why not create the widget in the sphere actor rather than in the Game Mode. You may have your reasons, of course and it can be done.

Try this in the sphere actor:

Now, instead of the widget trying to pull data from a world object, the sphere actor is pushing its own data into its own widget.


Alternatively, if you need to keep the widget and the sphere actor unrelated, and want to use a reference variable:

  • in the widget

The above assumes the sphere-actor is a possessed pawn.

This is what @eldany.uy referred to in regards of populating a reference variable. Think of creating a variable as of reserving memory for it, but we also need to tell it which data to actually store inside.

2 Likes

@Everynone is right…if the objects variable is right there is no need of casting

Thank you all so much for your quick support, really appreciate it!!!

This answers me a lot of my questions regarding the topic “casting”.
I also have to admit, as I’m still a noob to blueprints, I thought I would understand the basic concept of “casting”, but realized… nope :laughing:
At least for now I think I get the point :sweat_smile:

Regarding the right place of variables I already tested “nesting” the widget inside the SphereBlueprint and you are of course right, that worked without casting.
But for learning purposes and some future ideas regarding my little game I also wanted to understand how casting works, as I think I will need it.

Once again big thx to you all for the quick support and regards!