I want to display health on my UI widget, but it’s a variable that I declared in the C++ file of a different class. How can I reference this variable in the UI’s blueprints? I’m pretty new to blueprints.
Thanks in advance!
Edit: I couldn’t find anything helpful that told me how to do this anywhere so in case someone is trying to figure this out, here is what I did:
-
Drag a text widget onto the canvas panel.
-
Click the “Content” submenu along the right side of the new text widget.
-
To the right of “Text” and “”, click the “Bind” button, then “Create Binding.”
-
It will pull you into the blueprint of that new binding. From the “Return Node”, drag from “Return Value” out onto the graph to be prompted for a new node.
-
Type in “cast to [the name of the blueprint class derived from the C++ class that the variable is declared in*]”
-
Select the corresponding action.
-
Drag out “As [class name]” to create a new node.
-
Search “Get [variable name]”.
-
Drag out from that node and search “ToText [variable type]”.
-
Link that “ToText” node’s “Return Value” to the “Return Node’s” “Return Value”.
-
Now drag out “Object” from “Cast To […]” and search “Get Player Character”. If you’re variable is from somewhere else, like the PlayerController, then type “Get [that]” instead.
-
Finally, link the first “[Function Name]” node to “Cast To […]”, then link that node to the “Return Node”, compile, and run.
*I think the variable needs to be declared under “public:” and somewhat like this:
UPROPERTY (VisibleAnywhere, BlueprintReadOnly, Category="PlayerVariables /* or whatever category the variable is in */")
// variable initialization:
int32 Health;
Additionally, you might need to uncheck “Context Sensitive” when creating a new node.
I know that this is a very simple thing but it took me a while to figure out and I couldn’t find anything that could tell me.