Hey ! So I’m a bit new to Unreal Engine. For the past week I’ve been working on a survivorslike and I’m trying to find the solution to my problem but I’ve not managed.
Right now I have a Widget that spawns on the screen when the player levels up. The widget has 3 buttons that increase a specific player stat when clicked on. As of now they are static and the functions to increase the player stat are hard coded into each button.
What I want to do is : Have the buttons be random each time this widget is opened.
What I’m struggling with : have the correct player stat & function be associated with the correct button since it is random each time the widget gets opened.
I have been reading up a lot on how to deal with this but I don’t quite understand how. I’ve read that I should maybe use a struct for this but I’m unsure on how to implement this the correct way.
That is a really interesting question! Let me show you and explain how I would do it!
First of all I want to clarify that I tried to make the system as generic as possible so you can replicate it and add/remove stats without breaking it. In addition, I’m using variables inside the Character blueprint but you can use tables if you want. Another point to clarify is that I’m opening the widget with a key for testing purposes but you can open it with whatever method you want.
Lets start with a breif explanation of the system:
To create this widget, I choose to keep the Widget as simple as possible and manage almost all the logic inside the player blueprint. This is to avoid unnecesary back and forth of variables. We will need to create an Enum with all our stats, a Struct with the data for the widget buttons, and the widget itself.
The player will store all the variables, randomize the possible buttons and process the result after clicking on any button.
So this is the step by step:
Right click on your content browser and search for “Enumeration” and create one (mine is called E_StatType):
As you can see, this will be used to define the buttons on our blueprint. The first value (StatType) references our previously created Enum.
If you didn’t create a widget for this, create one (we will develop it later, but we need to create it now because we will need to create a reference inside the player blueprint).
Open your character blueprint and create the folowing variables (variables called “MaxSOMETHING” are the variables you probably already have) the most important ones here are Stats (which is of type E_StatType) and LevelUpWidget (which is the widget with the buttons we will be using later):
This is where the fun begins! Now you need to create a function (I called it ApplyStatUpgrade), which will apply the changes to the stats when we press one of the widget buttons:
Keep in mind that you need to repeat the final four nodes for each stat you have (you can see the three missing lines going out of screen for DMG, DEF and SPD)
Now we can open our widget and start configuring it! First of all, your widget should look like this:
The final part is to manage the “OnClick” event for each of our buttons, you only need to repeat this code for each one of them making sure you use the right “Data_Option” variable for each button:
And that’s it for our system! The only thing missing here is opening the widget, I will show you how I did it from the character but you can implement it where you want:
Keep in mind that wherever you open the widget you will need to call the character’s “Generate Random Upgrade Options” function to be able to pass the Options to the widget, as you can see in the first two nodes of this blueprint.
Let me show you how it looks! (I added some prints to be able to test it)
Hope this helps you with what you need! Let me know if you need any clarification!