Casting with TSubclassOf?

I’m having an issue with spawning a particular actor from collision because it needs the TSubclassOf subtext in the properties. I was curious if there was a way to us TSubclassOf when casting?

void ASpecialCharacter::OnCollision(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
	{
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("I HIT THAT"));
		AItem *Item = Cast<AItem>(OtherActor);
		AWeapon* Weapon = Cast<AWeapon>(OtherActor); // LOOKING FOR A WAY FOR OTHERACTOR TO BE A TSUBCLASSOF SUBTEXT
		if (OtherActor == Item)					//if the collided actor is an Item
		{
			GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("YAY"));
			bCanPickUp = true;					//can pick up item
			CollidedItem = Item;				//send the Item collided to the CollidedItem variable
		}
		if(OtherActor == Weapon)
		{
			CollidedItem = Weapon;
			OnSelectWeapon();
		}
	}
}

or if there was a better way of going about this?

I’m not entirely sure what you’re trying to do, but casting e.g. to AWeapon* will succeed for all subclasses of AWeapon.
So I think what you’re looking for is just to check whether the cast results in null or not.

i.e.

AItem *Item = Cast<AItem>(OtherActor);
AWeapon* Weapon = Cast<AWeapon>(OtherActor); // LOOKING FOR A WAY FOR OTHERACTOR TO BE A TSUBCLASSOF SUBTEXT

if (Item)             //if the collided actor is an Item
{
	GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("YAY"));
	....
}

if(Weapon)
{
	....
}

reason why I’m asking is because in order to use
CastSpawnActor(stufffff)
the first arguement requires a TSubclassOf Object

I’m sorry but I don’t understand what you are trying to do, and where it isn’t working. You will have to elaborate.

What I am trying to say is that I want to be able to collide with the particular actor, pick it up, and be able to spawn the actor from my inventory. But in order to make an actor to spawn, it has to be a TSubclassOf. I want to be able to collide with the actor, pick it up, and store it in an array that is a TSubclassOf. only problem is when I pick it up and attempt to spawn it, it says that the first argument in SpawnActor doesn’t match with UClass.

I was curious to see if I can store an actor that I collided with in an array that is a TSubclassOf.

to help visually, Here is my CollidedActor variable

	//Item Object to store a collided Item
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory)
	TSubclassOf<AItem> CollidedItem;

and i get an error when I try to associate the CollidedItem with the Item variable so I can transfer that to my Inventory. only problem is I do not know what to do to assign the collided item in CollidedItem.

		AItem *Item = Cast<AItem>(OtherActor);
		if (OtherActor == Item)					//if the collided actor is an Item
		{
			GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("YAY"));
			bCanPickUp = true;					//can pick up item
			CollidedItem = Item;// HERE I WANT COLLIDEDITEM TO EQUAL THE CASTED ITEM
		}

I think this is what you are trying to do:

void ASpecialCharacter::OnCollision(class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex)
{
	if ((OtherActor != NULL) && (OtherActor != this) && (OtherComp != NULL))
	{
		GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Collision"));
		AItem *Item = Cast<AItem>(OtherActor);
		if (Item) //if the collided actor is an Item
		{
			GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Red, TEXT("Collision with AItem"));
			bCanPickUp = true;
			CollidedItem = Item->GetActorClass();
		}
	}
}

You can then spawn the same class later with

GetWorld()->SpawnActor<AItem>(CollidedItem, ... );

Although this will spawn the default version of the class. If there are changes to the data in the instances of the classes that you are colliding with you will have to find some way to keep track of that to restore it when you spawn it again.

That was exactly what I was looking for !! Thank you soo much!!!