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.
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”:
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.
[HR][/HR]
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?
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.