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 ?
Marcna1:
GrabKeyOn
**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
DanM
October 24, 2022, 7:18am
3
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
Marcna1
October 24, 2022, 10:56pm
5
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
Marcna1
October 25, 2022, 12:23am
6
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
DanM
October 25, 2022, 1:46pm
7
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 ?
DanM
October 25, 2022, 4:15pm
9
What do you mean? You are using C++
Marcna1
October 25, 2022, 4:28pm
10
I mean without using Unreal API functions.
DanM
October 25, 2022, 4:40pm
11
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.
Marcna1
October 25, 2022, 4:45pm
12
No I mean only access the value of a member variable of another class
DanM
October 25, 2022, 5:01pm
13
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
Marcna1
October 25, 2022, 5:14pm
14
then is there a way that different classes use the Value member set/read with same instance to get the same value ?
Marcna1
October 25, 2022, 5:45pm
15
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;
}
}
DanM
October 25, 2022, 6:58pm
16
By writing the code that does that which is what the Unreal API does.
DanM
October 25, 2022, 6:59pm
17
Well how are you getting setting that variable?
Marcna1
October 25, 2022, 8:55pm
18
In the Grab() and Release() functions but Grabber is always nullptr
DanM
October 26, 2022, 11:48am
19
I think you misunderstood the question? If you’re doing it like how you did here
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>();
…
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.
Marcna1
October 26, 2022, 12:06pm
20
No wanted to use flag variable instead of a tag text in Grab() and Release() and wanted to check this value in GetAcceotableActor
DanM
October 26, 2022, 12:08pm
21
The question was how are you setting this Grabber variable that you are using. Grab and Release are member functions of UGrabber.