Skip to main content

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

Popular posts from this blog

Unreal Engine 4 Tutorial: Smooth Zoom using Mouse Wheel for Top Down Template

Well, I had already written a tutorial for implementing Smooth Zoom functionality for Top Down Template. But it was based on keyboard inputs. Since most Top Down PC games use mouse wheel for zoom, I decided to make a tutorial for the same. The core logic here is the same as the one implemented by Ryan Jon for the custom camera in his RTS Community Project. If anyone's interested, they can get the code for the RTS Community Project here:

UE4 RTS Community Project

Anyways, he has replaced the default camera for the characters with a general camera since that's more ideal for developing an RTS game. Since a basic Top Down game with a single playable character does not need a separate custom camera, I decided to implement the same functionality for the default player camera in UE4's Top Down Template. So let's get down to it.

First of all we need to make a custom curve from the content browser. We will be using this curve to define the smooth camera movement while zooming …

Unreal Engine Tutorial: Create Circular Ring Material without Textures

Hi, welcome back for another tutorial. It's been quite a while since I put up a tutorial as I've been busy working on toolkits for the Marketplace. One of my earlier tutorials [link: Unit Selection Decals for RTS Games] had focused on how to create ring materials using textures, as well as using them to display unit selection decals for RTS games. However, a drawback associated with using textures was that it prevented the user from changing the inner & outer radius of the ring. And during prototyping stages of a product, it's far better to implement structures that can be changed easily from within the engine so that you don't have to create a new asset from an external package every time you need to test out a new idea. I've found this approach quite useful while working on my own products. Hence, this tutorial focuses on creating a ring material that can be adjusted both from within the material editor as well as dynamically at runtime.

Alright before we get …

Unreal Engine Diaries #11: Nav Mesh Configuration

[This episode of Unreal Engine Diaries focuses primarily on nav mesh configuration, nav areas & configuration of agent types.

Source: Unreal Engine 4 AI Support Twitch Broadcast with Mieszko: https://www.youtube.com/watch?v=7LaazCv4rB0]

1. The Recast NavMesh has a couple of attributes named 'CellSize' & 'CellHeight' under the 'Generation' section in it's details panel. Together they determine the resolution of the nav mesh & lowering these values can create more precise nav meshes. It could be especially useful when there are lot of holes in the nav mesh due to the surface properties of the terrain. However, lowering them also makes the nav mesh calculations more expensive.

2. If runtime nav mesh generation is enabled, it would be best to set the 'Tile Size' attribute of Recast NavMesh to the minimum viable amount.
3. The 'Min Region Area' parameter which can also be found under 'Generation' section of Recast NavMesh can be i…