I’ve found lots of answers on general interaction and have the basic stuff working. However, I’m stuck trying to get stats dependant interactions to work.
For example, I have a medical object in the game that the player can use. I can get the object dialog box to pop up (with a description and ‘Use’ button). However, when clicking ‘Use’, I want to add 10% to the health bar if the player’s health IS below 70%, and if the player’s health ISN’t below 70% then I have a different dialog widget that pops up saying not to waste the item.
How can I get it to check the player’s health? I tried creating a variable for third person character with type third person character object reference. Then attached that as the target of the Health variables. But I get runtime error ‘accessed None’ trying to read it. What am I doing wrong?
Hello! I unserstand that this Widget opens rather seldom, so you dont need to be troubling about optimization in this case. Then you can easily use Event OnConstructof widget and use GetPlayerPawn node then just cast it to you 3rd Person character BP class and get Health from it.
This specific widget does, but there are loads of similar widgets throughout the game where the interaction is different based on player stats. So this will be a template for all of those ‘IF stats = n1, do Y, IF stats = n2, do X’ interactions.
I’ve had very little experience with interfaces (at least using them as a go-between like you’ve done), so excuse the noob questions. I’ve set up the interface to check all my player stats and max stats (since I’ll use these a lot to determine available options in interactions) and implemented it in both thirdpersoncharacter and object blueprints. However there doesn’t seem to be anywhere in the character blueprint to plug in health as a return node output. I don’t have the Query option you’ve used above (unless it’s a different version of UE4 and they’ve changed it?).
unless it’s a different version of UE4
and they’ve changed it
Nah, it needs setting up right.
If you look at the very first image I posted - that’s my Blueprint Interface with 2 functions sitting at the top. I added and named them myself. If you did not name yours Query Player Health, you won’t see it.
And while you’re at it, do have a look at the function’s Inputs / Outputs at the bottom of that image. That’s how you pipe in the data only to have it spat out by the blueprint that implemented it.
In short:
BP Interface is a layer that consists of functions
there is no implementation, functions do nothing apart from returning default values
each actor with access to this BP Interface gets to choose how to implement that function
Do tell if it makes any sense, keep the questions coming. And if that’s of any help whatsoever, I can zip the project and drop a link here so you can inspect the elements at your own leisure.
“that’s my Blueprint Interface with 2 functions sitting at the top. I added and named them myself. If you did not name yours Query Player Health, you won’t see it.”
Sorry, I wasn’t very clear. I mean I can create a new function. I called mine ‘CheckPlayerHealth’ and added Health float as the input node and PlayerHealth float as the output node. But it doesn’t seem to call the Health stat correctly when I add PlayerHealth to object blueprints. I connected it to a basic True/False branch in the object blueprint to test (if PlayerHealth > 30 printstring A, if < 30 printstring B) and it gives a wrong answer, which tells me it isn’t retrieving the stat from the original Health float but treating it as an entirely new one
“In short: there is no implementation, functions do nothing apart from returning default values”
I just want to make sure I’m not misunderstanding… I have Health created via the ThirdPersonPlayer on starting the game. This is used by multiple blueprints throughout the game and is the info the object interactions need to determine options on the interaction dialogs that pop up. If I create the interface you’ve described above, it will retrieve the current Health stat value from the ThirdPersonPlayer via the ‘input’ on the function and then ‘output’ this value as ‘PlayerHealth’ (since I can’t reuse ‘Health’) in a usable float that I can call in object blueprints. I.e. the above interface isn’t just creating a new stat called Health?
it will retrieve the current Health
stat from the ThirdPersonPlayer via
the ‘input’ on the function and then
‘output’ this stat as ‘PlayerHealth’
(since I can’t reuse ‘Health’) in a
usable float that I can call in object
blueprints. I.e. the above interface
isn’t just creating a new stat called
Health?
This retrieves a copy of the data, it’s not passed by reference. We query, we don’t modify. If the returned value is modified by some other blueprint, it will not alter the original in the player. It’s the player’s job to modify its own data.
And that’s precisely what the implementations of Heal Player is doing.
Have a look at my example below. There the Medkit does not know or care what the player’s health is. It Attempts To Heal; the player then decides whether it was needed or not and returns results to the Medkit.
You usually don’t want to give other blueprints direct access to variables they don’t own. Generally speaking the vars should be private / protected and Setter / Getter functions of owning actors modify them upon requests from other entities.
If you want another blueprint to modify player’s data directly from the outside of that class, cast and set the var to the new value. The simplicity may seem tempting at first but as the project grows, this approach becomes less feasible and turns into a little debugging nightmare.
Ok, I’m going to have a play around with this and see if I can work out why it’s giving me a wrong answer. Although one thing I did notice is that my interface is ‘Read Only’ whereas yours isn’t. Have I forgotten to tick a box somewhere?