[C++] how to make a variable containing an actor?


I have been learning C++ (coming from C#, I’m not having as much issues as I though I would), and this a spot for throwing this question here:

How do I “insert” a gameobject inside the script?
In c# I used to use "public GameObject ", and I could just drag and drop the object that variable would contain in the editor, and I could do things like spawn those objects inside the script afterwards.
What is the closest thing out there for C++ that would allow me to do the same (drag and drop probably not going to happen, but the outcome of spawning those objects would be crucial).



(Actor is very much like Unity’s “Prefab”, if I haven’t misunderstood that one?


No, actor is not prefab, it’s an actor :slight_smile: Unity’s actor is GameObject, and UE4’s prefab is any asset(.uasset).

Not sure what covers “prefab” in Unity actually, if it’s meant to be composed of other actors - it’s possible in UE4, since Blueprint actor can have child actors(child actor component).

Your actor is consist of components(static mesh component, skeletal mesh component, light components, etc.). You have to add it as UPROPERTY with corresponding specifiers, then you’ll be able to create Blueprint based on your actor and set meshes/audio/other parameters of the components inside editor. If you need reference to an actor in the world, you have to declare pointer to the actor of your interest, for example, if you want to reference static mesh actor that is spawned within the world, you have to add something like UStaticMeshActor* Actor in class of your actor; if you want to reference asset(in example of static mesh once again), you have to add UStaticMesh* Asset in class of your actor. That is pretty brief description, you might want to check C++ tutorials that were introduced not so long time ago. Also, wiki has lots of basic guides/code snippets that will help you get started.

So basically any functional object has to be constructed inside the code? (as in "prefab"s worked extremely similarly to blueprints. They could contain scripts, objects, child objects, etc. which could be re-used as a package, just like blueprint. Then you could assign those prefabs(blueprints) in the code, and give them a new name, with which you could spawn those saved objects to the defined transform.)

…or did I misunderstand something/use wrong term in the very beginning? (As I have a bit of a hard time understanding the difference between a blueprint and an actor, as both seem to be a collection of objects&scripts.)

Here’s a pretty good place to start:

You can define an Actor by attaching Components to it, like you would create a Prefab. You can do this in code or in the Blueprints Components interface. Blueprints are nothing more then a visual tool for generating C++ classes. When you save a Prefab in Unity it will get a file extension (.prefab perhaps?). You can think of UE4s “prefab” file extension as .cpp/.h.

To create an Actor in the world you Spawn them. Actors can Spawn other Actors to populate the world. A Gun Actor can Spawn Bullet Actors for example.

Attaching Components and Spawning Actors are the two tools and relationships between different Actors (an Actor can be a Component too as an ActorComponent type).

okay, that clears things up a lot, thanks. I snooped around the tutorials, documentation, etc. and found a function that would spawn me the actors if it would be fed the parameters needed. The question is, where do I put the function? Do I create own .cpp&.h file for it? And if so, what is the command to cast to it?. If I created an actor class, is there any way to find it from somewhere in the editor hierarchy (to edit it’s components)? Make sure to turn your answer into a, well, answer so I can give the karma to the right person :slight_smile:

(A lot of questions, but each is seemingly simple so it (hopefully) makes up for it)

Well where you wan’t to put it depends on the purpose of the function. It doesn’t work like in Unity though, you don’t create loose functions or scripts and attach them to objects. Instead you create new classes that has these functions. Of course you could create dummy classes of a Component type, give them a function and attach them to Actors but right now when you’re just getting started I think it’ll help to try and move away from the Unity way. You can always go back when you feel more confident.

Just to get you started I suggest you create a new Actor. In the Editor you can go File → Add Code to Project… and choose Actor in the list.

In your new Actor class create a function and declare it with the BlueprintCallable specifier (you also need to specify a category.

UFUNCTION(BlueprintCallable, Category = MyFunctions)
void MyFunction();

Making it BlueprintCallable will make it appear in the list of function nodes in Blueprints. It requires a Target object which may look a little confusing but what it’s asking for is an object to look for this function in. You should be able to find your new class in the Modes window under All Classes. Drag one of these into the world and you can use it as a the Target. You won’t be able to see it in the world but it’ll be in the Scene Outliner. In the .cpp file for your actor you can have your function do something simple like Log a Debug message. When you get all that running you can move on to spawn an actor instead.

Coming from Unity3D myself, this site has been helpful for me Unreal Engine 4 key concepts of C++ programming and comparison with Unity.

Blueprint is kinda like a prefab.