Casting from c++ to blueprint

When I try to cast my c++ actor class to it’s blueprint class, it fails. I’m not sure why since the blueprint class was created from the c++ class.

The createItem function spawns a new Aitem and returns its pointer. In blueprints, I then attempt to cast it but it always fails : image

.

Aitem* ADungeonLevel::createItem(FName iname)
{
Aitem *newitem = NULL;
FVector location(500.f, 500.f, 500.f);
FRotator rotation(0.f, 0.f, 0.f);
FActorSpawnParameters spawninfo;
spawninfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;

for (int i = 0; i < ItemsPtr->AllItems.Max(); i++)
{
	for (int n = 0; n < ItemsPtr->AllItems[i].Items.Max(); n++)
	{
		if (ItemsPtr->AllItems[i].Items[n].EditorName == iname)
		{
			newitem = GetWorld()->SpawnActor<Aitem>(location, rotation, spawninfo);
			
			// set item data
			newitem->setItemData(ItemsPtr->AllItems[i].Items[n]);

			break;
		}
	}

	if (newitem) break;
}

if(!newitem) UE_LOG(LogItem, Error, TEXT("Error creating new item") );

return newitem;
}

So you try to cast a AItem to AItem or what? Your question isn’t really clear, perhaps you can add a BP screenshot

Thanks I tried to clarify it a bit. I have a blueprint class that is created from my actor c++ class. When I try to cast my c++ class to the blueprint class it fails?

is your spawn, and function successful? Is it really the cast that fails or the getting of data to cast?

Also is AItem inherited from BP_Itembase?

BP_Itembase was created by “create blueprint class based on item” in the content browser by right clicking on the CPP class.

The spawn is successful and I can manipulate the Aitem object before I try to cast it and can even get it to render its sprite, but for whatever reason it won’t cast to a blueprint class that was derived from the c++ class.

I think I know what is going wrong, you are trying to 'down’cast. A super class is casted to a child class. That isn’t possible, you can only cast ‘up’, to a super class.

Ah that’s a bummer but makes sense now. Thanks for the answer. Back to the drawing board…