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"

Wait for widget creation for another class?

I am creating a widget in c++ within the BeginPlay function. Another script wants to access this widget. It wants to access within a BeginPlay function. So its looks like this:

HudClass

 void ARTSMainHud::BeginPlay()
 {
     Super::BeginPlay();
 
     if (hudWidgetClass)
     {
         UUserWidget *hudMainWidget = CreateWidget<UUserWidget>(this->GetOwningPlayerController(), this->hudWidgetClass);
         hudMainWidget->AddToViewport();
     }
 }

CameraPawn

 void ARTSCameraPawn::BeginPlay()
 {
     Super::BeginPlay();
     
     // Bind to the mouse move events
     ARTSMainHud *mainHud = Cast<ARTSMainHud>(GetWorld()->GetFirstPlayerController()->GetHUD());
 
     if (mainHud)
     {
         URTSMainWidget *mainWidget = Cast<URTSMainWidget>(mainHud->hudMainWidget);
     if (mainWidget)
     {
         GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, "Called");
         if (mainWidget)
         {
             mainWidget->OnMouseMoveRight.AddDynamic(this, &ARTSCameraPawn::mouseScrollRight);
         }
     }
     }
 }

So the cast to the mainHud works but the cast to URTSMainWidget doesn't work's. And I think it's because the widget wasn't created yet. But how I can wait for it to be created?

I also want to cast as early is possible because as you can see I want to add a function to the delegate within the hudMainWidget.

Product Version: UE 4.15
Tags:
more ▼

asked Mar 04 '17 at 12:06 PM in C++ Programming

avatar image

LifeArtist
47 5 11 18

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

I think you are right, it doesn't work because both may happen simulatenously or in whatever order the engine decides. You basically have to find a way to tie the one thing to the other.

One way to do this is to have one thing spawn/create the other. I don't know your game, so you have to decide on your own if that makes sense in your case, but I think there is nothing speaking against having the pawn create the widget (since it manipulates the pawn it should sort of belong to it in my opinion).

Another solution would be to move the code that is currently in the CameraPawn into the Hud Class. To still have a reference to your pawn you could use GetWorld()->GetFirstPlayerController()->GetPawn(); and then cast it to your pawn class. (Only works if your pawn is possesed by the player).

The very last resort would be to move the CameraPawn code into its tick function, and check if the widget is created yet every frame. This would be a quick and abominably dirty solution :D

I would prefer the first solution, since it seems to me that the ui should belong to the pawn anyway.

more ▼

answered Mar 04 '17 at 03:06 PM

avatar image

pulp_user
1.2k 46 29 58

avatar image LifeArtist Mar 04 '17 at 03:58 PM

Oh yeah the first solution sounds great. I though I have to create it within my DefaultHud class because I am assigning it as default hud. Anyways it worked like a charm!

(comments are locked)
10|2000 characters needed characters left
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