-
Notifications
You must be signed in to change notification settings - Fork 86
Using with C++
Daniel Butum edited this page Oct 22, 2020
·
12 revisions
[[TOC]]
- Check
DlgDialogueParticipant.h
. Your characters and NPC-s have to implement it. - Check
DlgManager.h
. You will be able to start a dialogue using the static functions there. - Check
DlgContext.h
. You will use a context instance to control the dialogue. - Check
DlgMemory.h
. You might need to serialize the HistoryMap member variable.
In your PROJECT.Build.cs
file add DlgSystem
to PrivateDependencyModuleNames
.
PrivateDependencyModuleNames.AddRange(new string[] { "DlgSystem" });
This will be used for the Dialogue Values based Conditions and Events.
If you don't want to use the Class based (reflection) variables or the custom events/conditions.
USTRUCT(BlueprintType, Blueprintable)
struct FDialogueDataValues
{
GENERATED_USTRUCT_BODY()
public:
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
TMap<FName, int32> Integers;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
TMap<FName, int32> Floats;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
TMap<FName, FName> Names;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
TSet<FName> TrueBools;
};
// This file contains the FDialogueDataValues struct we created above
#include "ExampleDialogueData.h"
// For IDlgDialogueParticipant
#include "DlgDialogueParticipant.h"
// For UDlgContext
#include "DlgContext"
class AExampleCharacter : public ACharacter, public IDlgDialogueParticipant
// Name of this participant
// Used for GetParticipantName
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Dialogue)
FName DialogueParticipantName = FName("ExampleParticipantName");
// UI name of this participant, what is displayed inside the UI
// Used for GetParticipantDisplayName
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Dialogue)
FText DialogueParticipantDisplayName = NSLOCTEXT("ExampleNamespace", "ExampleCharacterName", "ExampleParticipantName");
// Used for GetParticipantIcon
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Dialogue)
UTexture2D* DialogueParticipantIcon;
// Context used to control the Dialogue follow
UPROPERTY(BlueprintReadWrite, Category = Dialogue)
UDlgContext* DialogueContext = nullptr;
// Struct used to get/set the Dialogue Values
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category = Dialogue)
FDialogueDataValues DialogueDataStruct;
.h file
FName GetParticipantName_Implementation() const override { return DialogueParticipantName; }
FText GetParticipantDisplayName_Implementation(FName ActiveSpeaker) const override
{
return DialogueParticipantDisplayName;
}
UTexture2D* GetParticipantIcon_Implementation(FName ActiveSpeaker, FName ActiveSpeakerState) const override
{
return DialogueParticipantIcon;
}
ETextGender GetParticipantGender_Implementation() const override { return ETextGender::Neuter; }
bool ModifyIntValue_Implementation(FName ValueName, bool bDelta, int32 Value) override;
bool ModifyFloatValue_Implementation(FName ValueName, bool bDelta, float Value) override;
bool ModifyBoolValue_Implementation(FName ValueName, bool bValue) override;
bool ModifyNameValue_Implementation(FName ValueName, FName NameValue) override;
float GetFloatValue_Implementation(FName ValueName) const override;
int32 GetIntValue_Implementation(FName ValueName) const override;
bool GetBoolValue_Implementation(FName ValueName) const override;
FName GetNameValue_Implementation(FName ValueName) const override;
bool OnDialogueEvent_Implementation(FName EventName) override;
bool CheckCondition_Implementation(FName ConditionName) const override;
.cpp file
bool AExampleCharacter::ModifyIntValue_Implementation(FName ValueName, bool bDelta, int32 Value)
{
if (!DialogueDataStruct.Integers.Contains(ValueName))
DialogueDataStruct.Integers.Add(ValueName, 0);
if (bDelta)
DialogueDataStruct.Integers[ValueName] += Value;
else
DialogueDataStruct.Integers[ValueName] = Value;
return true;
}
bool AExampleCharacter::ModifyFloatValue_Implementation(FName ValueName, bool bDelta, float Value)
{
if (!DialogueDataStruct.Floats.Contains(ValueName))
DialogueDataStruct.Floats.Add(ValueName, 0.0f);
if (bDelta)
DialogueDataStruct.Floats[ValueName] += Value;
else
DialogueDataStruct.Floats[ValueName] = Value;
return true;
}
bool AExampleCharacter::ModifyBoolValue_Implementation(FName ValueName, bool bValue)
{
if (bValue)
DialogueDataStruct.TrueBools.Add(ValueName);
else
DialogueDataStruct.TrueBools.Remove(ValueName);
return true;
}
bool AExampleCharacter::ModifyNameValue_Implementation(FName ValueName, FName NameValue)
{
if (DialogueDataStruct.Names.Contains(ValueName))
DialogueDataStruct.Names[ValueName] = NameValue;
else
DialogueDataStruct.Names.Add(ValueName, NameValue);
return true;
}
float AExampleCharacter::GetFloatValue_Implementation(FName ValueName) const
{
return DialogueDataStruct.Floats.Contains(ValueName) ? DialogueDataStruct.Floats[ValueName] : 0.0f;
}
int32 AExampleCharacter::GetIntValue_Implementation(FName ValueName) const
{
return DialogueDataStruct.Integers.Contains(ValueName) ? DialogueDataStruct.Integers[ValueName] : 0;
}
bool AExampleCharacter::GetBoolValue_Implementation(FName ValueName) const
{
return DialogueDataStruct.TrueBools.Contains(ValueName);
}
FName ADlgExampleCharacter::GetNameValue_Implementation(FName ValueName) const
{
return DialogueDataStruct.Names.Contains(ValueName) ? DialogueDataStruct.Names[ValueName] : NAME_None;
}
bool AExampleCharacter::OnDialogueEvent_Implementation(FName EventName)
{
// Used for Dialogue based events
return false;
}
bool AExampleCharacter::CheckCondition_Implementation(FName ConditionName) const
{
// Used for Dialogue based conditions
return false;
}
.h file
UFUNCTION(BlueprintCallable, Category = Dialogue)
bool StartDialogue(UDlgDialogue* Dialogue, const TArray<UObject*>& Participants);
UFUNCTION(BlueprintCallable, Category = Dialogue)
bool SelectDialogueOption(int32 Index);
// Context used to control the Dialogue follow
UPROPERTY(BlueprintReadWrite, Category = Dialogue)
UDlgContext* DialogueContext = nullptr;
.cpp file
bool AExampleCharacter::StartDialogue(UDlgDialogue* Dialogue, const TArray<UObject*>& Participants)
{
DialogueContext = UDlgManager::StartDialogue(Dialogue, Participants);
return DialogueContext != nullptr;
}
bool AExampleCharacter::SelectDialogueOption(int32 Index)
{
if (!DialogueContext || !DialogueContext->IsValidOptionIndex(Index))
{
return false;
}
if (!ActiveContext->ChooseOption(Index) || ActiveContext->HasDialogueEnded())
{
// Dialogue Has Ended
ActiveContext = nullptr;
return false
}
return true;
}