How would have a C++ class exposed to blueprint, that has multiply execes outputs?
Can’t seem to find anything on google
Sorry, but it’s not really possible to have a C++ function have multiple Blueprint exec outputs.
You could consider first creating a custom Enum in C++, then expose to Blueprints and then use it as the return value of your C++ function. Then, in the Blueprint, you can use a SwitchOn node from the Enum to split the Exec pin for the different outcomes.
Just did that, but is there a reason why the enum “Looping” dont output everytime a loop goes through?
It only outputs when the loop is done
The concepts of execution in C++ and Blueprints are similar but different. Execution is more rigid and orderly in C++. It isn’t limiting, you just have to understand the rules and tackle the problem from the right perspective.
In C++, execution cannot branch out from functions. Execution enters the function when it is called and only breaks out when it reaches the end of the function or a return
statement.
For your current function, you should have the return type be the Enum instead of void
. and then have it return
the desired outcome.
So, your choice is to either look at the problem from the perspective of C++ execution, or you use a Blueprint Macro. If you want to stick with a C++ function, then you should be able to have the function call different other functions for each different path, all from inside the function, instead of trying to “break out” of it.
Delegates and events can also be helpful in C++, but I don’t think you need them here.
Okay thanks,
Since im quit new to c++, i find it a bit hard to find the soultion for my problem.
Is there any chance that you might setup a simple class that does what im trying to archive?
Also, perhaps many other new users that get’s into c++ might find this helpful
Well, I would ask, why do you need this function in the first place? It’s just a function around a for
loop… you should just use a for
loop directly in your code instead of building a function around it.
Also, you should be using int
s instead of float
s for A and B.
The whole concept of the function, is just to learn and test a few things.
Im currently on the topic of simple adding more outputs execs for a c++ class that is exposed to a blueprint
Reason for that, is i dont wanna use c++ for everything, other than heavy mechanics sutch as loops, calculation etc etc.
Okay, got it. So, for an Actor-based class, you could do something like this:
NewTesting.h
#pragma once
#include "GameFramework/Actor.h"
#include "NewTesting.generated.h"
UCLASS()
class FLEXIPROJECT_API ANewTesting : public AActor {
GENERATED_BODY()
public:
ANewTesting();
void TestFunction(int A, int B);
void Looping(int Index);
void LoopDone();
};
NewTesting.cpp
#include "NewTesting.h"
ANewTesting::ANewTesting() {
}
void ANewTesting::TestFunction(int A, int B) {
for (int i = A; i < B; i++) {
Looping(i);
}
LoopDone();
}
void ANewTesting::Looping(int Index) {
UE_LOG(LogTemp, Warning, TEXT("Looping, index %d"), Index);
}
void ANewTesting::LoopDone() {
UE_LOG(LogTemp, Warning, TEXT("Loop done"));
}
The UE_LOG calls will print to the Output Log. (They are warnings just so they are more obvious).
Since, the goal is to expose this c++ class to BP, i would have “Exces” one for Looping and one for when the loop is done.
I have already made these functions above, but the problem is that i dont really know how to expose them to an “Exces” for a blueprint?
Sorry i should prob have been more clear in the content.
basically asking, how would i go about making c++ class that can output from different exces
For simpler way, you can use BlueprintImplementableEvent.
MyActor.h
class AMyActor : public AActor
{
GENERATED_BODY()
UFUNCTION(BlueprintImplementableEvent)
void LoopFunction();
UFUNCTION(BlueprintImplementableEvent)
void LoopFinished();
void MyFunction();
}
MyActor.cpp
AMyActor::MyFunction()
{
for (int a = 0; a < 5; a++)
{
LoopFunction();
}
LoopFinished();
}
“LoopFunction” and “LoopFinished” can be created as event on blueprint.
If you want them on output nodes, you might need a custom K2_Node, but it is lack of documentation. Last time, I tried to create one, but I couldn’t figure it out.
As I said, with the exception of practically modifying the engine, that’s not possible.
As @Hexavx said, you can use BlueprintImplementableEvents, or some other event-based method.
However, I’m really not sure if having the loop in C++ and the loop body in Blueprint will have any performance benefit.
The solution of returning an Enum can only provide a way to branch out execution from the end of the function; it can’t break out execution from within the function.
Oh okay that makes sense now, mhh are there no work around this?
maybe Useing it with a BlueprintFunctionLibrary can do it, like Nachomonkey said ,using the Enum As output and using Swith to specific the output ,during the BlueprintFunctionLibrary ,add a timer call it while the fuction triggered ,and give it a loop ,add more out put as a blueprint macro, using this macro in your blueprint class,it may work, i haven’t try it ,but it is possible