Getting errors when declaring a UUserWidget as STATIC

This is the code

.h



UCLASS()
class TESTING_API AInteractable : public AActor
{
    GENERATED_BODY()

public:    
    AInteractable();

    UPROPERTY()
    static class UUserWidget* progressWidgetReference;  // <------- static UUserWidget pointer
...
...
...


.cpp



#include "Interactable.h"
#include "Components/BoxComponent.h"
#include "Runtime/UMG/Public/Blueprint/UserWidget.h"
#include "Components/StaticMeshComponent.h"

AInteractable::progressWidgetReference = nullptr;       // <------- initialization in the cpp

AInteractable::AInteractable()
{
    ...
    ...
}


Error: Unrecognized type ‘static’ - type must be a UCLASS, USTRUCT or UENUM

If I remove the static keyword from the declaration… it works.

But what I want is having a reference to a Blueprint Widget Class so that** I can modify its variables and call its functions through C++**

1234.jpg

But… from what I can see creating a static variable isn’t allowed…

(Yes, I edited the ProjectName.Build.cs accordingly… the problem isn’t that… but the fact I can’t make my variable static).

Thanks in advance.

As the error says, you cannot have static pointers as a UPROPERTY, as you mention - removing the static would allow you to set the property and modify it via blueprints (and you can grab that pointer in C++ as well once PostLoad/PostInitializeComponents is called). So, I’m not sure what the issue here is.

The issue is I need the pointer to be STATIC and has to be set through Blueprints (UPROPERTY)

I don’t need a pointer for each instance of the class

As already stated, you can not have a static UPROPERTY.

What’s the best way to do what I want then? Since the Engine doesn’t allow me Static Uproperties…

  • I have a Widget which is basically a progress bar.
  • Each object of my class (Actor-derived) will have a **different progress value **(one may be 40% completed, the other 10%, etc…)

Sincerely I think it’s stupid to create as many Widget instances as the number of objects…

For this reason I wanted a reference to the Widget so that the Widget that spawns in the game is just ONE, and it’s value (progress value) is changed everytime the player interacts with a specific object.

You basically have three options that I can think of:

1.) Just do a Widget per Actor and see if it becomes a performance issue.
2.) Dynamically instantiate / destroy the Progress Bar when the player interacts with the object, you could do this entirely through Blueprints or C++.
3.) Put the UPROPERTY on something that you know you’ll likely only ever have one of (e.g. The Player, GameMode) and then have your object get that UPROPERTY through Blueprints/C++.

I put the pointer into the GameMode class

But the Blueprint isn’t able to see it…

Why?

Your images don’t work. You may need to cast the GameMode to your new version (MyGameMode or whatever) before you can access the field.

You should be able to grab the GameMode using: Get Game Mode | Unreal Engine Documentation

Yes this is the image:

111.jpg

It’s exactly what I do

Can you post the code where you added the field? Is it marked as BlueprintReadWrite?

No it wasn’t. Now it works. Thank you :slight_smile:

Be advised that you won’t have an instance of your GameMode in client side if you aim for a network game.