Add Widget Component at Runtime

Hello everyone!

For my game project I want to implement some TCG-Elements. I designed a Widget Blueprint as a “Card” and now want to add multiple instances at runtime to an actor (->into 3D space). When I manually add a Widget Component for the actor, everything works just fine,
but when I add them at runtime with “Add Widget Component”, nothing happens. Do I have to call some extra functions?

Futhermore, the construction script of this blueprint doesn’t get called?!?!

Okay, I got it. But somehow the different cards are stacked on top depending on the distance to the viewer. I have 4 cards sligthly on top of each other, like in HEX or Hearthstone, but when I shift to the left, the lower cards get displayed on top of the physically overlaying cards, resulting in very strange looking images… How can I disable that?

So you solved the construction problem of spawning a specific widget component, but you didn’t think to share how you did it and immediately went on to the next problem.

That’s… Really bad etiquette. I myself have this problem right now and I don’t want to spawn a new actor for every single one because that’s incredibly wasteful and I got excited thinking I’d found my answer and instead I’m met with this.

https://imgs.xkcd.com/comics/wisdom_of_the_ancients.png

9 Likes

For the record, what you’re experiencing is z-fighting. Look into the ■■■■ render settings and change the CustomDepth/Stencil value stuff to overcome these problems.

To create a widget component runtime using Blueprints:

  1. Call “Add Widget Component”
  2. Call “Create Widget” with the class you want to create
  3. Call “Set Widget” with the Widget Component returned by “Add Widget Component” as Target and the created Widget as the Widget.

Like this:


You can then Set Widget Space, Draw Space etc. as needed on the Widget Component.

I had to do this myself today and saw this thread. I hope this helps.

Cheers!

19 Likes

I tried this approach in Construction Script using the Construct Objec from Class.
It works both in the Editor and Stand Alone Game Preview.
However in Play and Play in New Editor Window the widget just disappears…
Maybe Im wrong but Construction Script is called before Event Begin Play.
So why the widget disappears?
Again, it works fine in Stand Alone Game.
Anyone got that too?

I would like to do that way cause I have more control creating dynamic widgets.
AND using create object from class I dont need to reference the player.

Old but still gold, it seems impossible to find any good post on how to create actor bound widgets dynamically.

This is how I did it in ue5.3 in a blueprint actor component without issues mentioned below.
One thing to note is to go for “Add Component by Class” - there is no “Add Widget Component” Node visible to me.

Blueprints are context sensitive and the results are filtered - not all nodes are available in all contexts / places. Were you to add it inside an actor’s main graph (not in a component / widget) you should see it just fine:

A widget class can be set directly in its details panel, there’s no need to manually create and set widgets unless you need the comp to juggle them.


The benefit of Add Component by Class is being able to create an inherited component. But it should work all the same with the native one. But then you cannot see the class defaults (see above) unless you expose them yourself.

1 Like

For the future generations, who like myself, follow YouTube tutorials where you’re instructed to set space to ‘Screen’, here is the complete sequence that successfully returned a widget inside an actor for me.

p.s. Trigger is Begin Play event of an actor

Why do all this, select the Add Component node - all inner working are exposed there. If you want additional exposed data (interact), extend the widget component itself. You can then script functionality directly in the component.

  • widget comp:

image

  • stuff inside:

  • adding the comp:


Components really do not like switching between world / screen mode. If you ever run into issues, that’d be the first thing to investigate.

Good luck!

1 Like

In my case i am creating widget in Actor Component, for each Actor who gets that component to have mentioned widget out of the box

Even more reasons to utilise what I suggested in the previous point. Much cleaner and compartmentalised script. Instead of burdening actors with setting up components and copy / pasting a bunch of nodes, the components can do it all internally.

For the future generations

While what you posted will most likely work fine (for now at least), that’s not something I’d recommend anyone used. You mileage may vary, ofc. But why complicate things and work against the engine?

Looks like i am missing some crucial part, because my goal is exactly the same, to move all the related logic to components, and then depending on the task specific component will solve it for the parent actor.

And looks like i am doing everything the same (stuff inside: and adding the comp: parts), but because first part is missing, i am forced to set up widget programmatically, and i would be happy to not to, but i still can’t understand then how to add widget to actor component through UI ? I checked your response with images, and can’t find this All Classes section anywhere.

For me my Actor Component looks like this and i can’t add any nodes to it from UI


image

Right click in the content browser → Blueprint classes. :point_up: The Common tab has the stuff we’re all used to. But you can extend other classes, too. It works really well for static / skeletal meshes, anything that needs to be modular.

Once you’ve blueprinted the comps, you can then add them to actors from the dropdown:

Components fully support inheritance which really opens up for grander designs while keeping complexity at bay. It will still be a convoluted mess at some point, ofc. Just a tad easier to keep all the bits in your head. :innocent:

1 Like

This is amazing, found it, thank you very much, will try to use :raised_hands:

My main problem with Unreal Engine currently is (was?) lack of strong inheritance (for example i’d like to have interface extending other interface, but it’s my first week with the engine, so i admit that it can be engines problem with me, and not me with the engine :smile:), and such small things are the game changer.

Just to save someone losing their mind a bit like me. You might need to do a 180 on the Z axis for the relative transform. If your widget is “backwards” due to the current rotation of the actor you may not see it appear.

1 Like