Spawning with Loop Issues (it makes me feel insane)

I feel insane. I am definitely new to this but I feel like I am missing some critical logic of ue4 visual scripting.

I want to spawn X number of NPCs and have each be named “loop index number”. So they all get a name 1, 2, 3 – etc etc

The NPCs spawn just fine but they only take on the name of the index from the first loop iteration - all of them are named 0. However, the Print String at the end reads correctly (0,1,2,3,4).

Clearly I am doing something wrong – making a wrong assumption. Please advise so I can feel sane again.

How do you know that?

So it is working fine, no?

The script above looks fine, I’d blame it on the method used when you try to read the index.

Cast to. Set a name variable inside instead.
its probably an initialization issue. The cast plus set avoids it…

I apply the variable Name to a HUD widget to see in game – they are all “0”. Though the issue may be how I am doing the widget.

So run a cast to after the spawn node – then set the variable from there?

Can you show how you do that? I’m pretty sure that if you just Print it inside the actor, it will show ok. It’s a matter of displaying it, the value itself is correct.

I have been trying other things (learning in a awkward way) so this isn’t exactly like it was the other day – but pretty close. The npc blueprint has a widget child to the mesh. The widget has a text box bound to “text1”. This is “text1”:

The more I think on it – the more I am convinced how I am getting it to display is the issue.

Well, this will never work. You’re getting the same value for all your spawned NPC Guys - you know that already but here’s why. Have a look at the tooltip of that GetActorOfClass node, it finds the first actor in the world. So every NPC displays the first one’s value.
Your NPCs create their own widgets, surely. Can you show how that happens in your case - is it on Begin Play or perhaps you’ve set up a widget component?

oof – ok, that part makes sense (finding the first actor).

I have the widget as a component on the blueprint. So I started from scratch to just get a name on a character in the world. Its not going great haha.

ok ok ok – I think i have some forward movement.

Starting from scratch I did:

  • A simple NPC blueprint

    • It has a “Name” variable

      • public and exposed

  • A basic widget blueprint that has a text block

    • It has a variable that references the NPC object

      • public and exposed

    • The text block via the NPC variable, is set to the Name variable on the NPC

  • On the NPC blueprint

    • Event Begin Play does this

Now the character in the game world shows the proper default value of the Name variable.

If there is a better way to get a nameplate to work – I am open to it. At first blush, this feels awkward.


So my original loop was working completely correctly (as best to my knowledge/skill). It was how I was getting the UI widget to show the result that was way wrong. I super appreciate Everynone for pointing out mistakes and letting me talk it out loud.

I am still shakey on exactly all the back and forth referencing but I will chalk that up to still being new to UE4. Any advice on improvements/more efficient ways to accomplish this is welcomed!

You’ve created a hard reference to the NPC in the widget. The widget is pulling data from the referenced object every frame (or whenever you access it, I’m assuming here it’s a bound function as demonstrated here. And it will all work fine.

If you wanted to avoid creating a reference and simply assign value to the text block once, you could opt for something along the lines of:

This way you push the data into the widget’s element directly, avoiding a reference; you wouldn’t even need a Base Name variable in the NPC. (although it might be useful to have it there for other reasons, of course!)

There are many ways to accomplish what you needed here. Choosing the most suitable method will depend on the scope, on how complex the system will eventually become and what you expect it to do.

Hmm, unfortunately your picture for reference isn’t loading :frowning:

Loads fine on my end, the forum’s performance has been bit wonky over the last week. Try refreshing it or check again later.

not loading for me either.

Works now!

This makes much more sense to me – I appreciate the extra guidance!