Dynamically Spawning Blueprints in C++

Hello community! :slight_smile:

My first post with a question here in the forum!!! Yeah!!! :D:cool:

I would like to generate dynamic maps by using predefined “3D-Tiles” that are spawned by a C++ class. To test things I created a simple “test-tile” in the editor:

https://dl.dropboxusercontent.com/u/7757890/Basetile-24x24m-Blueprint.gif

Its a Blueprint of type “Actor” with a static FBX-mesh assigned as a component. The name of the Blueprint is “BaseTile_24x24m_BP”.

So far so good … Now I want to spawn copies of this “tile” into the gameworld.
For this I created a new C++ class called “Simulator”:


Simulator.cpp

//		*************************************************************
//		SIMULATOR CLASS
//
Simulator::Simulator()
{
	// Constructor

	FName		T = TEXT("/Game/Tiles/BaseTile_24x24m_BP");		// Path to blueprint
	FVector		P = FVector (0.0f, 0.0f, 0.0f);				// Position where to spawn
	FRotator	R = FRotator(0.0f, 0.0f, 0.0f);					// Spawn rotation
	AActor*		TestTile;								// Reference (pointer) to new tile

	// Spawn new AActor from blueprint
	TestTile = SpawnTileCopy(T, P, R);
}

For testing I’m calling “SpawnTileCopy”, a member-function of “Simulator” in the constructor. This is the declaration of SpawnTileCopy() in the .h :


Simulator.h

AActor* Simulator::SpawnTileCopy(const FName& TileNameInEditor, FVector SpawnPosition, FRotator SpawnRotation);

The purpose of this function should be to spawn a copy of the blueprint by its name “T” at a given postion “P” with a starting rotation of “R”.


Simulator.cpp

// -----------------------------------------------
// Spawn a copy of a blueprinted tile by its name/path into the gameworld
//
AActor* Simulator::SpawnTileCopy(const FName& TileNameInEditor, FVector SpawnPosition, FRotator SpawnRotation)
{
	// 1. Load pointer of AActor object (Blueprint of type "Actor" with a static mesh component assigned)
	if (TileNameInEditor == NAME_None) return NULL;
	AActor* TempAActor = Cast<AActor>(StaticLoadObject(AActor::StaticClass(), NULL, *TileNameInEditor.ToString()));

	// 2. Spawn that actor at a given position and rotation, return pointer to it
	return SpawnActor<AActor>(TempAActor, SpawnPosition, SpawnRotation, NULL, NULL);
}

/** Spawns given class and returns class T pointer, forcibly sets world position. */
template< class T >
T* SpawnActor
(
	UClass* Class,
	FVector const& Location,
	FRotator const& Rotation,
	AActor* Owner = NULL,
	APawn* Instigator = NULL,
	bool bNoCollisionFail = false
)
{
	return (Class != NULL) ? Cast<T>(GetWorld()->SpawnActor(Class, NAME_None, &Location, &Rotation, NULL, bNoCollisionFail, false, Owner, Instigator)) : NULL;
}

Unfortunately this code generates an Error “‘SpawnActor’ : undeclared identifier” … :frowning:
I’m totally lost with this … tried to bring it to work over the last couple of days - but with no success … grrrrrrr :confused:

My idea is to

  1. Load the effective adress of the blueprint with StaticLoadObject()
  2. then take this pointer to spawn the BP with SpawnActor() by using a template, that i found here.

Is this a good idea???
Is it even possible?
Am I missing something?
Maybe its just a few changes to the code?!

Pleaaaaase help me!! :):):slight_smile:

This “SpawnTileCopy()” function is totally important for me … And its also very important to have the ability to access the tiles by their names!!

Thanks for your time!

C68 :cool: