Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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)?

Product Version: UE 4.10
more ▼

asked Feb 06 '16 at 07:42 AM in C++ Programming

avatar image

12 3 5 5

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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:

 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


more ▼

answered Feb 06 '16 at 08:05 AM

avatar image

Orfeas El.
297 8 25 25

avatar image CombatJack Feb 07 '16 at 01:19 AM

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!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question