public:
//Used to generate Items for Spawning Item
UFUNCTION(BlueprintCallable, meta = (Category = "ItemGeneration", OverrideNativeName = "GenerateItems"))
void GenerateItems();
//Used to Generate Random item Number for spawning Item
UFUNCTION(BlueprintCallable, meta = (Category = "ItemGeneration", OverrideNativeName = "RandomItemNumber"))
void RandomItemNumber(/*out*/ int32& Number);
//Used to Generate Random item Type for spawning Item
UFUNCTION(BlueprintCallable, meta = (Category = "ItemGeneration", OverrideNativeName = "RandomItemType"))
void RandomItemType(/*out*/ EItemType& ItemType);
//FST stands for FStruct
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (DisplayName = Datas, Category = Default, MultiLine = true, OverrideNativeName = Datas))
FST_ItemGroupLocation GroupLocationDatas;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (DisplayName = "Datas", Category = "Default", MultiLine = "true", OverrideNativeName = "Datas"))
FST_ItemNumberProbability NumberProbabilityDatas;
UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, meta = (DisplayName = "Datas", Category = "Default", MultiLine = "true", OverrideNativeName = "Datas"))
FST_ItemTypeProbability TypeProbabilityDatas;
.cpp
void AMyGameMode::GenerateItems()
{
EItemType LocItemType{};//Used locally to fill its return type function
int32 LocNumbers{}; //Used locally to fill its return type function
TArray<FName> OutRowNames{};//used to store all the row names from the data table
TArray<EItemType> ItemsTypeArr{};//used to Stores the randomly selected item types
(OutRowNames).Reset();
if (UDataTable *DT_SpawnLocationLobby = LoadObject<UDataTable>(NULL, TEXT("/Game/DataTables/ItemsGeneration/DT_ItemGroupSpawnLocationLobby")))
{
OutRowNames = DT_SpawnLocationLobby->GetRowNames();
for (auto& Array : OutRowNames)
{
if (const UDataTable* DT_SpawnLocationLobby{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/DataTables/ItemsGeneration/DT_ItemGroupSpawnLocationLobby")) })
{
if (const FST_ItemGroupLocation* OutRow{ DT_SpawnLocationLobby->FindRow<FST_ItemGroupLocation>(FName(Array), "") })
{
if (OutRow)
{
FVector Scale = FVector(1.000000, 1.000000, 1.000000);
// SpawnTransform is the location + rotation + scale (absolute) of the spawn
FTransform SpawnTransform = FTransform(GroupLocationDatas.Location);
// SpawnInfo contains the Collision Handling Override
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// Spawn the actor:
TSubclassOf<AActor> AItemGroup = AActor::StaticClass();
AActor* ItemGroupRef = GetWorld()->SpawnActor<AActor>(AItemGroup, SpawnTransform, SpawnInfo);
RandomItemNumber(LocNumbers);
for (auto i = 1; i <= LocNumbers; ++i)
{
//Randomizing the item types
RandomItemType(LocItemType); //Randomize items by type
if (ItemsTypeArr.Find(LocItemType) == -1) //check for duplicates
{
ItemsTypeArr.Add(LocItemType); // add all items to array if not duplicated
}
else if (ItemsTypeArr.Find(LocItemType) != -1)//check for duplicates
{
RandomItemType(LocItemType);//Re-randomize items to avoid any duplicate
}
}
//Testing output results string
//-----------------------------------------------------------------------------------------------
for (auto& Array : ItemsTypeArr) //For each loop
{
UE_LOG(LogTemp, Log, TEXT("The Elements in Array: %s"), *UEnum::GetValueAsString(Array));
}print("__________________________"); //prints the marker after each group is made
//-----------------------------------------------------------------------------------------------
ItemsTypeArr.Empty();
}
}
}
}
}
}
void AMyGameMode::RandomItemNumber(int32& Number)
{
TArray<int32> Arr{};
TArray<FName> OutRowNames{};
(OutRowNames).Reset();
if (const UDataTable* DT_ItemNumberProbability = LoadObject<UDataTable>(NULL, TEXT("/Game/DataTables/ItemsGeneration/DT_ItemNumbarProbability")))
{
OutRowNames = DT_ItemNumberProbability->GetRowNames();
for (auto& Array : OutRowNames) //For each loop
{
if (const UDataTable* DT_ItemNumberProbability{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/DataTables/ItemsGeneration/DT_ItemNumbarProbability")) })
{
if (const FST_ItemNumberProbability * OutRow{ DT_ItemNumberProbability->FindRow<FST_ItemNumberProbability>(FName(Array), "") })
{
if (OutRow)
{
NumberProbabilityDatas = *OutRow;
for ( int32 i = 1; i <= NumberProbabilityDatas.Percent; i++)
{
Arr.Add(NumberProbabilityDatas.Number);
}
}
}
}
}
FCustomThunkTemplates::Array_Shuffle(Arr);
Number = Arr[FMath::RandRange(0, 99)]; //Getting a copy
}
}
void AMyGameMode::RandomItemType(EItemType& ItemType)
{
TArray<EItemType> Arr;
TArray<FName> OutRowNames{};
EItemType GetByCopy{};
if (const UDataTable* DT_ItemTypeProbability = LoadObject<UDataTable>(NULL, TEXT("/Game/DataTables/ItemsGeneration/DT_ItemTypeProbability")))
{
OutRowNames = DT_ItemTypeProbability->GetRowNames();
for (auto& Array : OutRowNames)//For each loop
{
if (const UDataTable* DT_ItemTypeProbability{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/DataTables/ItemsGeneration/DT_ItemTypeProbability")) })
{
if (const FST_ItemTypeProbability * OutRow{ DT_ItemTypeProbability->FindRow<FST_ItemTypeProbability>(FName(Array), "") })
{
if (OutRow)
{
TypeProbabilityDatas = *OutRow;
for (int32 i = 1; i < TypeProbabilityDatas.Percent; i++)
{
Arr.Add(TypeProbabilityDatas.Type);
}
}
}
}
}
FCustomThunkTemplates::Array_Shuffle(Arr);
ItemType = Arr[FMath::RandRange(0, 99)];
}
}