Setting variables to NPCs and displaying them in a widget help

Hello everyone, I am trying to assign variables to my NPCs at random and have them display when I click on the NPC which pulls up the widget showing their information (will figure out how to save this information at a later point)

So I created this function in the NPC BP to generate the variables at random from a data table.

I then get this function and assign it to the text boxes that should display the information like so

But nothing is show in the text boxes, I do get a accessed none from the UI (my most common error). So how can I get this to work? I tried casting to the UI from the NPC BP, I tried storing the NPC BP as a variable in the UI widget. but neither work

would setting this up as a BPI make more sense?

The first function is incorrect, sequence pins 1&2 never run as you return early from the pin 0. Only family name would be generated.

The dreaded Accessed None comes from the ref in the 2nd pic, I presume? The widget does not know which Farmer you mean. Creating a variable and not telling it which actor you mean will not work. What if there are 3 farmers?

There’s a more straightforward way of doing it. Hear me out.


Is the widget a widget component? If so, the whole thing could be done inside the the Farmer BP and boiled down to:

Run it on Begin Play. It’s the farmer’s widget, so they tell the widget what to display. Having widgets pull data out of somewhere else feels like pulling teeth. Necessary at times but to be avoided.


Another, much grander question, is to whether the farmer needs a generator function in the first place? If there are 1000 NPCs like this, each must know this function. Normally one would have a function like this in a dedicated manager BP that does the generation, or even in a singleton framework class - like the Game Mode, for example.

A farmer would ask another blueprint to generate them some vital data. The farmer’s job is to farm, not to generate names :]

Thank you for replying!
You make some very good points and that makes a lot of sense.

So this widget should be called from inside the Farmers BP?

Correct the current code for generating the code is a function created inside the Farmers BP.

I do have a game mode set up, so I can set that function up in the game mode and ask the farmer to get that function?

1 Like

It’s usually more convenient to tell the widget to do something, rather than have the widget look up things. The widget’s job is to display stuff, not to look for stuff to show. The widget can format the data you give it.

current code for generating the code is a function created inside the Farmers BP.

It’s probably fine. It would work, but in the long run it pays off to have a dedicated actor to do the generation. Normally you supplement your actors by all kinds of managers that do specific things.

I do have a game mode set up, so I can set that function up in the game mode and ask the farmer to get that function?

Yup. You could move the function there (you could probably copy and paste it) and:

Something close to this. Can be neatly wrapped in functions, ofc.

1 Like

At the moment, I am currently calling the widget through my player pawn

Ah, so it’s not that every farmer has its own widget? You have 1 widget only, correct? Efficient.

1 Like

If I was to set the function up as a dedicated actor, what kind of blueprint actor? would this been more efficient than putting it into the game mode BP?

1 Like

Ah correct, just one widget that will open when I click on that farmer haha was try getting this to work now and update you shortly

1 Like

So I tweaked the function, place it inside its own actor BP and tweaked it so it doesnt finish so early, though I release I could be pass it to some thing that is false, is there a node I can pass it to to help it finish?

The above does no look right. Is this what you mean:

Hmmm I think so, tweaking it now.
Also not sure why my UI widget is an array type
image

image

It’s this. But I am not sure I follow. Not sure what we’re looking at above? The farmer? The generator? The game mode?


Could you clarify a couple of things.

  • there’s a bunch of farmers, each has unique generated data
  • the data is generated in a separate actor
  • the player has a widget that is used to show each farmer’s data when they’re clicked on

Is this what we’re after? How close am I?

Thanks, I changed UI ref variable it to a single type but it wont plug into the Get User Widget Object hmmm.

Also adjusted the Generation Function, I think this would work

Pretty much spot on

  • a bunch of farmers spawn, and they each get a generated name from a DataTable
  • The data is generated in a separate actor
  • There is a widget that is show that farmers data when click on
1 Like

Probably, but perhaps it could be simplified to:

Any reason why you must return 2 names and then probably discard one.

Do you need help with implementing this?

I really do haha :sweat_smile:

1 Like

reason being if its a male or female

Then 0 is the name if its male or famle, Then 1 is the family name, I see that is a lot more simplified