Custom sniper scope widget - issue removing from parent

So I created this custom sniper scope for my game. I was actually amazed with myself on how easily I put this together. Not sure if this is how it’s usually done but for my purposes it works great with one minor glitch.

I have bound this to an input action on mouse right click (press/release). On press, the widget is created and displayed with a ref to itself and the camera’s FOV is changed to 30 to achieve the zoom effect.
On release, it removes the widget from parent and returns the camera’s FOV to 90.

Everything works perfectly… well… almost. On very sporadic occasions, I get the following error message when closing the PIE:

Blueprint Runtime Error: “Accessed None trying to read property SniperScope”. Node: Remove from Parent Graph: EventGraph Function: Execute Ubergraph BP Pawn Blueprint: BP_Pawn

Below is a screenshot of the BP.

It seems that occasionally, the reference to “SniperScope” gets broken.
It is so rare, that I have tried to replicate it on purpose… and could never do it. But it happened twice within a week. And I work on this game about 6-8 hrs a day with constant tweaking and testing.
Any idea what could be wrong? Any tips or advice on how to fix this?

I’m wondering if maybe you manage to close the scope before the widget has been created. You could always just put an ‘is valid’ check before the remove from parent.

Nice. Haven’t thought of that. Would that be something like this???

I guess it works for now… I’ll have to keep an eye on it… somehow it did close before it was created. As I said above… almost impossible to replicate it. I spam click it and it works for days on end… but then today it happened again in the span of 1 week. Wondering if it could be the 0.3 sec blending delay that’s causing it???

The blend node is skipped over ( ie, takes no time to run ), so it can’t be that.

I do wonder why you’re blending without changing camera. I don’t think it has any effect?

  • is there anything else in the project that interacts with the scope widget - is it accessed elsewhere, beyond this graph?

  • does this pawn ever die - gets destroyed? That’d be one way to replicate it:

    • press key → create widget
    • pawn gets destroyed
    • an orphaned widget now lives in the viewport
    • spawn a new pawn (we keep holding the key, it was never released)
    • release key in a new pawn → the variable is null :eyes:

On the other hand, you do check validity in the second screenshot… :expressionless: It’s a bit puzzling, indeed.

  • do we load levels?
  • do we ever call Remove All Widgets?

Grasping at straws. :innocent:


An alternative here is to create the widget on Begin Play, once. And then the input shows / hides it instead rather than constructs it from scratch.


If you do stick with the create / remove, consider clearing that reference after removing the scope from parent:

If you do not, that stale widget you’re not going to use anyway (since you create brand new ones every time a key goes down) will reside in the memory. The pawn referencing it prevents Garbage Collection from handling it. If you keep spamming the key, you’ll end up with many widgets tucked away until GC eventually scoops them all.

This is somewhat irrelevant here but good practice for when things get so complex that debugging becomes a living nightmare. :smiley:

1 Like

@Everynone

Nothing else in the project interacts with the scope widget.
Pawn never gets destroyed. I mean unless I press ESC and end the PIE session. (That is how the error shows up. But it is so random that it drives me mad)
The check “is Valid”, I just added now after @ClockworkOcean mentioned it. So that might take care of the issue, but I’ll have to keep trying to replicate it.

Not loading any levels yet, this is just per level testing.
Not using “Remove all Widgets” anywhere.

An alternative here is to create the widget on Begin Play, once. And then the input shows / hides it instead rather than constructs it from scratch.

I like that approach. I think it makes more sense, instead of creating/destroying it every time on right click. Lemme see if I can figure that out…

@ClockworkOcean

The blend node is skipped over ( ie, takes no time to run ), so it can’t be that.

What do you mean by skip over?? It actually adds a smooth transition on right click for the widget to be displayed and removed. It’s actually pretty cool looking.

I do wonder why you’re blending without changing camera. I don’t think it has any effect?

It was the only way I could think of doing it :slight_smile:
Didn’t even think to add a 2nd camera.
It actually does have a nice visual effect. It’s not instant, it’s sort of like a smooth delayed zoom transition with the scope showing up.

@Everynone

Is this how I should implement your suggestion???

Yup. Creating and laying out a complex widget can be expensive. Showing / hiding it is free by comparison. Pretty much irrelevant if the widget is simple and you do it on a keypress. But at least you’re only working with a single instance rather than juggle a bunch of them pending kill in the background.


Apart from that, your script is more than fine, should work and be reliable. It’s not something I’ve seen before and I’ve seen UMG misbehave like there’s no tomorrow!

Makes sense. I get it now… Still a complete noob trying to figure things out. Appreciate your input good sir. :+1:

1 Like

And you’re not swapping possession between pawns either, right? As in - there is a team of snipers ready and you switch between them?

I mean unless I press ESC and end the PIE session. (That is how the error shows up. But it is so random that it drives me mad)

I understand it’s rare. Perhaps keep an eye on the Output Log - it’s possible the error pops up earlier than session termination, and you’re getting the visual notification late.

No, nothing like that… Single pawn, never gets destroyed unless game ends.

1 Like