Download

Referencing a component or actor

Hi,
I’m a senior GPP and I’m new at UE4, transferring from a proprietary engines and Unity.
I’ve dabbled with blueprints nodal editor for a bit and now I’m trying to dive into the C++ implementation by doing simple components that I’ve done on past projects…

One of these is a teleport component: upon any actor entering its associated CollisionComponent, it gets teleported to a specific Actor position/oriented defined by Level Designers.
The first part is covered by the Class Creation - C++ Only documentation, but the second part has been tricky because I don’t understand how to add a variable to my Actor Component class that would allow a designer to pick another actor in the scene as the teleporting ending point.

I was thinking to reference an actor or its SceneComponent, so I’ve searched around a bit and it seemed that FComponentReference would be the solution. However, in the editor, the associated picker cannot pick an object of the scene and if I drag an actor over its ‘Other Actor’ field, the ‘invalid’ red outline appears.

Do you have any suggestion?
Thanks a lot in advance :slight_smile:

Hey, certain variables automatically grab references to either assets in the scene or in the content browser based on where they normally belong.

For example, all actors in the scene inherit from AActor, so if you were to do this:



UPROPERTY(EditAnywhere) AActor* TeleportLocation;


It would allow you to select actors directly from the scene.

However, if you did this:



UPROPERTY(EditAnywhere) UStaticMesh* Mesh;


It would allow you to select a static mesh from the content browser. Since static meshes only exist in the content browser. They automatically get attached to empty actors when you drag them into the scene.

You can also get more specific with exactly what should be selectable.



//This will only allow you to select actors of the custom class ATeleportActor (which you would create from AActor)
//A much safer solution to avoid them accidentally selecting the floor
UPROPERTY(EditAnywhere) ATeleportActor* TeleportLocation;

//.cpp
TeleportLocation->DoStuff();



//Array of selectable locations
UPROPERTY(EditAnywhere) TArray<ATeleportActor*> TeleportLocations;

//.cpp
if(TeleportLocations[0]) TeleportLocations[0]->DoStuff();
//^ Make sure there's an actor in there and not just an empty slot in the array


//My favorite, will allow them to select actors and associate names with them so you have more control over how they behave.
UPROPERTY(EditAnywhere) TMap<FName, ATeleportActor*> NamedTeleportLocations;

//.cpp
if(NamedTeleportLocations"startzone"]) NamedTeleportLocations"startzone"]->DoStuff();
//^ Make sure there's an actor in there and not just an empty slot in the map



Note: Anything that can be placed in the scene will always inherit from AActor, and have the A prefix, which makes it easy to identify what can be placed.