I’m going to go ahead and explain this to beginners such as myself.
In order to communicate between blueprints you will need a reference to the blueprint you are attempting to talk to. Before you can do that, you will often have to cast to the other blueprint first which I will attempt to explain.
You can cast to the other blueprint at the beginning of play and store your reference for later or you can cast to the blueprint only at the time you need to, like when a button is pressed on a Widget, a key is pressed, or on an overlap. I prefer to cast to any blueprints I think the current blueprint I am in will need at EventBeginPlay. This way later I can just pull my references instead of casting each time.
I was having lot’s of problems casting, particularly with objects.
There may be more than 2 types, but for what we are talking about there is simple casting like on an overlap event. And then there is casting between blueprints which is what we are after. I will cover both real quick.
Overlaps are good. Any time you can tie something to an overlap event instead of a tick you will be doing yourself a favor since it only happens when the overlap occurs. They are also easy to cast with because the EventOverlap has on object pin on it. Whereas EventTicks and others do not.
So, on an overlap, you simply cast to an on object to see if that is what overlapped. For the object type you connect the other actor pin. My example is an actor blueprint. Whenever it overlaps anything, it checks to see if it is one of the boundaries. First it checks the side, then the top etc. If it did overlap with one of those objects, then destroy actor destroys the actor, not the boundary.
Casting to a blueprint is a little bit different.
Mostly you just need to put a ‘get all something of class’ and plug it into the object type.
If it’s an actor it would be actors of class. in the previous pic I am casting to another Blueprint called Player_Controller whose native parent class is ‘Player Controller’. However it is still found under “all actors of Class”
If It’s a Widget, like your HUD, then it will be found under “All Widgets of Class”, Like this:
Other Pawn and Actor Blueprints will also be found under “Get all Actors of Class” and can be handled the same way.
To further explain the above pictures…
On EventBeginPlay, we drag off, get all actors of class (our target blueprint). Once we get that, we drag off the array pin called "out Actors’ or ‘Found widgets’. Then we type → get(copy). We drag off of that and type “Cast to…” and whatever we just came from pops up. *(note below) Connect the white execute pins. Now, on our newly placed ‘cast’ node, drag off the ‘as player controller’ or ‘as player HUD widget’. Type "promote to variable. This will place the set variable you see called ‘Set PC Ref Ball’ or ‘Set HUD Ref Ball’.
Name it what you want. This is now your reference to that other blueprint. If you want to pull another variable, first you will place this reference. You can do that by dragging it in from the left panel that lists your variables. You want ‘Get’. You only ‘Set’ it at the beginning. (Setting it without properly casting first will also give you errors. The editor might not warn you so it looks ok, but then it won’t work in game.) (**Final Note) Then you will drag off that and type ‘get…’ this will then give you access to whatever the other blueprint has. Or you could type ‘Set…’ and then change the variable that is stored in the other blueprint. like this:
Getting Variables from other Blueprints
** (note) We don’t have to worry about the index of the Array because often there is only 1 instance. I have only 1 player controller named xxx. I have only 1 Widget named xxx. I could have other widgets… but they will have different names. Now If I had an actor that represented say an NPC, and there were 30 instances of the same blueprint in my level… then I would need to worry about the index. It needs to know which one exactly are we talking about. I don’t know as much about that yet so I will not elaborate on how to go about that. But if you are trying to figure out how many there are of something you could do this:
** (Final note)
If anyone could explain that Blue Error I would love it. If you get all actors of class first it will work fine. At least it does for me. If I unplug object type, plug in anything else, or don’t get all actors of class… I cannot get anything to work.
and since it was mentioned briefly, here is how you take over another pawn.
Pawn Possession
That is being done from my Player Controller Blueprint.
The equivalent to ‘EventBeginPlay’ in a widget is ‘EventConstruct’