How to get/set reference value from another class member in another file ?

I have a class components has members like these:

in the components calss comp1.h:

public:	
    UPROPERTY(EditAnywhere)
	float MaxGrabDistance = 50;

and I tried to access it using include “comp1.h” and code like this
FVector end = start + GetActorForwardVector() * UComp1::MaxGrabDistance;

But I get error

illegal reference to non-static member ‘UComp1::MaxGrabDistance’

What is the correct way to do that ?

Using your class like this won’t work. You are trying to get a property of an object but you don’t have any object instance to get that data from. What you want to do is to create your component during the construction of an actor. Refer to the default C++ template to see how this is done.

It will look similar to this.

AMyActor::AMyActor()
{
   ...
   MyComponent = CreateDefaultSubobject<UComp1>(TEXT("Comp1"));
   ...
   FVector end = start + GetActorForwardVector() * MyComponent->MaxGrabDistance;
}
1 Like

Many thanks :sunflower:

does not work, the engine crash when I try to access the component

For anyone to be able to help you with that, you should post a little bit of code and as well the error messages found in the crash reporter window :wink:

after compile using ctrl+f11 the engine crash with this message:

Fatal error: [File:D:\build++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectGlobals.cpp] [Line: 3176] Objects have the same fully qualified name but different paths. New Object: LIVECODING_Grabber_1 /Engine/Transient.World_0:PersistentLevel.BP_Player_C_0.Grabber Existing Object: Grabber /Engine/Transient.World_0:PersistentLevel.BP_Player_C_0.Grabber

Constructor code:

ACryptRaiderCharacter::ACryptRaiderCharacter()
{
	UGrabber* GrabComp = CreateDefaultSubobject<UGrabber>(TEXT("Grabber"));
	UE_LOG(LogTemp, Warning, TEXT("grab val = %d"), GrabComp->gval);

Did you try building from your IDE? Structural changes like this often cause problems with live-coding.

yes building from live code. I will try build from code editor

building from code editor:

unresolved external symbol “public: void __cdecl UGrabber::Grab(void)” (?Grab@UGrabber@@QEAAXXZ) referenced in function “public: static void __cdecl UGrabber::execGrab(class UObject *,struct FFrame &,void * const)” (?execGrab@UGrabber@@SAXPEAVUObject@@AEAUFFrame@@QEAX@Z)