Make an End Widget appear and a Spotlight disappear

Hello, I’m working on a little CryptRaider puzzle that comes from the Udemy course “Unreal Engine 5 C++ Developer: Learn C++ & Make Video Games”. I have a TriggerBox made in C++ that works with a Mover, also made in C++. When an object that has the same Tag as the TriggerBox enters the interior and is dropped, the Mover is activated to move the door. My first question is how do I make the Spotlight disappear when the item is dropped and at an acceptable Tag? I don’t want when the object enters the TriggerBox it causes the light to disappear. I would like it to be the same mechanic that activates the Mover. For my second, I would like to take the TriggerBox and put it on a Stand at the end of the puzzle. When the player brings the object with the right Tag and drops the object, like for the Mover, it activates my End Widget? I searched a lot and couldn’t find anything that worked, I tried a lot of things in C++ or blueprints and nothing worked. If this is not clear enough, tell me because I use Google Translate as I speak French. Thank you for your help!







So, to summarize, you want to turn off the spotlight when Mover->ShouldSetMove is called.
If so, since the spotlight and mover are attached to the same actor, you can add this function the mover and call it inside of ShouldSetMove:

#include "Components/SpotLightComponent.h"

void UMover::TurnOffAttachedSpotlight()
{
    // Get the spotlight component attached to this actor
    USpotLightComponent* AttachedSpotlight = FindComponentByClass<USpotLightComponent>();

    // If a spotlight component is found, turn it off
    if (AttachedSpotlight)
    {
        AttachedSpotlight->SetVisibility(false);
        AttachedSpotlight->SetActive(false);
    }
}

As for your second question, I don’t see where your widget is defined, so I need to see that if you want me to write some code for you. Once you have a reference to your widget, you can either add it to the viewport if it hasn’t been added yet, or change its visibility if it has been.

Also, as a point of note, you provided a lot of code, which is nice, but screenshots are awful because I can’t cut and paste your code. Next time, paste the code directly into the post and surround it with code tags to make it easier for everyone to manipulate.

Thank you for the answer! I have to add this part of code in the Mover.cpp?

The Widget is created but actually it is not in the Blueprint of the Stand because it appeared as soon as I did Play, that’s why I removed it. I would like when the player drops the item and they have an acceptable Tag, the End Widget appears. If it’s an object with an unacceptable Tag, that doesn’t matter. The Mover on the Stand is useless, because it shouldn’t move so I tried to recreate my TriggerBox in code and change the Mover to something that would make the End Widget appear. I also tried with tutorials to make a UserWidget in C++ and the Parent link to my EndWidget Blueprint but nothing worked.

Perfect! I’m really sorry, this is the first time I’ve asked a question so I didn’t want to put too much code to get lost. I’m new to C++ but I’ll take note for next time and I can send you the Drive link of the project if you want to make it easier!

You could add the function to anything you want, but it would have to be modified based on where it’s being called from. You could add it to SecretWall or the Mover. You could even have a separate component if you wanted. You could also add it to something like the game instance or game mode, but you would have to locate the actor, and then locate the components you want to modify, but the principle is the same. You could also have a utility class and call it from that. It really depends on how you want to structure your code. Personally, I would actually add it to SecretWall since everything is already attached to that.

The code I wrote was made to be put on the Mover though, but I noticed a small mistake.

USpotLightComponent* AttachedSpotlight = FindComponentByClass<USpotLightComponent>();

The above would work if this function was in the actor class, but it won’t work from a component. You would have to do this to call it from a component:

USpotLightComponent* AttachedSpotlight = GetOwner()->FindComponentByClass<USpotLightComponent>();

This is because FindComponentByClass is a function that is part of an actor, not a component.

Go ahead and send me a sample project if you want. Showing and hiding a widget is fairly simple once you have a reference to it.

I sent you the link in a message, just in case. The other Widgets worked because they are not affected by a TriggerBox, the PauseMenu is activated with the P key.

I will try that right away!