C++ Class to Class commucation

Your code would crash anyway as you making function call on null pointer as MainActor is not set.

There no “class to class” communication, class is only declaring class of object, object which need to be created first (if it’s not created already (note that you code runs as part of the engine so there many object that sits in memory and you can get via various APIs) , then you can send function calls to created object. and you can create more then one… i mean all actors are AActors objects (as all actor classes base from that class). So if anything it’s “Object to Object” communication.

To make a function call or access object variables you need a pointer to actor (which technically is integer contianing memory address to object). You already made pointer variable AGround_Setup_Ground* problem is it’s not set, so compiler don’t know what value to give when this variable is allocated in memory, thats why you got error. You need to get proper address to object first to set that pointer and there many ways to do this depending what you doing

All function that create objects like SpawnActor one you used in your code CreateDefaultSubobject, return pointer to newly created object and you keep it in variable to remember it and communicate with it (you did that with BillBoardActor). But if you not created the object oyu want to communicate, you need to access it via varius APIs. In you case you want to access actor owning your component, you can do that by calling GetOwner function that every compoent have:

But your code have other issue, you doing this in constructor and at this point you component is not attach to any actor, not to mention in UE4 constructor is called during creation of so called Class Default Object (CDO) on you module startup to store default values, so by convention constructor can only set default varable states, CreateDefaultSubobject is a actually special function that creates object containing default and that object it self will be created when main object is created, you can’t use it outside of constructor.

For this reason UE4 provides different event functions that you can override that are called in different stages of object initation. like OnRegister for example:

If not try other functions:

Actor Component:
Cpp:

UGroundProximity_Check::UGroundProximity_Check()
{
	// Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
	// off to improve performance if you don't need them.
	PrimaryComponentTick.bCanEverTick = true;

	BillBoardActor = CreateDefaultSubobject<UBillboardComponent>("BillBoardComp");
	
	 FVector Origin;
	 FVector BoxExtend;
	 AGround_Setup_Ground* MainActor;
	 MainActor->GetActorBounds(false, Origin, BoxExtend);
	
	 BillBoardActor->SetWorldLocation(BoxExtend);
}

if i add #include “Ground_Setup_Ground” in GroundProximity_Check header file i get an error inside Ground_setup_Ground and GroundProximity_Check. One is a actor class(Ground_Setup_Ground) and the other is a
actor component(GroundProximity_Check), i added the actor component inside the actor class but i need some info from Ground_Setup_Ground inside GroundProximity_Check. Any way around to get that i was looking into Cast<> but dont know what i should be putting in its Arg. Also AGround_Setup_Ground* MainActor; says “Local variable ‘MainActor’ might not be initialized”.