How to cast a variable to an Actor? C++

Hi, I have an actor class AMyActor How to get a reference of this actor?
I used AMyActor* MyActorReference but this is always returning null.

How to cast instead of making a pointer?

Thank You.

To cast actor to another type use Cast(YourVariable). Instead YourUObjectChild put a classname to cast to. On success (YourVariable originally a YourUObjectChild or it child and valid), it returns a YourVariable parameter, but of type UObjectChildYour. On fail, it returns nullptr.

AActor* Actor; // imagine where is a valid APawn object in this variable
APawn* Pawn = Cast<APawn>(Actor); // cast Actor to Pawn, return valid pawn.
UWidget* Widget = Cast<UWidget>(Actor); // return nullptr because pawn not a child of UWidget

Thank You Sir for Reply to help me, but the issue is still not solved, the AMyActor* MyActorReference is null always. I want to read, check, set some data using it only if this is not null, but this is always null

We need to confirme a couple of things:

  1. What is the class of your character?
  2. How are you getting and setting the variable?
1 Like

I will post the detail reply in few minutes


	/*Used to get Reference of the AMyActor*/
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Default")
	AMyActor* ObjectRefference;
	/*Used to attach default mesh*/
	UFUNCTION(BlueprintCallable, Category = "Component Update")
	void CheckNulled(AMyActor* ActorRefference);


	void ABaseActor::CheckNulled(AMyActor* ActorRefference)

		ObjectRefference = ActorRefference;

		if (ObjectRefference == nullptr)
			print("ObjectRefference is Nullptr"); 
			print("ObjectRefference is not Nullptr"); 

AMyActor* SomeActor;

Does not instantiate an object of that actor. It is just a nullptr.
I assume you have instantiated an AMyActor somewhere and are using that object as argument for “ABaseActor::CheckNulled(AMyActor* ActorRefference)” ?
You can instantiate one like this:

AMyActor* MyActor = NewObject<AMyActor>(this);

If this is not the problem then we need more info, code to see what you are doing.

1 Like

I am currently trying this solution, it the problem not solved I will post more detailed information and code

Out Row Cast to Datas is failed

UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = Default)
FST_ItemWeapon Datas;


if (const FST_ItemWeapon * OutRow{ DT_ItamWeapon->FindRow<FST_ItemWeapon>(FName(ID), "") })
			Datas = OutRow;  //This always failed

I need to cast Datas to OutRow

no need for casting, you are confusing pointers with the data.

FS_YourStruct YourStruct;

FS_YourStruct* StructPtr = YourDataTable->FindRow<FS_YourStruct>("ARowName", "");
if (StructPtr) {
  YourStruct = *StructPtr;

Introduction to Pointers in C++


Thank You Sir very very much for help, this solution is very perfect and working very fine :slight_smile:

void AWeaponItem::SetDefaults()
	if (const UDataTable* DT_Weapon{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/Tables/DT_Weapon")) })
		if (const FST_Weapon * OutRow{ DT_Weapon->FindRow<FST_Weapon>(FName(ID), "") })
			if (OutRow)
				Datas = *OutRow;
			/*Set the basic properties and sub meshes of the weapon mesh*/
			SetDefaults(EItemType::E_Weapon, Datas.Name, Datas.StaticMesh, Datas.Sight);