im having difficulties assigning random names for NPCs in my game world. ive tried casting to the NPC and pulling the variable from the name library array but i cant figure out the object referenced in the “cast-to” node. ive tried calling and assigning the name locally from within the widget text block functions but it keeps randomly generating the names and changing the displayed name like a never stopping slot machine. i either need an object i can reference for the cast to or how to get the function to roll the
If this is for an NPC HUD display or something you are going about it in the wrong way. You should have the NPC reference its personal widget and pass data from itself to the widget function. Not code the widget to get the NPC and get info from the NPC. Video #25 goes over references in detail, it might be helpful for you. Specifically watching at 14:25 I talk about referencing a widget. Now this is a viewport widget which if it is on your NPC you would get the reference slightly differently. From your NPC drag out the widget component, the drag out of that and search for “get user widget object” (i think is what it is called), and then you cast that widget object to your widget class that has all this NPC stuff in it. Once you have that, you will be able to easily pass along info from the NPC to the widget it contains and set those variables and texts.
im trying to get a widget that displays above the NPC’s head that displays a random name from a string array. i have a text box that im trying to populate with the random name result. in the video you mentioned creating the widget from within the character blueprint but i have the widget itself already as a component of the character so i can control the widget display position with the move gizmo. i almost dont even need the NPC character except to assign the widget to it so i can display it above the NPC which is already done.
the function i used within the widget text box is calculating repeatedly instead of one time. i only tried pulling the name variable from the NPC because i figured to stop it from calculating repeatedly i could just calculate the name when the NPC spawns and have the widget pull the result when it calculates the contents of the text box which would then be constant because it would be calculated on “Event Begin Play”.
if i can do this entirely within the widget, id rather go that route
That’s what I meant by “get user widget object” and create the reference from there. I am not saying move the widget function to the NPC, I’m saying have the NPC tell you what widget it owns by getting its widget component and then telling that widget specifically what it should display. The NPC holds its name in a variable the widget just has a function to display a string text. The NPC should tell the widget my name is X change your text to X. And this can be done as I mentioned above with the user widget object reference route. (Not in the video)
I just realized something…is that a text binding? Because of so it will constantly fire and since you have that random name thing going the name will just change on tick and bug out. Make it a function and call it once should solve your issue.
A binding is similar to running something off tick. It continuously performs the function in the binding and outputs the data accordingly updating whatever it is bound to. What I am saying is, instead of doing this as a binding, manually create a function within your widget and call it on event begin play. That way you can control the number of times it runs. It will run once on begin play and then never again unless you call it a second time. I have this setup in my own game and it works perfectly. See screen shots below.
yep this solved it. i found the “Set Text(Text)” node and was able to tie the function to it from the construction event so it would only call the function once. i was able to figure out the rest from there.