跳至主要內容

F14.SpawnActorFromClass

Mr.Si小于 1 分钟u++

头像

SpawnActorFromClass顾名思义,从类构建生成对象

简单实例

角色按1生成一个球

cpp

	//获取玩家ActorforwardVector
	FVector forwardVector = GetActorForwardVector();
	//获取玩家ActorLocation
	FVector location = GetActorLocation();

	//ForwardVector*200+Location
	FVector const spawnLocation = forwardVector * 200 + location;

	UWorld* const World = GetWorld();
	//断言
	check(World);
	
	 World->SpawnActor<AActor>(CurrentTarget,spawnLocation,FRotator::ZeroRotator);

2024/0803 摘自大佬博客open in new window

SpawnActor流程

SpawnActorDeferred 流程

	// 延迟生成
	AInventoryItemActor* SpawnedItem =  GetWorld()->SpawnActorDeferred<AInventoryItemActor>(AInventoryItemActor::StaticClass(),SpawnTransform);
	if (SpawnedItem)
	{
		// 将设置好的数据指针赋值给生成的Actor
		SpawnedItem->SetPickUpData(InventoryPickUpData);
		// 完成生成
		SpawnedItem->FinishSpawning(SpawnTransform);
	}