Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Calling an element from a string array for text generation in a widget

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 alt textonce and commit to it for the text

Product Version: UE 4.20
npc-naming.jpg (137.8 kB)
more ▼

asked Feb 19 '19 at 06:54 PM in Blueprint Scripting

avatar image

3 1 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.


more ▼

answered Feb 19 '19 at 07:35 PM

avatar image

Nebula Games Inc
5.4k 28 9 15

avatar image kastrm88 Feb 19 '19 at 08:21 PM

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

avatar image Nebula Games Inc Feb 19 '19 at 08:27 PM

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)

avatar image Nebula Games Inc Feb 19 '19 at 08:44 PM

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.

avatar image kastrm88 Feb 19 '19 at 09:57 PM

it was a binding. im not sure how to fill the text box without it being a binding

avatar image Nebula Games Inc Feb 19 '19 at 10:06 PM

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.

alt text alt text alt text alt text

avatar image kastrm88 Feb 20 '19 at 08:16 AM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question