How to Cast actor to a variable to make its reference?

is this correct cast?

AMyActor* ActorRef = Cast<AMyActor>(AActor);

Hey @Alexa.Ki. Yes, that is a correct cast. Just to make sure though, the AActor you put there has to be a variable, not the class AActor, but any variable that you want to cast into a AMyActor. You also should check for a nullptr in case the cast was not successful. A simple ‘if’ is enough. Something like:

AMyActor* ActorRef = Cast<AMyActor>(AActor);
if (ActorRef)
{
    // Your code here
}

Or in one line:

if (AMyActor* ActorRef = Cast<AMyActor>(YourActorVariable))
{
    // Your code here
    // The variable ActorRef is only valid inside this scope
}
1 Like

this cast always failed

UPROPERTY(EditAnywhere, BlueprintReadWrite)
AItemGroup* ItemGroupRef = Cast<AItemGroup>(AActor);
if (::IsValid(ItemGroupRef) )
{
    print("Cast Succeeded");
}
else 
  { 
    print("Cast Failed");
  }

what is AActor in this case?

1 Like

this is what I asked in the question, I don’t know what I am doing.
I want to have a reference to my AGroupItems Class, to access the functions of the AGroupItems class

GroupItemRef->MyFunciton();

Try casting in begin play instead.

UPROPERTY(EditAnywhere, BlueprintReadWrite)
AItemGroup* ItemGroupRef = nullptr;

In begin play, replace the ??? with the variable you want to cast because AActor is just a type.
ItemGroupRef = Cast<AItemGroup>(???????);

Where’s this AItemGroup you’re trying to cast ?

1 Like

always failed

	Super::BeginPlay();
	ItemGroupRef = Cast<AItemGroup>(ItemGroupRef);
	if (ItemGroupRef)
	{
		printf("Item group is valid cast");
	}
	else
	{
		print("Item group cast failed");

	}

Again, it depends on what is the variable ItemGroupRef. If that is an UPROPERTY, maybe you haven’t set the initial value yet, so when you try to cast it to a specific class it fails, because there is nothing to cast.

If it is a value that is known from the beginning, then make sure to tag it as UPROPERTY(EditAnywhere) and assign the value in the Editor. Select the instance of your class that has that variable in the editor, go to the details panel and find where the ItemGroupRef variable is. There should be a dropdown where you can edit the default value for that variable.

2 Likes

variable is a

.h
UPROPERTY(EditAnywhere, BlueprintReadWrite)
AItemGroup* ItemGroupRef = nullptr;

I am trying to cast it on beginplay later in many functions it will access its own class to grab functions or call anything located in this AItemGroup or even will set variable data such as transforms etc

//in beginplay
AItemGroup* ItemGroupRef{};
ItemGroupRef->CallSomeFunction();//This works

but if I check for nullptr against ItemGroupRef after beginplay, it always failed.
before beginplay it was a nullptr and after beginplay it is not a nullptr, but still complaining about nullptr.

:point_up_2: This.

You need to post everything in relation to ItemGroupRef (in order, not bits and pieces) so we can help you.

1 Like

the variable created as a local variable from the node spawn actor from class later it is used to call the GetAllTransforms function.
the return of node spawn actor from class contains the group actor class and transforms.

In C++: are you casting before or after spawning the actor?
image

AItemGroup* ItemGroupRef{};
FVector SpawnTransform(EForceInit::ForceInit);
FTransform MakeTransform_Ret{};
AActor* BeginSpawnClass{};
AItemGroup* FinishSpawnClass{};

MakeTransform_Ret = UKismetMathLibrary::MakeTransform(SpawnTransform, FRotator(1.000000, 1.000000, 1.000000),FVector(1.000000, 1.000000, 1.000000));

BeginSpawnClass = UGameplayStatics::BeginDeferredActorSpawnFromClass(this, AItemGroup::StaticClass(), MakeTransform_Ret, ESpawnActorCollisionHandlingMethod::Undefined, ((AActor*)nullptr));

MakeTransform_Ret = UKismetMathLibrary::MakeTransform(SpawnTransform, FRotator(1.000000, 1.000000, 1.000000),FVector(1.000000, 1.000000, 1.000000));

FinishSpawnClass = CastChecked<AItemGroup>(UGameplayStatics::FinishSpawningActor(BeginSpawnClass, MakeTransform_Ret), ECastCheckedType::NullAllowed);

ItemGroupRef = FinishSpawnClass;
  1. I see no problem with it so far. Is that running before or after BeginPlay()?

  2. This shouldn’t compile with declaration of 'ItemGroupRef' hides class member. ??

1 Like

it compiles fine, I can call functions from its class , it only gives nullptr if i check it for nullptr, this makes me confused, It is not nullptr practically but if(IsValid(GroupItemRef)) is always false, but at the same time I can use it to call its referenced class members

You mean ItemGroupRef. :eyes:

Wish I could be of more help.

1 Like

yes ItemGroupRef my typo here , but in code I don’t have this type

tested in a new project and this works fine, something wrong with old project, thank you very much for solving the problem :slight_smile:

If something is inside an Actor class, you don’t access functions or variables inside the class. You need to have an instance of that class, and you need to have a reference to it somehow.

1 Like

AGroupActor* GroupActorRef{};
This works fine for me now.