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

7 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!

13 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.