Hi,
I’m learning the ropes with Unreal C++ and have encountered a bug I can’t seem to fix.
I’m trying to generate weighted random numbers through the approach of generating something like {0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,2,2,2,2,3,3,4} so 0 is more common and 4 very rare, then randomly pick one. For some reason the compiler can’t see my variables and is complaining that common–>legendary and spread are undeclared identifiers.
I don’t want to have to create an instance of SpawnRarity whenever I want to generate a random rarity, hence the ‘static’.
SpawnRarity.h:
#pragma once
#include <iostream>
#include <list>
#include "SpawnRarity.generated.h"
UCLASS()
class NOCTURNALSYNERGY_API USpawnRarity : public UObject
{
GENERATED_BODY()
private:
uint32 common = 70 * 10; // out of 1000
uint32 uncommon = 20 * 10;
uint32 rare = 8 * 10;
uint32 epic = 2 * 10;
uint32 legendary = 0.2 * 10;
std::list<int> generateSpread();
uint32 rand(uint32 min, uint32 max);
std::list<int> spread = generateSpread();
public:
static uint32 generateRandomRarity();
};
SpawnRarity.cpp:
#include "NocturnalSynergy.h"
#include "SpawnRarity.h"
#include <iostream>
#include <list>
std::list<int> generateSpread()
{
std::list<int> spread;
for(int i = 0; i < common; i++)
{
spread.push_back(0);
}
for(int i = 0; i < uncommon; i++)
{
spread.push_back(1);
}
for(int i = 0; i < rare; i++)
{
spread.push_back(2);
}
for(int i = 0; i < epic; i++)
{
spread.push_back(3);
}
for(int i = 0; i < legendary; i++)
{
spread.push_back(4);
}
return spread;
}
uint32 generateRandomRarity()
{
uint32 r = rand(0, 99);
return spread[r];
}
uint32 rand(uint32 min, uint32 max)
{
return min + (std::rand() % (max - min + 1));
}