How to get value from another class member?

Hi

I have Grabber.h has this variable


public:
bool GrabKeyOn = false;

Now I need to get its value in another class file so I included the header file and do it like this but engine crash on the if statement

#include “Grabber.h”

AActor* UTriggerComponent::GetAcceptedActor() const
{
   UGrabber GrabberClass;
  if (GrabberClass.GrabKeyOn) {
    //.....
  }

}

Update:

I tried like this

  UGrabber GrabberClass;
 if (GrabberClass.GrabKeyOn)

But the Engine crash in line UGrabber GrabberClass;

How I should do this ?

**UtriggerComponent.h**
UPROPERTY()
class Grabber* Grabber;

UtriggerComponent.cpp

AActor UTriggerComponent::GetAcceptedActor () const
{

if (Grabber)
{
    Grabber.GrabKeyOn

}

and don’t forget to include your Grabber in UTriggerComponent cpp and make sure its public or use some function.

1 Like

You need to get a pointer to the specific grabber you want. As that would be on the player you can use UGameplayStatics::GetPlayerPawn(), then from there FindComponentByClass().

1 Like

I get error
use of undefined type ‘Grabber’

UtriggerComponent.h


public:

	UTriggerComponent();

	class Grabber* Grabber;

TriggerComponent.cpp

#include "TriggerComponent.h"
#include "Grabber.h"

if (Grabber.GrabKeyOn)  //error   use of undefined type 'Grabber' when I compile

I tried like this but the engine crash when I try to access the member value in UE_LOG line

TriggerComponent.h

public:

	UTriggerComponent();
	
	UGrabber* Grabber;

TriggerComponent.cpp

void UTriggerComponent::TickComponent(float DeltaTime, ELevelTick TickType,
	FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	AActor* Actor = GetAcceptedActor();
	if (Actor == nullptr) return;
	Grabber = Actor->FindComponentByClass<UGrabber>();

       UE_LOG(LogTemp, Warning, TEXT("grab key %s"),(Grabber->GrabKeyOn ? TEXT("grab on") : TEXT("grab off")));   //engine crash here

And what if the actor doesn’t have a grabber? What would FindComponentByClass return and what are you doing in the log that could be bad?

I found that once the statue overlape with the secret door the Grabber == nullptr so when I try to access Grabber->GrabKeyOn engine crash. Is there a way to just use C++ only ?

What do you mean? You are using C++

I mean without using Unreal API functions.

That’s impossible. C++ does almost nothing automatically for you, it’s not going to create an array of objects for you to be able to reference.

No I mean only access the value of a member variable of another class

You’re not doing that though. You’re accessing members on an instance of that class.

To put it in an example.

class Point
{
public:
    int Value = 0;
};

class MyClass
{
    void Foo()
    {
        // Get Point::Value some how
    }
};

int main()
{
    Point p1{ 1 };
    Point p2{ 42 }; 
    Point p3{ -10000 };
}

Which one is MyClass supposed to get here? They all have different values. They don’t all share the same.

1 Like

then is there a way that different classes use the Value member set/read with same instance to get the same value ?

When I check for Grabber it is always null

void UTriggerComponent::TickComponent(float DeltaTime, ELevelTick TickType,
	FActorComponentTickFunction* ThisTickFunction)
{

  if (Grabber == nullptr)
	{
		UE_LOG(LogTemp, Warning, TEXT("no grabber")); //always null
		return;
	}

}

By writing the code that does that which is what the Unreal API does.

Well how are you getting setting that variable?

In the Grab() and Release() functions but Grabber is always nullptr

I think you misunderstood the question? If you’re doing it like how you did here

Then that’s only going to work when you’re in the trigger volume and get returned from GetAcceotableActor, which is checking tags it doesn’t have so would be impossible.

Refer back to this

You would do this in BeginPlay.

No wanted to use flag variable instead of a tag text in Grab() and Release() and wanted to check this value in GetAcceotableActor

The question was how are you setting this Grabber variable that you are using. Grab and Release are member functions of UGrabber.

Privacy & Terms