Unreal Engine Tutorial: How to use curves to control the prices of in-game items

Hi, welcome back for another tutorial. This time around, we're going to learn how to use curves to control the price of an item over time. I'm going to start with the Third Person template that is available by default in the Unreal Engine. 

First of all, we need to create a Float Curve along which we will plot the price of the item relative to time. To do so, just right click on the content browser, then select Miscellaneous >> Curve & choose the Float Curve option. Now open it up & add some points in the graph to define a curve. This can be done by right clicking anywhere on the graph & selecting 'Add key to curve'. Also in order to set accurate values, just click on the point & set 'Time' & 'Value' according to your requirements on the top left corner. I'm using the following values for this tutorial:

Time = 0; Value = 1
Time = 2; Value = 2
Time = 4; Value = 1

You may notice that it will generate a very sharp curve by default. In order to change this behavior, just try out the other interpolation methods available on the menu bar in the top. 



Next I just setup the scene with a desk and a couple of glowing cube meshes on it. They're all purely for visual purposes. There's really no need to do so.



Now we're going to create an actor blueprint named 'BP_ItemManager'. Within this blueprint, we'll add script to control the price of items based on the game time. But before that let's create a struct [Struct_ItemAttributes] to list the properties of the items. I've added a Name parameter of type 'String' plus Cost & GameTimeMultiplier parameters both of type 'Float'. The Game Timer Multiplier will be used to control the relation between the price of an item & game time by multiplying the game time with this value.



Now open the Item Manager blueprint & create an array of type Struct_ItemAttributes. Then give some default values to the different items. For example, these are the elements of my Item Array:

Element 0 >> Name = Green; Cost = 100; GameTimeMultiplier = 1
Element 1 >> Name = Orange; Cost = 100; GameTimeMultiplier = 3.6

The higher GameTimeMultiplier value of the orange item means that it's price will vary along the curve at a much faster pace than the green item.

We also need another variable of type Float Curve named 'PriceCurve' to hold a reference to our price curve. Make sure that it's default value is set to 'Price Curve'. 

Now we're going to create a function that can retrieve the curve float value at any point of time based on the game time as well as gametimemultiplier. First we find out the time range for the price curve. Then we do a modulo of the game time divided by this time range. The modulo operation will make sure that we will always get the required curve value, even if the game time has exceeded the max curve time plotted in the graph, thus essentially using the curve like a repeating wave pattern. The gametimemultiplier will be taken as an input parameter while the curvefloatvalue will be returned as an output parameter.



In order to retrieve the prices of all the items in the item array, we're going to add another function in the Item Manager named 'RetrieveCurrentPrice'. This function will loop through the items array, call the 'GetCurrentCurveValue' by passing in the GameTimeMultiplier from the struct & then multiply the returned curve value with the cost element. It then appends the result with the item name & prints it on to the screen.




All that's left now is to call this function from the player character. We can use 'Get all actors of class' at event begin play to store a reference to the Item Manager actor. When we press the required input button, we'll use this actor reference to execute the 'RetrieveCurrentPrice' function.



If we go ahead and the press the F button now, the function will print out the price of both items based on the game time at that point.

Alright, so that's it for this tutorial. I'll be posting a new Unreal Engine Diaries episode soon to continue on with Part II of the Basics of Particle Systems series. Until then, goodbye & have a great weekend. :)

Comments