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"

Removefromparent() does not work

Hello, I'm trying to display and toggle a widget (like a menu screen when hit esc) using c++. I was able to display the widget but wasn't able to toggle it on and off. The code is below GameState.cpp

 void AMyGameStateBase::SetVisible(bool v)
 {
     Visible = v;
 }
 
 bool AMyGameStateBase::GetVisible()
 {
     return Visible;
 }

NOTE: Visible is defined as bool in header file and initialized to false

MyPlayerController.cpp

 void AMyPlayerController::Inventory() {
     AMyGameStateBase* GameState = Cast<AMyGameStateBase>(GetWorld()->GetGameState());
 
     GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("%i"), GameState->GetVisible()));
     
 
     if (Widgets && GameState) {
         InventoryWidget = CreateWidget<UUserWidget>(this, Widgets);
         
         if (InventoryWidget) {
             if (GameState->GetVisible() == true) {
                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue,TEXT("Hello!!!!"));
                 InventoryWidget->RemoveFromParent();
                 GameState->SetVisible(false);
             }
             else {
                 InventoryWidget->AddToViewport();
                 GameState->SetVisible(true);
             }
 
         }
     }
 }

PlayerCharacter.cpp

 void Apractice_cppCharacter::Inventory() {
     AMyPlayerController* Controller = Cast<AMyPlayerController>(GetWorld()->GetFirstPlayerController());
     if (Controller) {
         Controller->Inventory();
     }
 }

and inventory() is called by using:

     PlayerInputComponent->BindAction("Inventory", IE_Pressed, this, &Apractice_cppCharacter::Inventory);

Any help is appreciated, thanks in advance!

Product Version: UE 4.15
Tags:
more ▼

asked May 05 '17 at 05:59 PM in C++ Programming

avatar image

Hympert
28 2 3 5

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

1 answer: sort voted first

You have no reference to the widget you have created.

Currently your code will always create a new InventoryWidget and add it to the screen, it will never get removed.

 InventoryWidget = CreateWidget<UUserWidget>(this, Widgets);

This line will always create a widget, your previously created widget no longer has a reference and is now just in memory.

more ▼

answered May 12 '17 at 05:20 PM

avatar image

MajinSephiroth
1.2k 31 8 32

avatar image Hympert May 13 '17 at 11:05 PM

Thank you so much!!! Got it figured out based on your reply!

(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