Buying items

Head back into the Item Blueprint. Where we left off before, we allowed that upon clicking a button, we would get all actors from the Field Player class. Here, we set up a branch that checks whether the Inventory Screen Boolean is true or false (which means that we check whether the player is on the inventory screen; if they are not on the inventory screen, we will perform the buying logic in our shop).

Let's first bring in a Get Data Table Row function located under Utilities in the Actions window:

Buying items

Then, set the Data Table to Items_Shop. This will allow us to get every row from the Items_Shop Data Table. Then, link the False pin from the branch that we created to the execution of Get Data Table Row:

Buying items

You may have noticed that we can select any row name from the Data Table. In this case, we just need to get the row name of the item that is currently selected. To do this, bring in Get of the Item text variable that you created in the previous chapter in this class. You need to link it to Row Name in the Get Data Table Row function, but these pins are not compatible. So, you need to first convert the text item to a string by left-clicking and dragging it from the Item node and then navigating to Utilities | String | To String (Text). This will create the first conversion you need:

Buying items

Lastly, you can just link this converted string to the Row Name pin in the Get Data Table Row function:

Buying items

Once done, we have completed the logic for a specific item being selected in the shop. Now, we need to calculate the amount of gold that would be the value of each item and subtract it from our total gold. To do this, we must first get the RPG instance of the game so that we can call the game gold. However, since we will need this instance for a number of other variables in this Blueprint, we may want the game instance to be called part of our constructor. Create an Event Construct if you have not done so already. Next, link a Cast To RPGGameInstance object located at Casting under Utilities. Then, link the Get Game Instance object (located in the Actions window under Game) to the Cast To RPGGameInstance object:

Buying items

Since we will eventually need to access character parameters, such as HP and MP, when applying our items to the player, we will need to get all the party members, and set a Character Target similar to what we did in previous chapters. To do this, create a new variable:

Buying items

Then, go to Details | Variable, call the Character Target variable, and change its type to Game Character, which will reference our game character within the party:

Buying items

Then, from the As RPGGame Instance pin, drag out a line, and pick the Get Party Members variable by navigating to Variables | Game Data:

Buying items

To the Party Members array, link a GET function. You need to link GET to the Character Target. So, bring in a SET version of the new Character Target variable that you created, and link the GET function to the Character Target pin in SET Character Target. Lastly, have the Cast To RPGGameInstance execute SET Character Target. When you are finished setting up the reference to the game instance and game characters, your constructor will look like this:

Buying items

Now that we have set a reference to our current game instance, we can manipulate the gold. The next thing you need to do is navigate to your Get Data Table Row function. Here, left-click and drag the Out Row pin within the function, which will give you some limited options; one of these options is to create Break ItemsData. This will allow you to access all of the data for each item. Once done, you will have a box that shows all of the data that we created in our Items_Shop Data Table:

Buying items

The logic is very simple. Basically, if the user has enough money, allow them to purchase an item and subtract the cost of the item by their game gold. If they do not have enough money, do not let them purchase the item.

To do this, we will create a Get Game Gold reference. This can be found by navigating to Class | RPGGame Instance if Context Sensitive is unchecked:

Buying items

Once it is created, link the reference to As RPGGame Instance in the Cast To RPGGame Instance. You may also notice a SET pin that sets HP to 5 in the following screenshot; you may add one or leave it alone. This will just indicate that the player starts with 5 HP; this is being done for testing purposes when we test the player consuming a potion; if you decide to use Set HP for testing purposes, remember to remove it when you finish play testing:

Buying items

Now, we will subtract the game gold from the cost of the item being purchased. So, simply create a math function that subtracts an integer from an integer. This math function can be found by navigating to Math | Integer:

Buying items

To do the math correctly, we will need to link the game gold to the top pin of the minus function and the gold from ItemsData to the lower pin. This will subtract our game gold from the cost of the item:

Buying items

Here, we need to check whether the player has enough money to purchase the item. So, we will check whether the final product is less than 0. If it is, we will not allow the player to make the purchase. To make this check, simply use another math function, named Integer < Integer, located at Integer under Math. You will then compare the final product of the subtraction with 0, as shown here:

Buying items

Next, create a branch by navigating to Utilities | Flow Control, and link the condition to the condition of the Integer < Integer function you just created. Then, link the Row Found pin from the Get Data Table Row to execute the branch so that if a row is found, the math can occur:

Buying items

If the final result is not less than 0, then we need to set the game gold to the subtraction product. To do this, bring in the SET Game Gold function by navigating to Class | RPGGame Instance in the Actions window with Context Sensitive off:

Buying items

Link the Target pin of Set Game Gold to the As RPGGame Instance pin from the Cast to RPGGame Instance function. Then, link the Game Gold pin to the final product of the subtraction operation to get the remaining game gold:

Buying items

The last thing we need to do is link everything correctly. The remaining link is from the branch; if the less than condition returns false, then it means that we have enough money to buy the product, and we can change the game gold. So, next, link the False pin from the branch to execute SET Game Gold:

Buying items

If you were to test this now, you would notice that items can be purchased flawlessly from the shop. However, the problem is that the items are never being populated from the shop to the player's inventory. This is a simple fix. Earlier in this chapter, we already set our inventory screen to be able to get an array that can be stored in the Field Player. We will simply use this array to add the items that we buy to the array, and then, retrieve these items when we open our inventory:

Buying items

Since we already have a way to gather variables from the Field Player, we will bring in the Get Array Item variable by navigating to Class | Field Player.

We will link the Target pin of Array Item to the GET of the Get All Actors Of Class function so that we have full access over the arrayItem variable. We will then bring in an Add function by navigating to Utilities | Array in the Actions window:

Buying items

The Add function will allow you to add elements to an array while dynamically increasing its size (such as a list). To use this, you will link the array that you want to populate; in this case, Array Item. Then, you will need to link the item that you want to add to the array; in this case, Item. Lastly, you will need to execute Add. We will execute it after the Gold value is set. In essence, after the player buys the item, the item will then be added to their inventory:

Buying items

Your buying mechanics are now complete, and you can now test your shop. You will notice that items can be purchased and these purchased items populate your inventory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.113.229