# C++ / Blueprint interaction

**URL:** <https://forums.unrealengine.com/t/c-blueprint-interaction/84944>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [February 10, 2017, 3:33pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944 "2017-02-10T15:33:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FrancescoDeso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancescoDeso](https://forums.unrealengine.com/u/FrancescoDeso)\
**Post date:** [February 10, 2017, 3:33pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944/1 "2017-02-10T15:33:54Z")

</div>

I know that there are plenty of topics regarding this, but I couldn’t find what I needed. I have a couple of questions:

1. Suppose I have a blueprint which defines a couple of mesh components. Suppose I spawn the blueprint in the editor as an actor. In a C++ class how would I get a reference to that actor so that I can modify some properties inside C++? Is using an iterator across all actors the only way?

2. Suppose now I have create a blueprint, but NOT spawned the actor in the edtiro. Is there a way to spawn the blueprint actor directly from C++?

I’m trying to understand how can I use blueprints alongside C++. I find that blueprints make creating components very easy, but once created them and positioned to form an actor I would like to process events, variables and so on directly from C++. It’s just that I can’t find out how!

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [February 10, 2017, 6:29pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944/2 "2017-02-10T18:29:06Z")

</div>

1):  
There a many ways to do this, but you could start with “TSubClassOf” property.  
You can use that to pick an Actor directly from the game level from your Blueprint’s detail panel; in your C++ you check if the field is valid, if valid you execute your codes.

> **[TSubclassOf](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/TSubclassOf/)**

2):  
C++ has SpawnActor function:

> **[UWorld::SpawnActor](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Engine/Engine/UWorld/SpawnActor/4/)**
>
> Templated version of SpawnActor that allows you to specify location and rotation in addition to class type via the template type

---

<div class="post-metadata">

**Author:** ![FrancescoDeso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancescoDeso](https://forums.unrealengine.com/u/FrancescoDeso)\
**Post date:** [February 10, 2017, 6:41pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944/3 "2017-02-10T18:41:06Z")

</div>

Thanks a lot, very helpful 🙂

Regarding the SpawnActor function, as far as I understand it requires a C++ generated class, am I right? How can I tell it to spawn a blueprint-only actor?

Also, I forgot about another question. Suppose again I have a blueprint-only actor. What if I create a blueprint function inside of it… is there any way to call that function from C++?

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [February 10, 2017, 6:55pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944/4 "2017-02-10T18:55:09Z")

</div>

You can use that same TSubClassOf\<classname\> property to spawn an Actor.  
Example:

```auto

UPROPERTY(Category = "Template", EditAnywhere)
TSubclassOf<AMyActor> TemplateClass;

FActorSpawnParameters SpawnInfo;
AActor* MySpawned = GetWorld()->SpawnActor<AMyActor>(TemplateClass,FVector::ZeroVector,FRotator::ZeroRotator,SpawnInfo);

```

To call a Blueprint’s function from within C++, you have to use Reflection; that is complicated stuff and open for errors;  
But an example would be, using the Actor spawned above, you would do something like:

```auto

UFunction* Function = MySpawned->FindFunction(TEXT("function's name"));

if (Function) {
    MySpawned->ProcessEvent(Function,NULL);
}

```

---

<div class="post-metadata">

**Author:** ![FrancescoDeso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancescoDeso](https://forums.unrealengine.com/u/FrancescoDeso)\
**Post date:** [February 10, 2017, 7:09pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944/5 "2017-02-10T19:09:23Z")

</div>

> [@BrUnO XaVIeR;664157](#):
>
> You can use that same TSubClassOf\<classname\> property to spawn an Actor.  
> Example:
> 
> ```auto
> 
> UPROPERTY(Category = "Template", EditAnywhere)
> TSubclassOf<AMyActor> TemplateClass;
> 
> FActorSpawnParameters SpawnInfo;
> AActor* MySpawned = GetWorld()->SpawnActor<AMyActor>(TemplateClass,FVector::ZeroVector,FRotator::ZeroRotator,SpawnInfo);
> 
> ```
> 
> To call a Blueprint’s function from within C++, you have to use Reflection; that is complicated stuff and open for errors;  
> But an example would be, using the Actor spawned above, you would do something like:
> 
> ```auto
> 
> UFunction* Function = MySpawned->FindFunction(TEXT("function's name"));
> 
> if (Function) {
> MySpawned->ProcessEvent(Function,NULL);
> }
> 
> ```

Thank you so much again! 😃

Regarding the function call, seems kind of dangerous, like you said. How would you suggest to approach this?

I’ll briefly explain my actual problem. I’m messing around with the HTC Vive and I created a teleport function. Now, I would like to show a cylinder (more or less) where the user is pointing, and I want to change the color of its component depending on whether the user can teleport there or not (say, a green color if they can teleport, red if they can’t).

This marker has to be manipulated from C++, since it’s where the teleport function operates. I quickly created a blueprint class with some cylinder-like mesh and a function to change the material’s color, and I would like to use an instance of that class from C++ and still be able to call that function.

I don’t want to create a C++ class just for this actor, it would be almost pointless. I’m new to UE4, and I aim to mainly work with C++; but if I have to create a class for each mini-actor for situations like this it can be a pain. How would you approach situations like the one I described?

Ok, in my situation I could just get a reference of the blueprint instance as an AActor in C++, change the material’s color of the root component and the children’s without having to use that function, but still I woud like to know if there is a better way to handle stuff like this.

---

<div class="post-metadata">

**Author:** ![BrUnO\_XaVIeR](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bruno_xavier/32/1110230_2.png) [@BrUnO\_XaVIeR](https://forums.unrealengine.com/u/BrUnO_XaVIeR)\
**Post date:** [February 10, 2017, 9:07pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944/6 "2017-02-10T21:07:01Z")

</div>

Interfaces are safe and easy to use:

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

I would just shoot an interface event and let the whatever Blueprint change it’s own parameters when the interface call is received.

---

<div class="post-metadata">

**Author:** ![FrancescoDeso](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@FrancescoDeso](https://forums.unrealengine.com/u/FrancescoDeso)\
**Post date:** [February 10, 2017, 10:24pm UTC](https://forums.unrealengine.com/t/c-blueprint-interaction/84944/7 "2017-02-10T22:24:34Z")

</div>

> [@BrUnO XaVIeR;664228](#):
>
> Interfaces are safe and easy to use:
> 
> [A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums](https://wiki.unrealengine.com/Interfaces_And_Blueprints)
> 
> I would just shoot an interface event and let the whatever Blueprint change it’s own parameters when the interface call is received.

Didn’t know it could be done this way, thanks!
