Communicating Between Pawn, HUD and Actor Blueprints
I can get my HUD widget to talk to my actor blueprint... but I cannot get either to talk to my pawn blueprint. I will detail my steps below. All my variables are set to public. I am not using any templates. In game modes, my default pawn is the pawn blueprint I am trying to access. (not sure if that makes any difference at all).
This first part works fine for me... I will just detail all the steps so you know exactly what I am trying. To get my actor blueprint to talk with my HUD_Widget I did this: In my actor blueprint, on EventBeginPlay I drag off -> create Hud_Widget, then I drag of the return value and promote to variable (I can also just create this variable in the widget blueprint and call the reference), I name the variable Hud_Ref2. I then set to viewport and go about my business setting all my variables in both blueprints for initialization. Pic01
Now I go back into my Hud_Widget, I have a button I want to do something. Under 'On Clicked' I go into event graph and create an event dispatcher named Teleport_Selected. I drag off the 'on clicked' and call 'Teleport_Selected' Pic02
If I now go back into my Actor Blueprint I can get it to do whatever I need. If I want something to happen when the button is pressed, I drag off the event tick (if I must) -> 'BindEvent to Teleport_Selected', then create a Custom Event and go about my business. Pic03
And if I ever want a variable from my HUD_Widget, I just put the reference in and 'get' or 'set' whatever variable I want. Pic04
I can display scores, display lives, make progress bars fill, I can make my static mesh (the Actor blueprint) move depending on which button was pushed in the game. I can make the game end and change to a start menu. - Life is great and I feel like I know what I am doing.
Then I try to do the same for my Pawn Blueprint and it won't work. I start with a very simple variable and print string. At first I just tried to create a new reference to the HUD_Widget like I did with my actor. I kept getting the 'access none while trying to read property error.' I realized the only difference in the actor and pawn blueprints is at EventBeginPlay on my pawn I never did anything. So I set it up exactly like I did in pic 1 except I don't 'add to viewport' since it is already there. (If I do add to viewport my HUD stops working, I imagine because an empty instance is on top of it now?). Now this made my errors go away... and this is the only instance I can access any information from the other blueprint. Pic05
I created a variable string named 'test' in the HUD_Widget blueprint. I set the default value to "not set". Now if I start my game it will print that string one time. If I go back into HUD_Widget and change the default value of test string to "set" and restart the game... it will print it with the correct value so I know I am getting close now.
This is where my problem starts... If I try to execute a custom event off a button set exactly like the actor blueprint... it doesn't do anything. If I call my reference and try to 'get' or 'set' any variables... it also doesn't do anything. It doesn't give me any errors like 'access none' unless I leave out the bit from the EventBeginPlay. It doesn't crash or prevent anything further down the chain from executing... it just acts like an empty wire basically. Pic06
At this point I am expecting it to print the variable 'test'... every time I hit the teleport button... I have tried it all ways. I have tried referencing the Pawn and pulling to the HUD and vice versa. I have tried custom events, I tried custom events not tied to other blueprints (although I was pretty certain that did not matter). I tried it with and without the 'get player controller' connected to the Hud_Widget at EventBeginPlay. I tried casting before referencing but it mostly gives me the blue 'unnecessary' semi error.
Where am I going wrong?? What am I not understanding about casting? Any Help would be much appreciated.
asked Jan 10 '18 at 06:14 PM in Blueprint Scripting
Hey there, first of all why is the actor creating the widget? Second, doing a bind in the tick is a bad idea, you should do that once like on begin play after you have set Hud Reference 2. Finally, if the pawn you are trying to communicate on the widget is the "player" then you dont need event dispatchers, if you execute Get Owning Player Pawn on the widget it will give you a direct reference.
answered Jan 10 '18 at 06:58 PM
Man, thanks so much, I was stuck on that for days. If it makes you feel better... I have not had to use my event tick yet. I always try to find a way to avoid it. And thanks for reminding me to move whatever Custom Events I did have from the EventTicks to the EventBeginPlays.
Where I was going wrong was what I was plugging into the object type whenever I would cast to my Pawn. Initially, I tried to make a reference to the Pawn... just like I made a reference to the widget in the Actor, (only I didn't promote it from something... I just went in the HUD_Widget and created the variable, found the pawn under object types, and named it Pawn_Ref1). That is where I would get errors. If I left it blank it said error, If i put the pawn reference I just made it would tell me I didn't need it... which I understood (I already knew what it was since I had the reference).
I mean, I still don't see what the difference is between the variable reference I created is and the 'get owning player pawn' - since they both point back to the same blueprint or object in a sense.... but I suppose in 6 months I will, lol.
Anyways... This is what I was trying...
Pic 8 Is what I was talking about... I didn't know what to put in object. If I left it blank, error and if I put the reference I made it said it already knew it was that.
And this is what made it work thanks to Xlar8or
Now as Soon as I star the level it prints the string over and over... The variable "Test_Bool" is stored in the Pawn Blueprint. Every time I click the button on the widget it toggles the boolean and my print string changed from false to true or true to false.
I'm not sure I even need the stuff at the event begin play on the Pawn now? Not if I'm using the Widget to act right? I still haven't tried to go the other way... have the pawn set the widgets variables but I am not sure I need to.
Thanks Again so very Much!!!!
Awesome, will do.... how do i mark it answered... looking everywhere
answered Jan 10 '18 at 10:34 PM
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:
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'
Follow this question
Once you sign in you will be able to subscribe for any updates here