Skip to content

Instantly share code, notes, and snippets.

@BobGneu
Created October 4, 2014 16:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save BobGneu/3344fe141e56100cb43e to your computer and use it in GitHub Desktop.
Save BobGneu/3344fe141e56100cb43e to your computer and use it in GitHub Desktop.
UE4 - DataTable Demo from Exodus
InitialUniversePopulation InitialUniverseResources InitialPlanetPopulation InitialPlanetResources Planets Bounds ResourceConsumptionRate OrbitScale LaunchTimer PopulationTimer ResourcesTimer LowPopulationGrowthRate EvenPopulationGrowthRate HighPopulationGrowthRate NoResourcesGrowthRate LowPopulationRatio EvenPopulationRatio OrbitTime MaxOrbits LaunchVelocity DeepSpace CargoSpace CrewMinimum Gravitation
Easy 4000 100000 852 1200 10 1024 0.0005 2 5 5 5 0.012 -0.042 0.001 -0.3 0.5 0.7 15 5 200 1536 250 50 1.8
Medium 3200 80000 805 1080 13 1536 0.0015 2.1 4.75 4.75 4.75 0.0144 -0.0504 0.0012 -0.36 0.525 0.735 15.75 5.45 360 2304 300 60 2.16
Hard 2560 64000 802 1026 16 2304 0.003 2.205 4.5125 4.5125 4.5125 0.0173 -0.0605 0.0014 -0.432 0.5513 0.7718 16.5375 5.9405 648 3456 360 72 2.592
Insane 2048 51200 800 975 20 3456 0.006 2.3153 4.2869 4.2869 4.2869 0.0207 -0.0726 0.0017 -0.5184 0.5788 0.8103 17.3644 6.4751 1166.4 5184 432 86.4 3.1104
void AExodusGameMode::InitGameState()
{
APlanet *planet;
Super::InitGameState();
FMath::SRandInit(LevelSeed);
int remainingResources, resourcesUsed;
AExodusGameState* const state = Cast<AExodusGameState>(GameState);
check(state);
state->SetGameDifficulty(DifficultyString);
state->OnGameStart();
NumAutoGeneratedPlanets = state->GetDifficulty()->Planets;
WorldBounds = state->GetDifficulty()->Bounds;
remainingResources = state->InitialUniverseResources;
resourcesUsed = 0;
for (int i = 0; i < NumAutoGeneratedPlanets; i++)
{
planet = CreatePlanet();
check(planet != NULL);
planet->SetPlanetName(*PlanetInfo->GetUniqueName());
state->PlanetsArray.Add(planet);
if (firstPlanet == NULL)
{
resourcesUsed = state->GetDifficulty()->InitialPlanetResources;
planet->SetPopulation(state->GetDifficulty()->InitialPlanetPopulation);
planet->SetScale(FMath::Clamp(resourcesUsed * 3.0f / MainPlanetResourcesMax, 1.0f, 2.0f));
firstPlanet = planet;
}
else
{
resourcesUsed = FMath::SRand() * FMath::Min(remainingResources, GeneralPlanetResourcesMax);
planet->SetPopulation(GeneralPlanetPopulation);
planet->SetScale(FMath::Clamp(resourcesUsed * 2.0f / GeneralPlanetResourcesMax, 1.0f, 2.0f));
}
remainingResources -= resourcesUsed;
planet->SetResources(resourcesUsed);
planet->EnsureViscinityIsClear(WorldBounds);
}
if (remainingResources > 0 && firstPlanet != NULL)
{
UE_LOG(Exodus, Log, TEXT("Lost %d resources"), remainingResources);
for (TActorIterator<APlanet> It(GetWorld()); It; ++It)
{
planet = Cast<APlanet>(*It);
if (planet == firstPlanet)
{
continue;
}
planet->SetResources(planet->GetResources() + remainingResources / (NumAutoGeneratedPlanets - 1));
}
}
AddGameEvent(TEXT("The exodus begins! The battle for the survival of the human race has started."), GetWorld()->TimeSeconds, this);
}
AExodusGameState::AExodusGameState(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
[ ... ]
Difficulties = Objects::Get<UDataTable>(TEXT("DataTable'/Game/Data/Difficulty.Difficulty'"));
DifficultyString = "Easy";
}
[ ... ]
void AExodusGameState::SetGameDifficulty(FString difficultyString)
{
DifficultyString = difficultyString;
}
FDifficulty *AExodusGameState::GetDifficulty()
{
FDifficulty* result;
result = Difficulties->FindRow<FDifficulty>(*DifficultyString, TEXT("Difficulty"));
// If the difficulty is not found, create an empty one
if (!result)
{
UE_LOG(Exodus, Warning, TEXT("Couldn't load difficulty - >%s<"), *DifficultyString);
result = new FDifficulty();
}
check(result);
return result;
}
UCLASS(config = Exodus)
class AExodusGameState : public AGameState
{
void SetGameDifficulty(FString DifficultyString);
FDifficulty *GetDifficulty();
UDataTable* Difficulties;
FString DifficultyString;
}
/// Difficulty Object
USTRUCT(BlueprintType)
struct FDifficulty : public FTableRowBase
{
GENERATED_USTRUCT_BODY()
public:
FDifficulty()
: InitialUniverseResources(100000)
, InitialUniversePopulation(4000)
, InitialPlanetPopulation(400)
, InitialPlanetResources(1200)
, Planets(10)
, Bounds(1024)
, ResourceConsumptionRate(0.0005f)
, OrbitScale(1.8)
, LaunchTimer(5.0f)
, PopulationTimer(5.0f)
, ResourcesTimer(5.0f)
, LowPopulationGrowthRate(0.012f)
, HighPopulationGrowthRate(-0.042f)
, EvenPopulationGrowthRate(0.001f)
, NoResourcesGrowthRate(-0.3f)
, LowPopulationRatio(0.5f)
, EvenPopulationRatio(0.7f)
, OrbitTime(15)
, MaxOrbits(5)
, LaunchVelocity(136)
, DeepSpace(1536)
, CargoSpace(250)
, CrewMinimum(50)
, Gravitation(1.8f)
{
}
/// Game State
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
int32 Planets;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
int32 Bounds;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
int32 InitialPlanetPopulation;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
int32 InitialPlanetResources;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
int32 InitialUniverseResources;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
int32 InitialUniversePopulation;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float ResourceConsumptionRate;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float OrbitScale;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float LaunchTimer;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float PopulationTimer;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float ResourcesTimer;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float LowPopulationGrowthRate;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float HighPopulationGrowthRate;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float EvenPopulationGrowthRate;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float NoResourcesGrowthRate;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float LowPopulationRatio;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float EvenPopulationRatio;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float OrbitTime;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float MaxOrbits;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float LaunchVelocity;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float DeepSpace;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float CargoSpace;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float CrewMinimum;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameDifficulty)
float Gravitation;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment