How To Make Currency System | Money System In-Game | Unreal Engine 4/5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the video and in this video I'm going to show you how you can set up a currency system inside your endail engine 4 or five the currency will be saved even after you restart your game as you can see the currency is of the same amount we left it before you can download this project and all the graphic icons from my patreon in the description below so to do it first we will need a blueprint class where we can store our currency varable to save our currency even after restarting the game we have to store it inside our save game blueprint class so right click go to blueprint class and from the all classes you can search for save game here you go let's select this and I'm going to name this coin save game now let's open this up and let's create a variable I'm going to name my currency coins you can call it anything you want now from the variable type you can either choose integer if you don't want the decimal part or you can choose float if if you want the decimal part for this tutorial I'm going to use float so now let's compile and save now let's set up the action mapping for adding and subtracting the currency values for that let's go to the project settings and under the input you can add the action mappings let's add one let's name this add coins and from the keys let's select the E key yeah there you go now let's add another action mapping now I'm going to name this subtract coins and we are going to subtract the coins when we will press the q key okay so now let's go to our character blueprint and let's quickly open this up there you go and now what we want to do uh we want to call add coin and subtract coin action mapping and this character blueprint so let's search for them there you go now in the Press we want to add the coins so for that uh let's create a custom event so search for custom events and I'm going to name this add coins so we are going to call this custom event when we will press the E key so let's call this event uh there you go add coins so when we are pressing the E key we are calling this custom event so now uh let's search for the do save game exist so we are going to check if the save game slot already exist or not so we are going to name this slot coins and now from the return value we are going to search for the branch so this will return either true or false so if it returns true then we are going to load it and let's give it the same slot name we gave it before it's coin so let's copy and paste it here okay now what we need to do we have to cast to our coin save game so why we are casting we are casting to this class to access this coin variable that is our currency so now let's promote this two variable so that we can always use this variable to access our coin I'm going to name this save game reference so we promoted the cost to the variable uh let me show you let's get this so we promoted this so that we can access the coin currency from this variable you can see this so we can access it through casting it and promoting it to the variable okay so now let's move to the false condition so if the save game is not already created then we have to create it so let's search for the create save game object there you go now from here you can select the coin save game class yeah the same one where we have our currency and now we are going to set the save game reference to the created class there you go we are setting the save game reference to both the conditions okay so now uh let's get this and now we have access to the currency variable so there you go and now we can use the add operator to add a number inside this coin currency so for for that we need a value so let's track this parameter to add coin custom event so let's select this and in the input you can change its name I'm going to call this coin now let's compile and here you can see when we are calling it we have this coin parameter where we can put some value and this value is going to be added inside the currency so uh now we can set the value of our currency to the value we got after the addition and now let's connect the execution pins for both of the conditions okay now the important part we have to save the edit value of the currency so for that let's search for the save game to slot here we have to save the currency inside the save game class so let's connect the reference and we need a slot name so just copy and paste the previous slot name that is coins so this is how we add the currencies now we need to call one more action mapping like this uh that is subtract coins so let's call this there you go and on the Press we want to call some custom event so let's create it I'm going to name this subract coins and now what we are going to do with it is we are going to copy the same thing we did with at coin event and we are going to do some minor changes on it so let's connect the execution pins okay so what change we are going to do is we are going to use minus operator rather than using plus operator so let's search for minus so here is the minus float operator and we are just going to connect this parameter to the custom event and I'm going to rename this parameter to coins okay now let's set the subtracted value to the currency and then we are saving it to the slot so this is all for the adding and subtracting the currency but now we need to call that custom event over here so let's search for it there you go and let's set the value to 200 okay so one thing to notice here is that we are loading the save game every time we are adding or subtracting the game currency so why not to do it only once as we are just storing it into a variable so let's create a separate custom event for loading our save game I have named this load save game so let's copy and paste the save game code to the custom event so we are going to call this custom event on event being play because we are just storing the save game data into this variable okay so now we don't need this code we can just delete this and directly connect the execution pin to the set currency node and let's do the same thing with the sepat coin event just connect the execution pins now let's call this custom event on event begin play so let's search for it so we want to load our save game on event begin play so call this custom event on event begin play and connect the execution pin and now we are good to go okay now let's set up a user widget where we can show our player how much game currency you have in your game so for that let's create a user widget I'm going to name this coin uncore UI now let's open this up and now let's uh search for the buttons let's drag this to the canvas panel and just give it the appropriate size now let's rename this button to coin background okay so now we need some images to apply on this button if I'll go into the graphic UI folder you can see I already have a bunch of graphic uis that you can use inside your game project you can also get this project and all these graphic uis from my patreon my patreon link is in the description below so you can get this project and all these graphic uis from there let's select the coin background image and through this small Arrow we can apply to the button and set this button as a image now do the same thing for H and pressed okay so now we can add a text inside this coin background button and this text is the amount of currency the player currently have now let's change the default amount to be 0 0 and now we can set its size to be 50 it depends on your game whatever size you want to give it and then you can change its color also I'm going to give it slightly orangish yellow okay so now we need an image uh let's track this inside the canvas panel and this image is going to be our coin icon so here it is so let's apply this to the image just click on this small Arrow there you go and you can see it's kind of distorted so use this size to context and this will give it the perfect size depends on the resolution of your icon so if your icon is too small or too big you can always resize it from outside under engine now select your icon and set it to the top left anchor and do the same for the coin background image so that they both will align properly now the important part we have to update the value of this text according to the amount of currency one way is by using The Binding but it takes a lot of performance as it runs every tick a better way to update the value of the text is whenever we will increase or decrease the coins the text value will be updated with it so to update the text value Let's uh go to the graph panel and delete the unnecessary events from here we can create a custom event I'm going to name this update coin text but here you can see uh we don't have any text variable so to access it go to designer tab uh select the text and check the is variable option now you can see we have access to the text inside the graph panel so let's get this and through this let's search for set text yeah this one connect the execution pins and now we want to set the text so that we can change it so what we want uh we want to set this coin that is our currency we want to display its value as a text so for that we need its reference so we will get this reference through the custom event let's add the input I'm going going to name this coin reference now it variable type is going to be the coin save game that is the blueprint class uh set it to be object reference now through this we can access our currency value so uh let's track this search for the coin get coin there you go let's make some space and now we can set the value of coin as a text so there you go we are finished for the update coin text custom event now we can call this custom event whenever we will add or subtract our value okay so let's make some space and call this function but for that we need to have the reference to this widget so to do it uh let's go to the event begin play of the third person and from here we can create that widget let's create it uh I'm going to select that coin UI now let's promote this to variable uh we promoted it so that we can use it later on to access that custom event uh let's rename this to coin UI reference there you go and now we can add this UI to the viewport so let's make some space and connect the execution pins okay so now we have the reference and we are also displaying it so now through this reference we can get this and we can access is our custom event uh this one update coin text this custom event through this reference there you go now let's connect the execution pin after adding the coins as you can see here we have a coin reference as a parameter in the custom event so we need to pass the reference to the custom event so let's pass it there you go connect the parameter okay so now we need to call the same custom event after subtracting the coins so uh let's move this all and let's uh call this custom event after both the events so now let's compile and test this let's play and as you can see we have this coin UI over here and if I'll press e it's increasing and I'll press Q to decrease it but one problem here is that if I'll keep on decreasing it it goes on negative value so let's see how we can fix it so here in the subtract coin custom event you can see we are simply just subtracting the value from the coins so let's uh make some space we need to add a condition here and the condition is uh when we'll get our subtracted value we need to check if it is uh smaller than zero so if it is smaller than zero let's have a branch let's make some space and if our currency is not less than zero only then we are going to subtract it okay so let's see if it solves the problem so let's decrease it and yeah I'm pressing the key but it's no more decreasing the value but there is one more issue let's say our currency is 1200 if I'll restart it you can see the value initiates with zero but whenever I will add or subtract the currency it will continue from where we left it before so the problem is we are only updating the text value whenever we are uh adding or subtracting the coin so to fix it uh on the event begin play we also need to update the value so let's call that custom event here also connect the execution pin and now it should work as you can see the text value initiates with the amount which was left before one more thing you can do is from the coin background you can uh decrease the alpha channel that will increase the transparency of your coin background button also you can get access to this website through my patreon where you can copy and paste all the blueprint code I used in this tutorial without wasting any time my patreon link is in the description below so you can check that out so that you can download the whole project graphic uis add all the blueprint code directly without wasting any time so if this video helped you out you can drop a like And subscribe to this YouTube channel my Discord server link is in the description below so you can join my Discord server from there till then see you bye-bye
Info
Channel: Vercion Games
Views: 795
Rating: undefined out of 5
Keywords:
Id: KkPBqF2IFCY
Channel Id: undefined
Length: 15min 40sec (940 seconds)
Published: Sun Mar 17 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.