Complete Quest

Let’s replace the CompleteQuest BP function with one in C++.

Take a look at the node and details:

../../_images/bp_quest_complete_details.PNG

We notice that.

  1. This is a Public function

  2. It is not Pure

  3. The Inputs are:

  1. QuestId of type Name and not passed by reference.

  2. CompleteWholeQuest of type Boolean and not passed by reference.

  1. There are no output

  2. There is a pi going in named Index

Create the CompleteQuest C++ Function

In QuestManager.h add

UFUNCTION(BlueprintCallable, BlueprintNativeEvent)
void CompleteQuest(FName QuestId, bool CompleteWholeQuest);

In QuestManager.cpp add

void AQuestManager::CompleteQuest_Implementation(FName QuestId, bool CompleteWholeQuest)
{

}

Overriding CompleteQuest

  1. Grab all but the input node from CompleteQuest BP, Ctrl-X to cut.

  2. Delete CompleteQuest

  3. Override the function CompleteQuest.

  4. Paste the graph.

Noteice that we see Event Complete Quest. So it appears as an event. This is the case if the function return is void.

  1. The function is void

  2. Is not const

  1. Right click on Event Complete Quest and select Add call to parent function

../../_images/add_call_to_parent_function.PNG

Overriding GetQuestIndex

  1. Notice GetQuestIndex that is only available in Blueprint.

../../_images/get_quest_index.PNG

That would need to be migrated.

Also notice that the Output is Index which would not match C++.

../../_images/get_quest_index_return_value.PNG

Rename that to Return Value which is a special name in Blueprint would fix that.

../../_images/get_request_index_outputs.PNG
  1. Add the below to QuestManager.h

UFUNCTION(BlueprintPure, BlueprintImplementableEvent)
int32 GetQuestIndex(FName QuestId);
  1. Ctrl-X cut the content of the GetQuestIndex, delete and override with same name. Hook up the wires correctly

4: Fix the below error by Right-Click Refresh Node and reconnect.

Implementing CompleteQuest

  1. Add code to QuestManager.cpp

void AQuestManager::CompleteQuest_Implementation(FName QuestId, bool CompleteWholeQuest)
{
    int32 QuestIndex = GetQuestIndex(QuestId);
    FQuestInfo Quest = QuestList[QuestIndex];
    if (CompleteWholeQuest) {
        QuestList[QuestIndex].Progress = Quest.ProgressTotal;
    }
    else {
        QuestList[QuestIndex].Progress = FMath::Min(Quest.Progress + 1, Quest.ProgressTotal);
    }
}
  1. All nodes pasted from before was used only for reference to replicate as code above. Now delete them.

  2. connect input to calling parent to output along with GetQuestIndex and we have replaced the BP function in C++.