Alternative to Casting to player blueprint

Try to make things more efficient. I know casting is expensive and should be avoided, so looking for any tips on how to avoid it in this scenario. My combat system is, the player’s primary weapon is it’s own blueprint. When you swing the sword, the animation has notifiers that turn on the sword collision, and when it collides with another actor, it applies damage. Here is how I have the notification set up right now (as a notifier). I need to retreive the primary weapon through a cast to my player blue print. This then allows me to run my attacking event within my sword blueprint. Any way to retrieve the primary weapon reference without the cast? Thank you!

Hi drewjohnson71,

I wouldn’t worry about optimizing something like that too much as it’s only happening on a collision - if it were happening 100,000 times per frame then it would probably be something to worry about.

But having said that - I usually create a function called “GetPlayerBP” or some such and have a variable that is a BP_Base_New - the function checks that with “IsValid” - returns it if true (99.9% of the time) otherwise Casts it from GetOwner and sets the variable.

Who told you that? Casting is probably one of the least expensive nodes and not something you should worry about at all.

You may be confused about casting creating a “hard reference” and making the class load other classes that you cast to but this is only something that happens during loading and not related to calling the cast function.

Good to know, thank you!
Just to make sure I understand what ou are saying by hard reference and load other classes… would that be applicable to if you have one base character blueprint and then several child blueprints (I.E., one enemy blueprint, and a dozen child blueprints with different stats or whatever)? So if you case to the parent, it has to sift through all of the data of the children too?

thank you!

It may not matter much, if casting is pretty inexpensive actually anyways, but was just curious.
Would using an interface and running an event from what blueprint to another be more effiecent than casting?
As an example, I have a sword/weapon blueprint and a player blueprint. When the AI attacks, if the player is blocking, they will go into a recoil event in the player blueprint. I can run that event by either casting and then calling the event in the character blueprint… or I can have the event in an interface, add interface to both blueprints and then run it that way, which would avoid the need to cast to the character blueprint.

thank you for the help!

Interfaces help avoiding “hard references” but they are still just functions like casting so you won’t see any difference at run-time.

You can use the “Reference Viewer” on your Blueprint to view the “dependencies” to the right that gets loaded when you spawn the class you are inspecting.