Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Fill out your copyright notice in the Description page of Project Settings.
- #pragma once
- #include "CoreMinimal.h"
- #include "Kismet/BlueprintFunctionLibrary.h"
- #include "Kismet/KismetMathLibrary.h"
- #include "Resource_Bank.generated.h"
- /**
- *
- */
- USTRUCT(BlueprintType) // Declare custom struct and expose to blueprints
- struct FPurchase_Cost // Struct name in blueprints will be "Purchase Cost"
- {
- GENERATED_USTRUCT_BODY() // Struct member variables (4 main resources, 1 premium resource and a currency exchange ratio)
- public: // If your game has more/less resource types or no "premium" currency
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Gold")) // You can add/remove these struct variables to suit your specific needs
- int32 Resource_1_Cost = 0;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Silver")) // Just be sure to update the .cpp file accordingly
- int32 Resource_2_Cost = 0;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Copper")) // The names of the resources are "fillers", simply replace the "Display Names"
- int32 Resource_3_Cost = 0;
- UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Resource Cost", Meta = (DisplayName = "Platimun")) // Gold, Silver, Copper, Platinum, with the names of your game's resources
- int32 Resource_4_Cost = 0;
- 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
- int32 Currency_Exchange = 0; // of the premium currency is equal to
- // For example if set to 100, 1 premium unit will replace a deficit of 100 units
- }; // of any other resource
- UCLASS()
- class CODE_TEST_API UResource_Bank : public UBlueprintFunctionLibrary
- {
- GENERATED_BODY()
- public:
- // The function called from blueprints, replace "Display Names" with your game's resource names
- UFUNCTION(BlueprintCallable, Category = "Purchases and Upgrades")
- 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);
- };
- -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- // Fill out your copyright notice in the Description page of Project Settings.
- #include "Resource_Bank.h"
- 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)
- {
- // Determines if player has enough resources outright to complete the purchase/upgrade
- 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)
- {
- Resource_1 -= Purchase_Cost.Resource_1_Cost; // If player has sufficient funds for all resources, subtract cost and complete purchase
- Resource_2 -= Purchase_Cost.Resource_2_Cost;
- Resource_3 -= Purchase_Cost.Resource_3_Cost;
- Resource_4 -= Purchase_Cost.Resource_4_Cost;
- Purchase_Successful = true; // Return "true" for a completed purchase
- }
- // If player does not have enough, check if using the "premium" currency at the given exchange rate will cover the deficits
- else
- {
- TArray <int32> TempResourceArray; // Array to hold a copy of current player resources
- TArray <int32> TempCostArray; // Array of the resource cost values
- int32 Temp1 = Resource_1; // Create temporary variables so as not to affect the referenced resource variables
- int32 Temp2 = Resource_2; // If player has insufficient funds to complete the transaction
- int32 Temp3 = Resource_3;
- int32 Temp4 = Resource_4;
- int32 ActualPremium = Premium_Currency; // Create temporary variable to hold player's premium currency
- int32 TotalSpecialNeeded = 0; // Variable to keep track of the total amount of premium currency needed to cover deficits
- TempResourceArray.Add(Temp1); // Add the temporary player resource variables to an array
- TempResourceArray.Add(Temp2);
- TempResourceArray.Add(Temp3);
- TempResourceArray.Add(Temp4);
- TempCostArray.Add(Purchase_Cost.Resource_1_Cost); // From the struct "Purchase Cost" add the cost of each resource into an array
- TempCostArray.Add(Purchase_Cost.Resource_2_Cost);
- TempCostArray.Add(Purchase_Cost.Resource_3_Cost);
- TempCostArray.Add(Purchase_Cost.Resource_4_Cost);
- for (int x =0; x < TempResourceArray.Num(); ++x) // Loop through the resource array and determine which resources player has sufficient
- { // Or insufficient funds for
- if (TempResourceArray[x] - TempCostArray[x] >= 0)
- {
- TempResourceArray[x] -= TempCostArray[x]; // Set the temporary resource variable to the value it would have if the transaction succeeds
- }
- else // Determine the amount of premium currency needed if funds are insufficient
- {
- TotalSpecialNeeded += FMath::DivideAndRoundUp(UKismetMathLibrary::Abs_Int(TempResourceArray[x] - TempCostArray[x]), Purchase_Cost.Currency_Exchange);
- TempResourceArray[x] = 0; // Zero out the resource that was insufficient
- }
- }
- if (ActualPremium - TotalSpecialNeeded >= 0) // Determine if player has sufficient premium currency to cover all resource deficits
- {
- ActualPremium -= TotalSpecialNeeded;
- Premium_Currency = ActualPremium; // Set player's actual premium currency to value after purchase
- Resource_1 = TempResourceArray[0]; // Set player's actual resources to values after purchase
- Resource_2 = TempResourceArray[1];
- Resource_3 = TempResourceArray[2];
- Resource_4 = TempResourceArray[3];
- Purchase_Successful = true; // Return "true" for a successful purchase
- }
- else
- {
- Purchase_Successful = false; // If player did not have sufficient funds/premium currency return "false", no charges made
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment