Download

How to reference from another class.

I’m UE4 C ++ beginner.
How do I write C ++ that references variables of another class
Or you should not refer to it

UCLASS ()
class HYI_API ACntdown: public AActor
{
GENERATED_BODY ()
public:
// Sets default values for this actor’s properties
ACntdown ();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay () override;
public:
// Called every frame
virtual void Tick (float DeltaTime) override;
// Cntdown timer second value set
UPROPERTY (EditAnywhere, category = “Countdown”)
int32 CntdownTime;
};
I want to reference this “CountdownTime” from another class.





UCLASS ()
class HYI_API ACntdown: public AActor{

GENERATED_BODY ()

public:

     // Sets default values for this actor's properties

     ACntdown ();

protected:

     // Called when the game starts or when spawned

     virtual void BeginPlay () override;

public:

     // Called every frame

     virtual void Tick (float DeltaTime) override;

     // Cntdown timer second value set

     UPROPERTY (EditAnywhere, category = "Countdown")

     int32 CntdownTime;

     UPROPERTY(EditAnywhere)
     class AMyAnotherClass * myAnotherClass;               //Create a pointer to your another class

};



On your Cntdown.cpp file:




#include "Cntdown.h"
#include "MyAnotherClass.h"

void ACntdown::BeginPlay(){
     myAnotherClass.DoSomething();            //myAnotherClass is your referente to another class
}



UPROPERTY Will expose the pointer myAnotherClass to <Details Pannel>, you simple drag and drop your another class into the empty slot so you can start using that class on the Cntdown script.

Thank you so mauch.
But, I can’t understand what you said below.
“You simple drag and drop your another class into the empty slot so you can start using that class on the Cntdown script.”

What is mean?

And , I wrote a source file bellow.

Countdown.cpp
#include “Countdown.h”
#include “MyActor.h”

void ACountdown::BeginPlay()
{
// Super::BeginPlay();
AMyActor.DoSomething();
}

After that , I got a error below at line “AMyActor.DoSomething();”.
“need an identifier”
What is wrong?

By the way, Please tell me about how to make code column.
Thanks a lot for reading.:stuck_out_tongue:

I found an answer.
I had overlooked that bellow 2 line.

 UPROPERTY(EditAnywhere)
 class AMyAnotherClass * myAnotherClass;

I try to build again that add 2 line.

[FONT=“Roboto”]It was useless.
What’s wrong.

I got it !!!
I added it to the reference side of the two lines of code that taught me.
Not the side to be referenced.

Then after, I was able to do build successfilly.
I will try to debug whether I can refer it.

Thanks.

That’s too bad.
Could not refer.
When played, the added reference pointer is said to be NULL.
Certainly, nothing is assigned to the added pointer.
I want to assign a pointer to an instance of the class to refer to.
How do I write the code?
Um,
GetActorInstance()?
No.
There is no such a function, I think.
Um,Um,