Skip to content

Instantly share code, notes, and snippets.

@mhaimes
Created February 10, 2018 05:15
Show Gist options
  • Save mhaimes/041d997ea24c5bda84a37b1c799371bd to your computer and use it in GitHub Desktop.
Save mhaimes/041d997ea24c5bda84a37b1c799371bd to your computer and use it in GitHub Desktop.
Here's the magic incantation to make Enums & Structs work in the Editor from Blueprints and from C++ land in modern versions of Unreal Engine 4
// MIT License
#include "EnumContainer.h"
FString UEnumContainer::GetStringFromE_TileState(E_TileState EnumValue)
{
FString EnumName = FString(TEXT("E_TileState"));
return EnumToString<E_TileState>(EnumName, EnumValue);
}
E_TileState UEnumContainer::GetE_TileStateFromString(const FString& String)
{
TCHAR* EnumName = TEXT("E_TileState");
UEnum* Enum = FindObject<UEnum>(ANY_PACKAGE, EnumName, true);
if (!Enum)
{
return E_TileState(0);
}
return (E_TileState)Enum->GetValueByNameString(String);
}
FString UEnumContainer::GetStringFromE_DeathCubeType(E_DeathCubeType EnumValue)
{
FString EnumName = FString(TEXT("E_DeathCubeType"));
return EnumToString<E_DeathCubeType>(EnumName, EnumValue);
}
E_DeathCubeType UEnumContainer::GetE_DeathCubeTypeFromString(const FString& String)
{
TCHAR* EnumName = TEXT("E_DeathCubeType");
UEnum* Enum = FindObject<UEnum>(ANY_PACKAGE, EnumName, true);
if (!Enum)
{
return E_DeathCubeType(0);
}
return (E_DeathCubeType)Enum->GetValueByNameString(String);
}
FStruct_PlayerGameState UEnumContainer::GetDebugSamplePlayerGameState() {
FStruct_PlayerGameState sample_state;
int width = 5, depth = 10;
sample_state.GameWidth = width;
sample_state.GameDepth = depth;
sample_state.CubeOfDeathRollPeriod = 2.0;
E_TileState TilesArray[] = {
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off, E_TileState::Off,
};
sample_state.Tiles.Empty();
sample_state.Tiles.Append(TilesArray, ARRAY_COUNT(TilesArray));
TArray<E_DeathCubeType> death_cubes;
E_DeathCubeType DeathCubesArray[] = {
E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::Indestructible, E_DeathCubeType::Indestructible, E_DeathCubeType::Indestructible, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::Basic, E_DeathCubeType::Basic, E_DeathCubeType::Basic, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::Basic, E_DeathCubeType::Chaining, E_DeathCubeType::Basic, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::Chaining, E_DeathCubeType::Basic, E_DeathCubeType::Basic, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::Indestructible, E_DeathCubeType::Basic, E_DeathCubeType::Indestructible, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None,
E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None, E_DeathCubeType::None,
};
sample_state.DeathCubes.Empty();
sample_state.DeathCubes.Append(DeathCubesArray, ARRAY_COUNT(DeathCubesArray));
sample_state.PlayerPosition = FVector2D(300, 700);
sample_state.GameSeconds = 20.0;
sample_state.GameTicks = 10;
// below here unused... for now
sample_state.Points = 0;
sample_state.TimeLeft = 0.0;
sample_state.BombsInInventory = 0;
return sample_state;
}
// MIT License
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "EnumContainer.generated.h"
UENUM(BlueprintType, meta = (DisplayName = "E_DeathCubeType"))
enum class E_DeathCubeType : uint8
{
// Empty Tile, No cube above it
None UMETA(DisplayName = "None"),
// Can and must be cleared to win the puzzle
Basic UMETA(DisplayName = "Basic"),
// When this type of cube is cleared, it sets a chain clear to the 8 adjacent cubes
Chaining UMETA(DisplayName = "Chaining"),
// This cube can't be cleared, it gets in the way and pushes the player around.
// Reserved for later difficulty levels.
Indestructible UMETA(DisplayName = "Indestructible")
};
UENUM(BlueprintType, meta = (DisplayName = "E_TileState"))
enum class E_TileState : uint8
{
// Tile is Inactive
Off UMETA(DisplayName = "Off"),
// Tile is Indicated for Selection (Hovering)
Indicated UMETA(DisplayName = "Indicated"),
// Tile was armed by a cube detonation
ArmedByCube UMETA(DisplayName = "ArmedByCube"),
// Tile was armed by a manual player action
ArmedByPlayer UMETA(DisplayName = "ArmedByPlayer")
};
UENUM(BlueprintType, meta = (DisplayName = "E_GameAction"))
enum class E_GameAction : uint8
{
// Kick Off a Roll
OneRoll UMETA(DisplayName = "OneRoll"),
// Change a tile's state
ChangeTileState UMETA(DisplayName = "ChangeTileState"),
// Blow up a cube
DestroyCube UMETA(DisplayName = "DestroyCube"),
// Move a player around in their game
MovePlayer UMETA(DisplayName = "MovePlayer"),
// Increase Roll Frequency (Game Difficulty)
ChangeCubeOfDeathRollPeriod UMETA(DisplayName = "ChangeCubeOfDeathRollPeriod")
};
template<typename T>
static FString EnumToString(const FString& enumName, const T value)
{
UEnum* pEnum = FindObject<UEnum>(ANY_PACKAGE, *enumName);
return *(pEnum ? pEnum->GetNameStringByIndex(static_cast<uint8>(value)) : "null");
}
USTRUCT(BlueprintType)
struct FStruct_PlayerGameDelta
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, EditAnywhere)
E_GameAction Action;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0))
int TileArrayIndex;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0.0))
float TimeStamp;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0))
int TickStamp;
//Constructor
FStruct_PlayerGameDelta()
{
Action = E_GameAction::OneRoll;
TileArrayIndex = 0;
TimeStamp = 0.0;
TickStamp = 0;
}
};
USTRUCT(BlueprintType)
struct FStruct_PlayerGameState
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 1))
int GameWidth;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
int GameDepth;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0.0))
float CubeOfDeathRollPeriod;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
TArray<E_TileState> Tiles;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
TArray<E_DeathCubeType> DeathCubes;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FVector2D PlayerPosition;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0.0))
float GameSeconds;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0))
int GameTicks;
// below here unused... for now
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0))
int Points;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0.0))
float TimeLeft;
UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (ClampMin = 0))
int BombsInInventory;
//Constructor
FStruct_PlayerGameState()
{
GameWidth = 5;
GameDepth = 10;
CubeOfDeathRollPeriod = 2.0;
Tiles.Init(E_TileState::Off, GameWidth * GameDepth);
DeathCubes.Init(E_DeathCubeType::None, GameWidth * GameDepth);
PlayerPosition = FVector2D(300, 700);
GameSeconds = 0.0;
GameTicks = 0;
// below here unused... for now
Points = 0;
TimeLeft = 0.0;
BombsInInventory = 0;
}
};
UCLASS()
class INFINITEQUBE_API UEnumContainer : public UObject
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintPure, Category = "E_TileState")
static FString GetStringFromE_TileState(E_TileState EnumValue);
UFUNCTION(BlueprintPure, Category = "E_TileState")
static E_TileState GetE_TileStateFromString(const FString& String);
UFUNCTION(BlueprintPure, Category = "E_DeathCubeType")
static FString GetStringFromE_DeathCubeType(E_DeathCubeType EnumValue);
UFUNCTION(BlueprintPure, Category = "E_DeathCubeType")
static E_DeathCubeType GetE_DeathCubeTypeFromString(const FString& String);
UFUNCTION(BlueprintPure, Category = "Struct_PlayerGameState")
static FStruct_PlayerGameState GetDebugSamplePlayerGameState();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment