Transfering blueprint code to C++?

ifthe reason you want to do this is because you want better performance, Blueprint nativization can get very good results, almost as good as writing the code in C++ in the first place, and you wont have to transfer.

But if the reason is that you want to write your project in C++ from now on for non-performance reasons then yes i suppose you will need some
guidance or help on that.

I saw this video on how to transfer blueprint code to C++ - YouTube and it almost helped me but the problem I am trying to transfer the blueprints code from my player and enemy ai to C++,
Does anyone know any tutorials or books/documents on how to do this?

hmmm while the blueprint navigator does seem nice I would still like to write it all in code. Do you know any online tutorials or books/documentations about how to write blueprints in code?

I personally think there is no such books for teaching how to transfer blueprint into code.

My tips for transferring would be :

Define logic chunks from the main uber graph. Also remove duplicated / unwanted logic first.

For each of the event, there would be a corresponding virtual function in C++. For example, BeginPlay(), Tick(), OnConstruction(). Check out the API page for the virtual functions.

You can do something similar like this

void BeginPlay()
{

    Super::BeginPlay(); 

	// Calling super means you attaching logic. 
	// You can omit super if you wants a full override. 
	// But don't do this on main framework function like Tick(). 
	// You will prevent such object receiving ticks from uppermost layer.
	 
    // All your blueprint logic should converted here.
}

All your function are can be dealt in same way. Create function in C++, with same parameter list. Then call them in code. If you wish to call them again in Blueprint, consider check out all UFUNCTION / UPROPERTY / UMETA usage.

Keep on refactoring and simplifying code. Blueprint are excellent when dealing with sequence of logic calls, but could be the worst when come to array sorting, complex math, containers accessibility, etc. I would use Blueprint for logic sequence, so I can see execution fires on simulation, and C++ to hide logic details.

For blueprint function, you can hover over them and see their C++ path. Navigate in your engine files, and find that function. You can decide to use them directly in code, or you can write a simpler one to fit your need. Visual Assist would be helpful here.

The rest is all about refactoring, or try to negate logic for cleaner/shorter implementation.

:open_mouth: wow this was very informative, this actually helped out a little. But I’m still a little lost, in the picture do you know how to convert some of this code? I think if you were to explain this I might be able to get this under control.

There no way to directly convert your blueprints (other then hard to modify nativisation) , you need to do it manually and best way to do it is just learn C++ once oyu got that it becomes natural, just because most nodes are normal simple function binds, name is generates straight from function name, just remove spaces, the “Target” shows you in which class the function is. As for static nodes (without “Target”) most of them sit in UGameplayStatics or other blueprint library classes:

But keep in mind even thru those functions are reusable in C++ most of them was made to make access to other things in C++ that can not be directly binded to Blueprint like for example UKismetMathLibrary which is just front end for FMath which is one you should use in C++. Same goes with events, events in C++ are mainly virtual function overrides something that Blueprint can not do and event dispatchers are called delegates.

You already know about C++ side of things just by knowing blueprint, they using exact same APIs, all you see in blueprint has a orgin in C++, so just learn it and you will be suppriced how you already know a lot of C++ functions

I just starting out but i making C++ tutorial stream but it’s just beginning but should get you started:

// As the matter of BeginPlay alone, a directly converted implementation looks like this :

In .h

UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AActor* Enemy;

In .cpp

void BeginPlay() 
{
    Super::BeginPlay();

    UBlackboardComponent* const pBlackboard =
                 UAIBlueprintHelperLibrary::GetBlackboard(this);

    if(!pBlackboard) return;

    Enemy = UGameplayStatics::GetPlayerController(pBlackboard->(),0);
    pBlackboard->SetValueAsObject(TEXT("Enemy"), Enemy); 
}

Then again, you would have to start thinking on: Where is my blackboard get instantiated. Spawn in the blueprint? One may think it is not pretty, so may do like this because we don’t like the BlueprintHelperLibrary here.

In .h

// Edit on main blueprints only. After spawned the instance, it is technically useless
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly)
TSubclassOf<UBlackboardComponent> BlackboardTemplate;

// The actual instance, we use the data here.
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite)
UBlackboardComponent* BlackboardInstance;

In .cpp

void BeginPlay()
{
    Super::BeginPlay();
    BlackboardInstance = 
        NewObject<UBlackboardComponent>(this, BlackboardTemplate);
 
    if(!BlackboardInstance) return;
    
    Enemy = UGameplayStatics::GetPlayerController(BlackboardInstance ->(),0);
    BlackboardInstance->SetValueAsObject(TEXT("Enemy"), Enemy); 
}

Above just examples. UE4 maybe spawn one for you. I never deal with BB, so this is just a format to follow, may contains compile error. Alternatively, find a way to grab the reference by all means.

As for the OnPerceptionUpdate…I didn’t know the owner of the delegates, so I don’t know how to access the delegate, but a partial syntax can be this:

OnPerceptionUpdated.AddUniqueDynamic(this, &AIController::CheckPlayer);

// Please make sure function parameter are same as your delegate. 
void AIController::CheckPlayer(TArray<Acctor*> UpdatedActors)
{
     const bool bFoundAPlayer = UpdatedActors.Contains(Enemy);
     // I didn't know what is the one beyond branch node. You can continue here.
}

By the way, you don’t even have to use loop in your original blueprint. Use TArray Contains() function, it is also exposed in Blueprint. Unless your for-loop object has nested properties, then consider use FindByPredicate(), ContainsByPredicate() or FilterByPredicate() to throw in a lambda. (C++ only, unless you have Extended BP Library plugin)

(I have a feel of since this thing is a component, consider using CreateDefaultSubobject as part in the constructor. Spawning using NewObject would possibly fail. To access the blackboard, should be Blackboard->GetBlackboard(). Could be much simpler than I wrote above, which maybe wrong. Component created by DSO require you to setup value in BP, and you still can access it in C++.)

:open_mouth: Thank you so much! I actually learned a lot from this.

:open_mouth: thx as well, I will definitely look into the videos