How do i get a target reference to cast to?

I need to cast or utilize an interface to modify a variable in another blueprint. However, all the tutorials I have found always use overlap, therefore they can use the other actor to set a reference variable or connect directly to the target pin. The two blueprints do not overlap. How can I get a good reference variable to utilize with cast or an interface blueprint? Thank you in advance for your help.

Well, it depends on your setup obviously, you weren’t very specific on how (or if) these objects interact. If you can’t use Line of Sight/LineTrace, you can get the reference in a few different ways. Easiest way is to get all actors of class (especially if this is just one in the game) and get 0 from the array and there’s your reference.

I already said LineTrace, so the other option is to get the reference of the BP via LevelBP and send that value wherever you need. Probably somewhere easy to grab, like GameMode or Instance. Then you can just get that value easily from GetGameMode->CastToGameMode->GetReference.

Off the top of my head, those are the ways. BPIs also a good way. Obviously if you spawn actors, you can get the reference easily also.

Without more information on the objects (as in, how the other actor is created and what actor wants to interact with it and how it does, or you want it to), there’s not much more I can think of.

I have attached the two blue prints. the game controller blueprint spawns enemies and destroys projectiles, as they go off screen. the enemy blue print detects when it has been hit reduces health of the enemy, and determines if the enemy actor should be destroyed. The game controller blueprint has a variable enemycount. when a spawned enemy actor is destroyed, I would like to subtract one from the game controller blueprint variable, enemycount. The game controller blueprint is not a physical object it just contains logic (I possible should have put this logic in the level blueprint). I need to have a reference to plug into the target pin, without the two blueprints physically interacting.

Moving the variable to the game mode worked well. I can modify it via casting or via interface.

I almost forgot. thank you for your help

TBH, your setup is messed up. Enemy Count/Score should go into Game Mode. Then you can easily just get the reference anywhere/anytime.

When enemy is spawned (in enemy BP, on Begin Play, add one to GM Enemy Count).

In enemy BP, On destroyed → Add one to enemy destroyed or subtract one from enemy count, whatever you want.

First of all Call Destroy at the very end otherwise you might run into Problems. For that Interface message input the Actor from your Overlap. Everything your Enemy overlaps it will execute that Function if the thing that he overlapped implements that Interface. Be Smart and make a Dispatcher here a easy example setup to get notified:

Enemy: https://i.imgur.com/q2QDp41.png

Spawner: https://i.imgur.com/LcF27e4.png

There is a good Training video around for BP Communication I recommand you to watch:

It will get you trough the common basics. And as Victor mentioned you can refference it in a easy to Access Object like GameMode, GameState, GameInstance, PlayerController, etc. aslong it makes sense to do so. Don´t make it a Habit and learn how to Refference your Stuff. Many Beginners make the mistake Piping every Communication between Blueprint trough such a class and End up in a unorganized Mess where simpler forms of Communication are possible and are well Organized and understandable by anyone =)

I See a lot of other Beginner mistakes you made there but don´t worry to much about them right now. Have Fun with the Engine. Do some Fun Stuff and get comfortable before you attempt to really make a Game.

Good Luck and have Fun =)

No worries, good luck!

I think you have to set my answer again to answer, so this can be resolved. Since you commented afterwards. (so, don’t comment after you mark my answer resolved :slight_smile: )