Download

[RESOLVED] Referencing UMG Widgets in Code - error LNK2019

Hello,

looking to create a button in a HUD, linked to a method of a class, I followed this tutorial: https://wiki.unrealengine.com/UMG,_R…dding_Modules:
but when I copy the code provided, and I compile / generate the code, here is the error that Visual Studio gives me:
ps: sorry for the lines in French, the lines that pose me problems are those speaking of “error LNK2019”.


  C:\Users\jreix\Documents\Unreal Projects\GenerationProcedurale\Tests\GenProc_Texture\Binaries\Win64\UE4Editor-GenProc_Texture-3541.dll : fatal error LNK1120: 4 externes non r?solus
  Manette.gen.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) class UClass * __cdecl Z_Construct_UClass_UUserWidget_NoRegister(void)" (__imp_?Z_Construct_UClass_UUserWidget_NoRegister@@YAPEAVUClass@@XZ) r?f?renc? dans la fonction "void __cdecl `dynamic initializer for 'public: static struct UE4CodeGen_Private::FClassPropertyParams const Z_Construct_UClass_AManette_Statics::NewProp_Generer''(void)" (??__E?NewProp_Generer@Z_Construct_UClass_AManette_Statics@@2UFClassPropertyParams@UE4CodeGen_Private@@B@@YAXXZ)
  Manette.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) public: static class UUserWidget * __cdecl UUserWidget::CreateWidgetInstance(class APlayerController &,class TSubclassOf<class UUserWidget>,class FName)" (__imp_?CreateWidgetInstance@UUserWidget@@SAPEAV1@AEAVAPlayerController@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z) r?f?renc? dans la fonction "class UUserWidget * __cdecl CreateWidget<class UUserWidget,class AManette>(class AManette *,class TSubclassOf<class UUserWidget>,class FName)" (??$CreateWidget@VUUserWidget@@VAManette@@@@YAPEAVUUserWidget@@PEAVAManette@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z)
  Manette.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) public: void __cdecl UUserWidget::AddToViewport(int)" (__imp_?AddToViewport@UUserWidget@@QEAAXH@Z) r?f?renc? dans la fonction "public: virtual void __cdecl AManette::BeginPlay(void)" (?BeginPlay@AManette@@UEAAXXZ)
  Manette.cpp.obj : error LNK2019: symbole externe non r?solu "__declspec(dllimport) private: static class UClass * __cdecl UUserWidget::GetPrivateStaticClass(void)" (__imp_?GetPrivateStaticClass@UUserWidget@@CAPEAVUClass@@XZ) r?f?renc? dans la fonction "class UUserWidget * __cdecl CreateWidget<class UUserWidget,class AManette>(class AManette *,class TSubclassOf<class UUserWidget>,class FName)" (??$CreateWidget@VUUserWidget@@VAManette@@@@YAPEAVUUserWidget@@PEAVAManette@@V?$TSubclassOf@VUUserWidget@@@@VFName@@@Z)

here is my two files where the code of the tutorial is (when I comment them, all compile fine) :

HEADER :


// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "Manette.generated.h"


class UUserWidget;

/**
 *
 */
UCLASS()
class GENPROC_TEXTURE_API AManette : public APlayerController
{
    GENERATED_BODY()

    public:
        // Note: that I am using forward declaration Because I am not including the
        // widget in the header and to prevent circular dependency.
        // You don't need to do that if you include the Widget Class in the .h
        // Forward declaration is just putting "class" before the class name so the compiler know it's a
        // class but it's not included in the header and don't freak out. Ex. “class UUserWidget”

        // Reference UMG Asset in the Editor
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Tests")
        TSubclassOf<UUserWidget> Generer;

        // Variable to hold the widget After Creating it.
        UUserWidget* GenBouton;

        // Override BeginPlay()
        virtual void BeginPlay() override;
};

SOURCE FILE :


// Fill out your copyright notice in the Description page of Project Settings.


#include "Manette.h"

#include "Blueprint/UserWidget.h"

void AManette::BeginPlay()
{
    Super::BeginPlay();

    if (Generer) // Check if the Asset is assigned in the blueprint.
    {
        // Create the widget and store it.
        GenBouton = CreateWidget<UUserWidget>(this, Generer);

        // now you can use the widget directly since you have a referance for it.
        // Extra check to  make sure the pointer holds the widget.
        if (GenBouton)
        {
            //let add it to the view port
            GenBouton->AddToViewport();
        }

        //Show the Cursor.
        bShowMouseCursor = true;
    }

}

I am not a veteran programmer, nor an expert on Unreal engine with C ++.
can you help me find the error or provide me with a simpler solution?

Thank you in advance for your answers.

I’m sorry for posting… My mistake was that I Put the line


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

in the wrong “build.cs” file… in another project…

Don’t be sorry. I had the same problem and this helped me solve it.