Cast To SceneComponent

Hey, I’m using the BP_Weapon_Component from the FPS start template, and I’m trying to figure out how to cast to it from a UI binding, but I don’t know which object it takes. I’ve tried every object I can think of. I’m just trying to get the “current ammo” variable that I added in the blueprint.

It’s probably better to do it with an interface

That’s because it’s not a good idea ( from a clean programming point of view ) to do a deep dive from one blueprint to another.

But to answer your question directly, it’s better to put a function in the player that returns the current ammo. That way, in the widget you can say

And let the player deal with the fact that they might not have even picked up the gun yet…

2 Likes

It’s better to just fire an event in the widget when you pick up / remove ammo. Why execute something 60 times every frame if you need it only once per event. You could even have the widget created by the weapon component - seems relevant; and it would require no casting or interface.

3 Likes

How would I go about doing that?

I currently have this, and it works, but the binding function is being called every frame, even though I’m only using the Current Ammo variable when a shot is fired. What should I do different?

For anyone wondering, the way you cast to a Scene Component is to add a variable and search for variable type ‘scene component’ and when you hover over that click ‘object reference’, then use that variable as the object to your cast.

But, blueprint interfaces are definitely better, lot less intensive and no hard referencing.

But you’re not doing any of that here. This cast will always fail. This will not work once hooked up to anything. Unless the gun is actually a scene component which it probably should not and be a functioning actor instead.

I’m using the BP_Weapon_Component from the FPS start template

edit: I guess it could work OK if it was an overriden component (is it?); if it’s not you can’t make it functional which will be quite limiting further down the line. It depends on the scope a lot. Is there only 1 gun ever and we never get rid of it? Knowing this would affect the way things are designed.

The below still applies.


If you need help with this, do explain how the player gets the Pistol_BP.

Assuming:

  • there are many guns
  • the player spawns them / picks them up and or discards them dynamically
  • each guns tracks its own ammo via a widget

Wouldn’t it be easier and more logical to:

  • weapon widget:

image

  • each weapon creates and then updates its widget when fired:

  • the player spawns and fires the weapon:

Note no casts, interfaces, or updating every frame.


This is the most straightforward, crude yet functional way to set it up. To make it work better, you’d use inheritance and have a base weapon class that does all the mundane stuff, and only extend it if you needed a gun that does something fancy. An orbital strike beacon is different enough from a glock to be a child class, for example.


You’d work in an interface when working with multiple unrelated classes and still wanted to use the same input / interaction method. For example, when the player hits E to use while looking at something:

  • it’s a tree - we punch it, get wood, profit
  • it’s an NPC - we strike… a conversation
  • it’s a door - it swings open

All with the same E key. That’s where interfaces shine.

1 Like