So I have a normal health system, with an health bar, that increases if I pick up a health potion (and if it’s full, I can’t pick up any more health potions).
What I wanted to do is that if I have full health, each time I got a pickup, a little image of a potion would appear under my health bar, and if I pressed “1” (or any other key), that health potion would be used (I would get X amount of health) and the image would disappear, but I could only for example have at max 5 little potions (images).
How would I go about doing that? My biggest problem would be not letting the player pick up health if the potions were 5 max.
In this case, you kinda have several options. Here is the suggestion I can think of that is relatively simple:
You need to store somewhere the amount of Health Potions your character is currently carrying, so create a variable inside your character to store this value. Now, everytime your character picks up a health potion, you should first branch and check if he has full health. If the character has full health, add 1 to the number of potions stored, but before, check if the number of stored potions is less than 5. If positive, then add 1. For the effect of healing, you can quickly prototype by setting any key you want, and when pressed, first check if you have any potions stored. If positive, then subtract 1 from this value, and apply the effect (eg. Heal X health points).
For the little image of potion, you could work with UMG, creating the space under your health bar for the images. Then, you can set the number of visible images by retrieving the character’s number of health potions, and working a logic with it. I am not able to script it and print a screen for you to help right now, and also, explaining this HUD logic would become quite complex and long, so I suggest that you study some videos about UMG and its capabilities, and try to figure some starting point for this!
From what I can see, you are going the right direction. Basically, upon “picking up” a potion, you seem to be checking if the player is already at full HP (above the screenshot position), and if positive, checking if the player has reached maximum amount of potions, perfect. Then, when you press the potion button (V for your blueprint), you are “using” one instance of potion
What I found was missing: you are straight forward “consuming” a potion before checking if the player has any. I know it is completely missable aspect, but make sure to check if Current Potion > 0 before consuming it, or else you might get stuck with -2 potions during the gameplay! Also, make sure to continue the logic of using the potion, effectively restoring the HP!
Alright so the good news is that I got the HUD part working (I’m using numbers instead of doing the multiple images thing, it would clutter the screen too much).
I’ve set the Current Potion to 1 just to test to see if when I press ‘V’ it heals me for 50 health (I don’t have a damage system so this is faster to try out), but it doesn’t do anything, here’s how my BP screen looks like, I’m sure there’s either something badly linked or something missing, I feel like I’m pretty close to get this working.
I may be mistaken, but I believe that you are destroying the potion before you can use it? Destroy Actor (on self) should be the very last command, because once it is destroyed, nothing runs on it. It’s gone.
You are implementing the button input on the pickup itself.
The problem: When you pick up a pickup, you destroy it afterwards. At least that seems to be happening from your screenshots. The only time it would react to your press of the “v” button would be while it exists and it doesn’t exist anymore once you pick it up.
Actually, what happens when there’s pickups in the level? Do they actually react to your button press? From the code, it looks like they would all spontaneously disappear.
Try to implement it on your character or player controller instead. Just remember to not call “destroy actor” unless you want your character to die every time he chugs a potion.
Two random tips:
Your amount of potions is a float. Unless you want to be able to have 1.2 potions or something, an integer would make more sense.
You can set button mappings under “input” in the project settings. This will allow you to write “use potion” instead of “v”, allowing you to remap “use potion” to anything else at a later point in time.
So I’ve set the Input, I actually have done that for everything else but for some reason I forgot to do it this time.
I’ve moved the all the input related nodes to the character blueprint (which again, for some reason I was dumb enough to forget that I should’ve done that earlier).
I do get an error now, related to the Character reference, which I think I know the fix, but I don’t think that would fix the problem that my character still isn’t drinking the potion, I’ve deleted both “Destroy Actor” nodes, but it still won’t heal me.
At a glimpse, you seem to be accessing a variable that saves a reference to your ThirdPersonCharacter… Inside of the ThirdPersonCharacter.
You can just edit “Current Potion” and “Current Vit” directly from the character’s blueprint, because those variables are its own. Referencing the owner is only necessary inside of the Pickup blueprint.
As for the error message, you are reading your character reference from the variable “character” inside of ThirdPersonCharacter. Does that variable actually contain any data? You might have forgotten to set it up.
In fact, as you are using that variable to access all of the data (Potions and Health), this error messages might be the reason why nothing works.
So I don’t get an error anymore, which is good, but pressing the Heal button (in my case ‘V’) still doesn’t do anything, I’m thinking it’s the other two character references, I’ve also tried to replace them with Self but it’s the same result, or it’s something in the Pick Up blueprint, here’s how it is right now:
So, first of all:
Try to use Breakpoints. When you set a breakpoint on a node in the blueprint, the game will stop once the node is reached.
This allows you to see if some nodes are even reached.
I would wager that your first comparison will always throw a “false” result, as 0 is never bigger than any amount of potions the player can have. This would mean that nothing past the branch node would ever be reached.
I don’t see any other issues with the code in your screenshot.
For comparison, this is the quick hackjob that I did to roughly replicate what you are doing:
(Please note that this is implemented in the PlayerController on my side, so you shouldn’t need the “Get Player Pawn” and “Cast to” nodes. I’m also using the “Print String” to see the potion count, as I didn’t implement a HUD for it.)
It works now! There’s just one tiny problem, when I have full health, and I pick up a potion, the actor isn’t destroyed so this allows the player to pick up more than 1 potion out of a single actor, how can I destroy it inside the Character blueprint?
You can just call “destroy actor” after your pickup did whatever it does.
The earlier problem you had was because you were writing something that would have required the pickup to still exist after getting picked up.
Now that the entire “Press ‘consume potion’ button to decrement the potion counter and heal yourself” logic is inside of your character instead of on your pickup, you can just destroy the pickup after picking it up.
However, you could destroy the pickup from your character, depending on how picking up items works in your system. What are you using? Raytracing?
This is how I did it, the entire code of my pickup:
(“HealAmount” is the amount by which a pickup will heal. You may note that I’m using an interface event here, as I built the system to allow the “Interact” button to interact with more than just health pickups. If you don’t already know how it works, I can try to explain. The Cast that I do is also technically not an intelligent choice)
Worked wonders, thank you very much, just added a Destroy Actor at the end of the Pick Up blueprint, now I’m just going to figure out a way of not being able to use a potion if I have over 100 health, but that’s pretty easy to do I think.
If you have any other issues, let me know. It’s fun to help - I knew how to do pickups in theory for a while, but never had the motivation to implement them until now, because I got stuck at some really obtuse part of development.
Alright, everything is working perfectly now, thanks a ton for your help vb4, mcaetano15 and Yggdrasil, it was fun to understand the system more, and I think I ended up learning a thing or two about blueprint logic