Subsystem not compiling if I add UFUNCTION()

Hi, I have the following code:

WidgetFocusSubsystem.h


#pragma once

#include "CoreMinimal.h"
#include "Components/Widget.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "WidgetFocusSubsystem.generated.h"

UCLASS()
class MINING_V2_API UWidgetFocusSubsystem : public UGameInstanceSubsystem
{
   GENERATED_BODY()

private:
   TArray<UWidget*> focusStack;

public:
   UFUNCTION(BlueprintCallable, Category = WidgetFocusSubsystem)
   void AddFocus(UWidget* widget);
};

WidgetFocusSubsystem.cpp


#include "WidgetFocusSubsystem.h"

void UWidgetFocusSubsystem::AddFocus(UWidget* widget)
{
   focusStack.Add(widget);
}

If I comment away the UFUNCTION line, it compiles!
What am I doing wrong?

ERROR:

This is not a compile error, this is a link error.

Your build setup file probably needs to add a reference to the UI subsystem library/dll.

The easiest way to do this is to cheat a little bit: In the project, choose “add C++ class,” and then create a subclass of UWidget called whatever you want, and have that added to your project.
Once that’s all done, the build files are updated to reference the libraries necessary to link against UI, and then you should be able to link in the other widget functions like you want to do.

Ah, sorry. I just personally call everything that has to do with “I typed something wrong” a compiler error.
I know its not technically correct. But the solution is all the same… I have to write something differently. Hehe.

Thanks, I will try out the subclass solution. Does I need to include this subclass header and pass in subclasses of that as parameters in my subsystem or does it just need to exist?

The module this belongs to is most likely missing UMG as a dependency the Build.cs file.

You don’t need to do anything with it, the whole point is to make the Unreal Editor figure out what dependencies you need to use the widget classes (UMG) and make the edits to the project for you. You can delete the subclass once it’s all set up and compiled, its only purpose is to make the Editor edit the Build.cs file for you to add the appropriate library reference.
You can also add the library reference yourself, for libraries that you know that you need, in your Project.Build.cs file.


 PublicDependencyModuleNames.AddRange(new string] { "Core", "CoreUObject", "Engine", "InputCore" });


Add whatever libraries you need to link with to this set.
If you don’t know which classes live in which libraries, is why using the editor as a shortcut is convenient :slight_smile:
You can also look up the class in the documentation:
https://docs.unrealengine.com/en-US/…get/index.html
And see that it requires the module “UMG”

That’s kind of neat.