How to Generate an Actor from Class Using C++?

Hello, I want to translate this blueprint into C++ but I am a little bit stuck…

void ATestClass::GenerateItems()
{
	AItemGroup* ItemGroupRef{};
	TArray<FName> OutRowNames{};
	(OutRowNames).Reset();

	if (UDataTable *DT_SpawnLocation = LoadObject<UDataTable>(NULL, TEXT("/Game/Testing/DT_SpawnLocation")))
	{
		OutRowNames = DT_SpawnLocation->GetRowNames();

		for (auto& Array : OutRowNames)
		{
			if (const UDataTable* DT_SpawnLocation{ LoadObject<UDataTable>(GetWorld(), TEXT("/Game/Testing/DT_SpawnLocation")) })
			{
				if (const FST_ItemGroupLocation * OutRow{ DT_SpawnLocation->FindRow<FST_ItemGroupLocation>(FName(Array), "") })
				{
					if (OutRow)
					{
						/*I am Stuck here... */
					}
				}
			}
		}

	}
}

You can use my example in this post:

Can't spawn ACharacter c++

1 Like

Thank you sir for reply, but the post doesn’t solved my problem… I am stuck in the node
image_2022-08-03_051253201

if (OutRow)
	{
                SpawnActor(); // I don't know how to use this in C++
		OutRow.Location; // I already have this to set in spawnTransform under SpawnActor Node
	}

The spawnActor node can be translated to c++ like this:

GetWorld()->SpawnActor(YourActorClass, SpawnTransform, SpawnInfo);

The full example from the other post:

// SpawnTransform is the location + rotation + scale (absolute) of the spawn
FTransform SpawnTransform;
// SpawnInfo contains the Collision Handling Override
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
// Spawn the actor:
// Use your own actor class:
TSubclassOf<AActor> YourActorClass = AActor::StaticClass();
AActor* NewActor = GetWorld()->SpawnActor<AActor>(YourActorClass, SpawnTransform, SpawnInfo);

Where exactly do you get stuck?

1 Like

Thank you sir for more detailed reply, but I am still stuck to set the location from the data table.

I want to do something like this. I already have the location in the data table.

MakeTransform(OutRow.Location, FRotator(0.000000,0.000000,0.000000), FVector(1.000000,1.000000,1.000000));

There are a few ways you can make an FTransform, in Visual Studio you can see a complete list of overloads.

FVector YourLocation= YourStruct.Location;
FTransform SpawnTransform = FTransform(YourLocation);

For example you can also provide the rotation and scale immediately:

FTransform(const FQuat& InRotation, const FVector& InTranslation, const FVector& InScale3D)

  • If you need to, FQuat can be made from FRotator: FQuat YourQuat = YourRotation.Quaternion()
2 Likes

You can also use this to create a transform
FTransform Trf = UKismetMathLibrary::MakeTransform(OutRow.Location, FRotator::ZeroRotator, FVector(1.f, 1.f, 1.f));

2 Likes

yes, UKismetMathLibrary is for Blueprints I maybe

Kismet libraries are indeed for Blueprints. rarely useful in c++

1 Like

yes but I don’t want to use them, I want to master pure C++ for UE4.

If you have the whole engine source, I would recommend doing Find In All Files “SpawnActor” and then you can find both the definition of the function and examples of how to use it.

1 Like

yes I am doing this and when I am failed… I come to this forum get help )

I was hoping that would be a tip that would help you find answers in the future :slight_smile:

2 Likes