How to: Add new Classes that contain Static Functions

I’m sure the answer is obvious but I am in need of a guideline to get this to work.

Goal: I have to create a series of dynamic material instance colors. In C# thats as easy as creating a static utils class that has static color functions that return colors based on what I want.

Ex: static Color GetPlayerControlledUnitColor() => Color.Green; (or whatever)
Then I can just call it by saying MyUtilsClass.GetPlayerControlledUnitColor();

Unreal it seems there is more to it than that and while I can get this to work by just adding static functions to classes that already exist, I am not going to cram unrelated code in classes just to get it to work. I need a utility class that I can call to bring me static functions.

So this is what I’ve done so far.
In Unreal Editor, Created a new UObject class called MyClassStatics. It creates the .h and the .cpp for me as normal. Everythings great.

UCLASS()
class MyGame_API UMyClassStatics : public UObject
{
	GENERATED_BODY()

public:
	static FLinearColor PlayerControlledUnitColor();
	static FLinearColor AlliedControlledUnitColor();
	static FLinearColor EnemyControlledUnitColor();
};

In the .cpp

#include "MyClassStatics.h"
#include "Math/Color.h"

static FLinearColor PlayerControlledUnitColor()
{
	return FLinearColor(0.036837f, 0.619792f, 0.018987f, 1.0f);
}

static FLinearColor AlliedControlledUnitColor()
{
	return FLinearColor(0.f, 0.05f, 0.75f, 1.0f);
}

static FLinearColor EnemyControlledUnitColor()
{
	return FLinearColor(0.75f, 0.01f, 0.084f, 1.0f);
}

Then in my calling class

SetMyColor(UMyClassStatics::PlayerControlledUnitColor())

This does not build. Error LNK2019: unresolved external symbol “public: static struct FLinearColor __cdecl UMyClassStatics::PlayerControlledUnitColor(void)”

This is not a helpful error other than it seems to say it doesn’t know what FLinearColor is, even though I am including the Math/Color.h file.

Can someone point me in the right direction on what is going on here and how to fix?

Note: these files reside in my root source along with my game mode and game state, which compile just fine so I can’t see how its not registering the location properly.

Rubber Duck programming with forums for the win. I figured it out 2 minutes after posting that my syntax is wrong on the static definitions. Nothing to see here.

1 Like