# Interaction based on player stats?

**URL:** <https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477>\
**Category:** UI\
**Tags:** add-variable, unreal-engine, Widget, question\
**Created:** [April 26, 2021, 3:29pm UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477 "2021-04-26T15:29:01Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![super-squirrel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@super-squirrel](https://forums.unrealengine.com/u/super-squirrel)\
**Post date:** [April 26, 2021, 3:29pm UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/1 "2021-04-26T15:29:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kehel18](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kehel18](https://forums.unrealengine.com/u/Kehel18)\
**Post date:** [April 26, 2021, 4:28pm UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/2 "2021-04-26T16:28:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![super-squirrel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@super-squirrel](https://forums.unrealengine.com/u/super-squirrel)\
**Post date:** [April 26, 2021, 4:39pm UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/3 "2021-04-26T16:39:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [April 26, 2021, 6:04pm UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/4 "2021-04-26T18:04:53Z")

</div>

How familiar are you with blueprint interfaces?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [April 26, 2021, 6:30pm UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/5 "2021-04-26T18:30:47Z")

</div>

Here’s a quick & dirty one:

- you can create a blueprint interface in the Content Browser, open it and add functions:

 ![337163-01-bpinterface.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/9/b/5/9b5b3bbaf2ed97e15fea5fb4df05d75da138c48c.png)

- above, I’ve got two - one for checking the player’s health and one for healing
- below, **both** the player and the widget implement that interface:

 ![337164-02-implemented.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/6/6/e66d01b7bf36ccb78b2a52835b25ccd0eb8640d5.png)

- this is how the widget checks the player’s health and, if its value is below threshold, attempts to heal them

 ![337165-03-starthealing.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/5/2/652a31ef2cb73202e921ac48c331dd3a5b906884.png)

- and this is how this interface function is implemented in the player

![337166-04-query.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/c/1/3c1e3208181308899ca29fdff72b972424cb93f8.png)

- and the actual healing

 ![337167-05-heal.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/8/d/78d42aa3548b2de64119e348360c75cfa0ae5a18.png)

---

<div class="post-metadata">

**Author:** ![super-squirrel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@super-squirrel](https://forums.unrealengine.com/u/super-squirrel)\
**Post date:** [April 27, 2021, 12:00am UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/6 "2021-04-27T00:00:11Z")

</div>

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?).

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [April 27, 2021, 8:26am UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/7 "2021-04-27T08:26:52Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [April 27, 2021, 8:53am UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/8 "2021-04-27T08:53:36Z")

</div>

Here’s another example:

- I’ve added another function to the BP Interface - _Attempt To Heal_
- it has both Inputs and Outputs, and some default values (which actors can override)

 ![337231-01-bp-interface.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/5/f/2/5f27468f1974cea6b5466219d0b4ed59abcc8fd0.png)

* * *

- the Medkit blueprint (here, a widget - but it does not matter) calls that function, overriding one of the default values
- in this very case, it’s the Medkit’s job to dictated the threshold below which it can be used

 ![337233-02-callbp.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/9/6/b96847c9150150a5c5493b001de4a9e572d615fb.png)

* * *

- the player can override how this function works for them

 ![337234-03-player.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/8/6/a86261bf2bea2ad9a3b885fca7cce4fb10e47593.png)

 ![337235-04-player.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/d/e/ede626ee61893a7ce37321829d0b6d66c49d5741.png)

- Above, the Medkit’s call is interpreted by the Player. If their health is below the threshold the Medkit set, Player Health is increased by Heal Value
- we return a Boolean value indicating whether there was the need to use that item
- The Medkit will receive that return and can decide what to do with that information (image 2)

---

<div class="post-metadata">

**Author:** ![super-squirrel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@super-squirrel](https://forums.unrealengine.com/u/super-squirrel)\
**Post date:** [April 27, 2021, 9:52am UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/9 "2021-04-27T09:52:29Z")

</div>

“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?

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [April 27, 2021, 10:42am UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/10 "2021-04-27T10:42:03Z")

</div>

> 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?

![337166-04-query.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/c/1/3c1e3208181308899ca29fdff72b972424cb93f8.png)

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.

In the end, the choice is your.

---

<div class="post-metadata">

**Author:** ![super-squirrel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@super-squirrel](https://forums.unrealengine.com/u/super-squirrel)\
**Post date:** [April 27, 2021, 10:49am UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/11 "2021-04-27T10:49:20Z")

</div>

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?

 ![337237-playerstatsinterface.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/4/b/d4b7d9a2adc1aa7e04d2073498ff86ae0f7329ba.png)

 ![337238-medikit.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/c/a/ccafca41ccd989732cd3f6dadc85945a0aa61bde.png)

---

<div class="post-metadata">

**Author:** ![Everynone](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/everynone/32/1135017_2.png) [@Everynone](https://forums.unrealengine.com/u/Everynone)\
**Post date:** [April 27, 2021, 11:01am UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/12 "2021-04-27T11:01:44Z")

</div>

Mine isn’t? It says READ-ONLY with big bold letters right there 😃 On both of them, too!

They are always read-only as the functions have no implementation in the interface itself. There’s nothing to script - _write_.

Btw, I’m on 4.25 as well.

---

<div class="post-metadata">

**Author:** ![super-squirrel](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@super-squirrel](https://forums.unrealengine.com/u/super-squirrel)\
**Post date:** [April 27, 2021, 4:33pm UTC](https://forums.unrealengine.com/t/interaction-based-on-player-stats/482477/13 "2021-04-27T16:33:41Z")

</div>

Yeah, noticed that after posting it! ; D
