NebulaGames

C++ to Blueprints Purchase Node

Mar 1st, 2019
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.55 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "Kismet/BlueprintFunctionLibrary.h"
  7. #include "Kismet/KismetMathLibrary.h"
  8. #include "Resource_Bank.generated.h"
  9.  
  10. /**
  11.  *
  12.  */
  13.  
  14.  
  15. USTRUCT(BlueprintType)                  // Declare custom struct and expose to blueprints
  16. struct FPurchase_Cost                   // Struct name in blueprints will be "Purchase Cost"
  17. {
  18.  
  19.     GENERATED_USTRUCT_BODY()            // Struct member variables (4 main resources, 1 premium resource and a currency exchange ratio)
  20.  
  21. public:                                                                                                             // If your game has more/less resource types or no "premium" currency
  22.  
  23.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Gold"))          // You can add/remove these struct variables to suit your specific needs               
  24.         int32 Resource_1_Cost = 0;
  25.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Silver"))        // Just be sure to update the .cpp file accordingly
  26.         int32 Resource_2_Cost = 0;
  27.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Copper"))        //  The names of the resources are "fillers", simply replace the "Display Names"
  28.         int32 Resource_3_Cost = 0;
  29.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Platimun"))      // Gold, Silver, Copper, Platinum, with the names of your game's resources
  30.         int32 Resource_4_Cost = 0;
  31.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Exchange Rate")) // This variable set in the editor determines how much of the other resources 1 unit
  32.         int32 Currency_Exchange = 0;                                                                                // of the premium currency is equal to
  33.                                                                                                                     // For example if set to 100, 1 premium unit will replace a deficit of 100 units
  34. };                                                                                                                  // of any other resource
  35.  
  36. UCLASS()
  37. class CODE_TEST_API UResource_Bank : public UBlueprintFunctionLibrary
  38. {
  39.     GENERATED_BODY()
  40.  
  41. public:
  42.                                                                                                                     // The function called from blueprints, replace "Display Names" with your game's resource names
  43.         UFUNCTION(BlueprintCallable, Category = "Purchases and Upgrades")      
  44.         static void Spend_Resources(UPARAM(ref, DisplayName = "Gold") int32& Resource_1, UPARAM(ref, DisplayName = "Silver") int32& Resource_2, UPARAM(ref, DisplayName = "Copper") int32& Resource_3, UPARAM(ref, DisplayName = "Platinum") int32& Resource_4, UPARAM(ref, DisplayName = "Gems") int32& Premium_Currency, FPurchase_Cost Purchase_Cost, bool &Purchase_Successful);
  45.    
  46. };
  47.  
  48.  
  49.  
  50.  
  51.  
  52. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  53.  
  54.  
  55.  
  56. // Fill out your copyright notice in the Description page of Project Settings.
  57.  
  58. #include "Resource_Bank.h"
  59.  
  60.  
  61.  
  62. void UResource_Bank::Spend_Resources(UPARAM(ref) int32& Resource_1, UPARAM(ref) int32& Resource_2, UPARAM(ref) int32& Resource_3, UPARAM(ref) int32& Resource_4, UPARAM(ref) int32& Premium_Currency, FPurchase_Cost Purchase_Cost, bool &Purchase_Successful)
  63. {
  64.     // Determines if player has enough resources outright to complete the purchase/upgrade
  65.  
  66.     if (Resource_1 >= Purchase_Cost.Resource_1_Cost && Resource_2 >= Purchase_Cost.Resource_2_Cost && Resource_3 >= Purchase_Cost.Resource_3_Cost && Resource_4 >= Purchase_Cost.Resource_4_Cost)
  67.     {
  68.         Resource_1 -= Purchase_Cost.Resource_1_Cost;            // If player has sufficient funds for all resources, subtract cost and complete purchase
  69.         Resource_2 -= Purchase_Cost.Resource_2_Cost;
  70.         Resource_3 -= Purchase_Cost.Resource_3_Cost;
  71.         Resource_4 -= Purchase_Cost.Resource_4_Cost;
  72.  
  73.         Purchase_Successful = true;                             // Return "true" for a completed purchase
  74.     }
  75.  
  76.     // If player does not have enough, check if using the "premium" currency at the given exchange rate will cover the deficits
  77.  
  78.     else
  79.     {
  80.         TArray <int32> TempResourceArray;                       // Array to hold a copy of current player resources
  81.         TArray <int32> TempCostArray;                           // Array of the resource cost values
  82.  
  83.         int32 Temp1 = Resource_1;                               // Create temporary variables so as not to affect the referenced resource variables
  84.         int32 Temp2 = Resource_2;                               // If player has insufficient funds to complete the transaction
  85.         int32 Temp3 = Resource_3;
  86.         int32 Temp4 = Resource_4;
  87.         int32 ActualPremium = Premium_Currency;                 // Create temporary variable to hold player's premium currency
  88.         int32 TotalSpecialNeeded = 0;                           // Variable to keep track of the total amount of premium currency needed to cover deficits
  89.        
  90.         TempResourceArray.Add(Temp1);                           // Add the temporary player resource variables to an array
  91.         TempResourceArray.Add(Temp2);
  92.         TempResourceArray.Add(Temp3);
  93.         TempResourceArray.Add(Temp4);
  94.  
  95.         TempCostArray.Add(Purchase_Cost.Resource_1_Cost);       // From the struct "Purchase Cost" add the cost of each resource into an array
  96.         TempCostArray.Add(Purchase_Cost.Resource_2_Cost);
  97.         TempCostArray.Add(Purchase_Cost.Resource_3_Cost);
  98.         TempCostArray.Add(Purchase_Cost.Resource_4_Cost);
  99.  
  100.         for (int x =0; x < TempResourceArray.Num(); ++x)        // Loop through the resource array and determine which resources player has sufficient
  101.         {                                                       // Or insufficient funds for
  102.             if (TempResourceArray[x] - TempCostArray[x] >= 0)
  103.             {
  104.                 TempResourceArray[x] -= TempCostArray[x];       // Set the temporary resource variable to the value it would have if the transaction succeeds
  105.             }
  106.             else                                                // Determine the amount of premium currency needed if funds are insufficient
  107.             {
  108.                 TotalSpecialNeeded += FMath::DivideAndRoundUp(UKismetMathLibrary::Abs_Int(TempResourceArray[x] - TempCostArray[x]), Purchase_Cost.Currency_Exchange);
  109.                 TempResourceArray[x] = 0;                       // Zero out the resource that was insufficient                         
  110.             }
  111.         }
  112.        
  113.         if (ActualPremium - TotalSpecialNeeded >= 0)            // Determine if player has sufficient premium currency to cover all resource deficits
  114.         {
  115.             ActualPremium -= TotalSpecialNeeded;
  116.             Premium_Currency = ActualPremium;                   // Set player's actual premium currency to value after purchase
  117.  
  118.             Resource_1 = TempResourceArray[0];                  // Set player's actual resources to values after purchase
  119.             Resource_2 = TempResourceArray[1];
  120.             Resource_3 = TempResourceArray[2];
  121.             Resource_4 = TempResourceArray[3];
  122.  
  123.             Purchase_Successful = true;                         // Return "true" for a successful purchase
  124.         }
  125.         else
  126.         {
  127.             Purchase_Successful = false;                        // If player did not have sufficient funds/premium currency return "false", no charges made
  128.         }
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment