# How to create a widget when aiming at an actor (line trace) and then close it when not aiming at it ?

**URL:** <https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, question\
**Created:** [February 7, 2024, 10:13pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158 "2024-02-07T22:13:56Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![catz111](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/catz111/32/618520_2.png) [@catz111](https://forums.unrealengine.com/u/catz111)\
**Post date:** [February 7, 2024, 10:13pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/1 "2024-02-07T22:13:56Z")

</div>

I awready have the code with the line trace. And it seems to work fine when aiming at an actor and then not aiming at it. The problem that i have is that i can create a widget when aiming at the actor but i don’t know how to close it properly after stopping aiming at it. Like i say my line trace works both when aiming and not aiming so i need to only make the widget to close after im not aiming.

It looks like this atm…

 ![bandicam 2024-02-07 23-07-47-449](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/5/a/b5a01df69f18aac9e9e80fd50e6539b187e02772.png)

And here is an example video how this looks like in game so i can confirm the branch if i aim or not aim at the actor acctually works (i have some effect running in a loop when aiming and it stops when not aiming at the actor to visualise that this is working). Widget gets awso created when i aim at the actor (is just simple text “Sell Traps”). The only problem It’s just the logic how to acctually close this widget after when i’m no more aiming at it and my code has to be wrong there.

---

<div class="post-metadata">

**Author:** ![Midisport\_8x8s](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Midisport\_8x8s](https://forums.unrealengine.com/u/Midisport_8x8s)\
**Post date:** [February 8, 2024, 12:29am UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/2 "2024-02-08T00:29:14Z")

</div>

Your problem is you are trying to get data from a different path. The widget you are trying to get is from a divergent universe that never happened if you think about it. You need to save the widget into a variable when you create it, and then remove and clear it when you stop looking at the thing.

---

<div class="post-metadata">

**Author:** ![catz111](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/catz111/32/618520_2.png) [@catz111](https://forums.unrealengine.com/u/catz111)\
**Post date:** [February 8, 2024, 2:37am UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/3 "2024-02-08T02:37:00Z")

</div>

Hey thx for the reply,

So i made a variable to the widget but im still failing to remove it…  
Can you explain more specific how to do it?

Here is what i try

 ![bandicam 2024-02-08 03-34-58-799](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/7/d/87d67d992c09a88a822fdbc16103353bcaf3e4e2.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 8, 2024, 7:31am UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/4 "2024-02-08T07:31:57Z")

</div>

Example:

> [@Third person crosshair object highlight troubles](https://forums.unrealengine.com/t/third-person-crosshair-object-highlight-troubles/252041/13):
>
> The communication in the example revolves around Inheritance + Interface - a potent combo, imho. the base class implements the interface (which children inherit) the widget uses the same interface - calls children receive can be propagated: Actor → Widget Component → Widget without casting or finding user widgets (this can be done in many ways, though) the player has a collision sphere and sends an interface call to overlapped actors as we approach: [image] [image] Above, handl…

A more recent one:

> [@Moving an object by pointing another object at it](https://forums.unrealengine.com/t/moving-an-object-by-pointing-another-object-at-it/1657869/2):
>
> See if this point_down helps with this point_up_2 particular bit. a standard forward camera trace: You’d use an interface or inheritance if there are more types to handle, ofc. not sure how you want stuff to move, here it’s via a timeline: which results in: the branches in the script prevent it from spamming since we’re tracing on Tick: Hopefully you can adapt it to your needs. It’s a setup similar to this one:

* * *

Essentially, store a ref to what you hit last, and base the logic on whether it’s valid.

Oh, the Fairy. I remember her.

---

<div class="post-metadata">

**Author:** ![Midisport\_8x8s](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Midisport\_8x8s](https://forums.unrealengine.com/u/Midisport_8x8s)\
**Post date:** [February 8, 2024, 1:54pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/5 "2024-02-08T13:54:18Z")

</div>

Why are you creating a new widget and overwriting your variable? Get rid of these nodes and use a Get instead of Set and it should work.

 ![code](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/2/b/e2bce8a7c2c1fdf7086fbab6aae3fde35f8d9cab.png)

---

<div class="post-metadata">

**Author:** ![catz111](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/catz111/32/618520_2.png) [@catz111](https://forums.unrealengine.com/u/catz111)\
**Post date:** [February 8, 2024, 5:09pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/6 "2024-02-08T17:09:23Z")

</div>

Idk i just tested with and without but it didn’t work. True no need to create a new one there.

I changed it to “get” my widget reference as you pointed but it is still not working and disconected the rest. Still same result widget gets created but it doesn’t close after…

 ![bandicam 2024-02-08 18-10-23-711](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/9/b/49b269dd4e3c76b2b3ae41ea540b607c468e4838.png)

I can awso try reverse the code and widget gets created when not aiming at the actor and when aiming it will awso not close. I so at least know that this looking at the actor or not looking at it isnt the cause because that is working. There must be some way to close this…

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [February 8, 2024, 5:49pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/7 "2024-02-08T17:49:46Z")

</div>

I’d start with looking closer at the logic before tackling anything else:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/1/4/2143cac822631296571eeef11d5c5df434aa3895.png)

You run a loop but return no matter what. Without checking references you don’t even know how many widgets get created. You may have 100s of orphaned widgets sitting in the viewport and leaking memory. Widgets you cannot even remove.

Let’s say you run this script 60 times per second. You will have 59 overlapping widgets on top of one another in the 1st second of the trace because only the one added last gets removed. Admittedly, hard to tell what else is going on.

---

<div class="post-metadata">

**Author:** ![catz111](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/catz111/32/618520_2.png) [@catz111](https://forums.unrealengine.com/u/catz111)\
**Post date:** [February 8, 2024, 5:52pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/8 "2024-02-08T17:52:29Z")

</div>

I just found out the same thing. I think this widget gets created in a loop every 0.2 sec because i have on tick node in the event graph that checks for this

 ![bandicam 2024-02-08 18-51-46-214](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/f/d/8fd39b549f229f41e3ce75691fc513b61048354f.png)

Edit : Ok i changed the event tick to a node where i press a key and it works. Aiming at the actor and the widgets pop up on the screen - then looking away and pressing the key again removes the widget. Now how can i make this to work without the need of a key ? It should constantly run like on tick event. Is there anything i can do to make it work ? Was trying “Do once” node in the function but it bypasses it…

Something like this

 ![bandicam 2024-02-08 19-01-12-464](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/a/0/9a002ded84c990bfa18f24a0adde6c1fb97c21a2.png)

But it bypasses it as if it wasnt there. This emitter run non stop in loop so it must run the code after in a loop too creating many widgets. I tought when i aim at the actor it should fire Once and then when not aiming at the actor should reset it + close the widget. So next time by looking at it it can create a new wiget again and repeat. It seems it still fire non stop when aiming with do once node… Is this somehow flawed tactic to achieve what i aimed for ?

---

<div class="post-metadata">

**Author:** ![baobao4435](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/baobao4435/32/950790_2.png) [@baobao4435](https://forums.unrealengine.com/u/baobao4435)\
**Post date:** [February 8, 2024, 6:31pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/9 "2024-02-08T18:31:02Z")

</div>

You have to check if the widget variable is valid first, only when the widget variable is not valid then you can use the create widget node,if it’s valid just use the add to viewport node.

---

<div class="post-metadata">

**Author:** ![catz111](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/catz111/32/618520_2.png) [@catz111](https://forums.unrealengine.com/u/catz111)\
**Post date:** [February 8, 2024, 6:38pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/10 "2024-02-08T18:38:05Z")

</div>

Thx this works 😃

I have it like this right now and it works perfectly now.

 ![bandicam 2024-02-08 19-36-46-476](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/1/9/c198363f86a80c640e8d29e48ab6399da5f8908c.png)

Ty all for the help!

---

<div class="post-metadata">

**Author:** ![baobao4435](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/baobao4435/32/950790_2.png) [@baobao4435](https://forums.unrealengine.com/u/baobao4435)\
**Post date:** [February 8, 2024, 6:40pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/11 "2024-02-08T18:40:56Z")

</div>

Well,they said the same thing.  
maybe they just not said as simple as I do…

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [March 9, 2024, 6:41pm UTC](https://forums.unrealengine.com/t/how-to-create-a-widget-when-aiming-at-an-actor-line-trace-and-then-close-it-when-not-aiming-at-it/1673158/12 "2024-03-09T18:41:20Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
