x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

UWidgetComponent::GetUserWidgetObject() returns 0

I have Actor with UActorComponent and UWidgetComponent.

In BeginPlay() within my ActorComponent I want to get UUserWidget which I setted in UWidgetComponent:

     AActor* owner = GetOwner();
     if (owner)
     {
         UWidgetComponent* w = Cast<UWidgetComponent>(owner->GetComponentByClass(UWidgetComponent::StaticClass()));
         if (w)
             screen_w = Cast<UMyScreenWidget>(w->GetUserWidgetObject());
     }
 
     if (!screen_w )
         UE_LOG(LogTemp, Error, TEXT("cannot get screen_widget"))

w is non zero and w-GetUserWidgetObject() is 0. It works fine in 4.19. Widget is setted, I can see him in editor.

Product Version: UE 4.21
Tags:
more ▼

asked Jan 14 '19 at 07:15 PM in C++ Programming

avatar image

alebaster
7 2 4

avatar image the_batch Jan 14 '19 at 07:51 PM

There's a typo, right? Your actual code reads screen_w = Cast<UMyScreenWidget>(w->GetUserWidgetObject()) with the -> being typed correctly?

The way you have it pasted, it looks like you're subtracting instead of de-referencing. Though, I'm not sure that would even compile, so I doubt that's the issue.

avatar image alebaster Jan 14 '19 at 07:55 PM

of course its typo, thank you)

avatar image the_batch Jan 14 '19 at 08:09 PM

Nothing obvious is wrong with the snippet you've shared. So, where is this being called from? Maybe the problem is that GetUserWidgetObject() is being called before the widget is fully initialized.

Is it being called during BeginPlay() by chance?

avatar image alebaster Jan 15 '19 at 04:47 AM

Yes, this is in BeginPlay(). I checked sequence and it turned out that first called this BeginPlay() then Initialize() and NativeConstruct() of my widget. That is the problem. How can I fix this? I have two components and one depends on another and must wait for him. Is there any build in sync methods? Or I just use Tick() function until widget is ready?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I haven't tested this, but why not give UWidgetComponent::InitWidget() a try?

I looked at the engine code for that function, and it basically just calls CreateWidget(...) if the UWidgetComponent's user widget object is still null. This seems to be exactly what you need.

So, try doing...

 UWidgetComponent* w = Cast<UWidgetComponent>(owner->GetComponentByClass(UWidgetComponent::StaticClass()));
 if (w)
 {
     w->InitWidget();
     screen_w = Cast<UMyScreenWidget>(w->GetUserWidgetObject());
 }
 

...and see if that works?

more ▼

answered Jan 15 '19 at 06:09 PM

avatar image

the_batch
1.2k 4 5 8

avatar image alebaster Jan 15 '19 at 07:05 PM

Thats worked! Thank you!

Can you promote your comment into answer, so I can set it accepted?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question