Hi. First of all, my knowledge of Unreal and its inner working is still beginner-level and my CPP is still green.
I made a simple UMG widget that has a Canvas, which contains an image in it and I want to control the position of said Canvas image in real-time through C++. So I re-parented the widget to a C++ class I made - ReticleWidget.
ReticleWidget.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Engine/Canvas.h"
#include "ReticleWidget.generated.h"
/**
*
*/
class UCanvas;
class UUserWidget;
UCLASS()
class UMGTEST_API UReticleWidget : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(Instanced, meta = (BindWidget))
UCanvas* ReticleCanvas;
float XInit, YInit;
float GetX();
float GetY();
void SetX(float XCoord);
void SetY(float YCoord);
void ReticleUpdate();
};
ReticleWidget.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "ReticleWidget.h"
float UReticleWidget::GetX()
{
return ReticleCanvas->OrgX;
}
float UReticleWidget::GetY()
{
return ReticleCanvas->OrgY;
}
void UReticleWidget::SetX(float XCoord)
{
ReticleCanvas->OrgX = XCoord;
}
void UReticleWidget::SetY(float YCoord)
{
ReticleCanvas->OrgY = YCoord;
}
void UReticleWidget::ReticleUpdate()
{
ReticleCanvas->Update();
}
And Iâm trying to control the Canvas from a basic GameModeBase class as follows.
UMGTestGameModeBase.h
// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "GameFramework/GameMode.h"
#include "Engine/Canvas.h"
#include "ReticleWidget.h"
#include "UMGTestGameModeBase.generated.h"
/**
*
*/
class UReticleWidget;
UCLASS()
class UMGTEST_API AUMGTestGameModeBase : public AGameModeBase
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
virtual void Tick(float DeltaTime) override;
float temp;
};
UMGTestGameModeBase.cpp
// Copyright Epic Games, Inc. All Rights Reserved.
#include "UMGTestGameModeBase.h"
void AUMGTestGameModeBase::BeginPlay()
{
temp = UReticleWidget::GetX();
}
void AUMGTestGameModeBase::Tick(float DeltaTime)
{
temp += 0.1;
UReticleWidget::SetX(temp);
UReticleWidget::ReticleUpdate();
}
And when I compile, I get these errors-
|Error|C2352|âUReticleWidget::GetXâ: illegal call of non-static member function|UMGTest|C:\Users\prana\Documents\Unreal Projects\UMGTest\Source\UMGTest\UMGTestGameModeBase.cpp|8||
I get three errors for each of the three functions I try to call from UReticleWidget.
Now, Iâve obviously tried making these static in the first place but in doing so, I make ReticleCanvas unusable because âa nonstatic member reference must be relative to a specific objectâ and then I obviously canât try making the ReticleCanvas static in the first place. So what should be my course of action here? Any ideas? Thank you in advance.