Download

C++ / Blueprint interaction

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!

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.

2):
C++ has SpawnActor function:

Thanks a lot, very helpful :slight_smile:

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

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



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:



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

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


Thank you so much again! :smiley:

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.

Interfaces are safe and easy to use:

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!