Instantiate a prefab at a certain position

I come from Unity where I instantiate a prefab like this:

  1. Load it from the Resources folder: Object[] prefabs = Resources.LoadAll(“Prefabs/Rooms”, typeof(GameObject));
  2. Load it into the world space: GameObject newRoom = (GameObject)Instantiate(prefabs[Random.Range(0, prefabs.Length)], new Vector3(0, 3, 0), Quaternion.identity);
  3. Move and rotate it: newRoom.transform.position = new Vector3(x, y, z);

Can someone help me to translate this into c++ (I know c++ but I don’t know much about UE4)?

The equivalent of prefabs are blueprints. I’m assuming you have a blueprint that is responsible for the spawn which is based on a c++ class. Inside the header file of your c++ class, add the following property:

UPROPERTY(EditAnywhere)
TSubclassOf<AYourActor> YourActorBP; // where AYourActor is the parent c++ class of the blueprint

Then, don’t forget to compile and switch to your editor and “link” the YourActorBP with the blueprint you want.

After that, somewhere in your logic (.cpp file) type in:

//Create a Spawn Parameters struct
FActorSpawnParameters SpawnParams;
//Spawning Actor...
AYourActor* AYourActor = GetWorld()->SpawnActor<AYourActor>(YourActorBP, GetTransform(), SpawnParams);

Spawn Parameters provide some useful functionality when spawning. Of course, you can create a transform you want and replace the second property I’ve provided above. There are some other overloads for the SpawnActor function, all of them can be found here

-Orfeas

In looking for information on Prefab support on these forums, I keep seeing people respond that Blueprints are the equivalent to Prefabs. This does not seem correct. Prefabs should support a collection of objects that can be combined together to form a higher level object. In order for there to be Prefab support in UE4, a Prefab would be able to have a collection of Blueprints, or even other Prefabs. Anything short of that is not a Prefab.

Here’s a concrete example. I can make a blueprint of a lightswitch that shows a staticmesh that switches back and forth, and signals On and Off. Another blueprint might be a lamp that turns on and off a light based on receive On and Off signals. Other blueprints would be a TV that randomly shows different things, a chair that rocks, etc. A Prefab would be all of these blueprints arranged with walls and a floor into a room. We could make several different room Prefabs from lots of different blueprints. We could make a house Prefab from all of our room Prefabs.

Blueprints are a far way from supporting anything of what I would want from a Prefab. I would have needed to build that entire house entirely from the smallest building blocks of lightswitches and lamps, rather than the coarser Prefabs.

Maybe the childactorcomponent is the intended Prefab support? If so, it falls flat. It has no instance editing features in the editor. I must duplicate all the properties I want to edit in a childactor in my parent Blueprint and then manually bind them to the properties of the child actor in the construction script. This is a terrible workflow. A Prefab should have its own scene tree so you can manipulate the instances of the blueprints within that prefab.

If anyone has gotten a good Prefab workflow in UE4, a tutorial would be much appreciated!

1 Like