22
33
44#include " DialogueRunner.h"
5+
56#include " Line.h"
67#include " Option.h"
78#include " YarnSubsystem.h"
8- #include " YarnSpinner.h"
9- #include " Kismet/KismetInternationalizationLibrary.h"
109#include " Misc/YSLogging.h"
1110
1211THIRD_PARTY_INCLUDES_START
@@ -47,45 +46,45 @@ void ADialogueRunner::PreInitializeComponents()
4746 }
4847
4948 // Create the Library
50- Library = TUniquePtr <Yarn::Library>(new Yarn::Library (* this ) );
49+ Library = MakeUnique <Yarn::Library>();
5150
5251 // Create the VirtualMachine, supplying it with the loaded Program and
5352 // configuring it to use our library, plus use this ADialogueRunner as the
5453 // logger and the variable storage
55- VirtualMachine = TUniquePtr <Yarn::VirtualMachine>(new Yarn::VirtualMachine ( Program, *(Library), *this , * this ) );
54+ VirtualMachine = MakeUnique <Yarn::VirtualMachine>(Program, *(Library), *this );
5655
57- VirtualMachine->LineHandler = [this ](Yarn::Line& Line)
56+ VirtualMachine->OnLine . AddLambda ( [this ](const Yarn::Line& Line)
5857 {
59- UE_LOG (LogYarnSpinner, Log, TEXT ( " Received line %s" ), UTF8_TO_TCHAR ( Line.LineID . c_str ()) );
58+ YS_LOG ( " Received line %s" , * Line.LineID );
6059
6160 // Get the Yarn line struct, and make a ULine out of it to use
6261 ULine* LineObject = NewObject<ULine>(this );
63- LineObject->LineID = FName (Line.LineID . c_str () );
62+ LineObject->LineID = FName (* Line.LineID );
6463
6564 GetDisplayTextForLine (LineObject, Line);
6665
6766 const TArray<TSoftObjectPtr<UObject>> LineAssets = YarnProject->GetLineAssets (LineObject->LineID );
6867 YS_LOG_FUNC (" Got %d line assets for line '%s'" , LineAssets.Num (), *LineObject->LineID .ToString ())
6968
7069 OnRunLine (LineObject, LineAssets);
71- };
70+ }) ;
7271
73- VirtualMachine->OptionsHandler = [this ](Yarn::OptionSet& OptionSet)
72+ VirtualMachine->OnOptions . AddLambda ( [this ](const Yarn::OptionSet& OptionSet)
7473 {
75- UE_LOG (LogYarnSpinner, Log, TEXT ( " Received %i options" ) , OptionSet.Options .size ());
74+ YS_LOG ( " Received %llu options" , OptionSet.Options .Num ());
7675
7776 // Build a TArray for every option in this OptionSet
7877 TArray<UOption*> Options;
7978
80- for (auto Option : OptionSet.Options )
79+ for (const Yarn::Option& Option : OptionSet.Options )
8180 {
82- UE_LOG (LogYarnSpinner, Log, TEXT ( " - %i: %s" ) , Option.ID , UTF8_TO_TCHAR ( Option.Line .LineID . c_str ()) );
81+ YS_LOG ( " - %i: %s" , Option.ID , * Option.Line .LineID );
8382
8483 UOption* Opt = NewObject<UOption>(this );
8584 Opt->OptionID = Option.ID ;
8685
8786 Opt->Line = NewObject<ULine>(Opt);
88- Opt->Line ->LineID = FName (Option.Line .LineID . c_str () );
87+ Opt->Line ->LineID = FName (* Option.Line .LineID );
8988
9089 GetDisplayTextForLine (Opt->Line , Option.Line );
9190
@@ -97,47 +96,46 @@ void ADialogueRunner::PreInitializeComponents()
9796 }
9897
9998 OnRunOptions (Options);
100- };
99+ }) ;
101100
102- VirtualMachine->DoesFunctionExist = [this ](const std::string & FunctionName) -> bool
101+ VirtualMachine->OnCheckFunctionExist . BindLambda ( [this ](const FString & FunctionName) -> bool
103102 {
104- return YarnSubsystem ()->GetYarnLibraryRegistry ()->HasFunction (FName (UTF8_TO_TCHAR ( FunctionName. c_str ()) ));
105- };
103+ return YarnSubsystem ()->GetYarnLibraryRegistry ()->HasFunction (FName (* FunctionName));
104+ }) ;
106105
107- VirtualMachine->GetExpectedFunctionParamCount = [this ](const std::string & FunctionName) -> int
106+ VirtualMachine->OnGetFunctionParamNum . BindLambda ( [this ](const FString & FunctionName) -> int
108107 {
109- return YarnSubsystem ()->GetYarnLibraryRegistry ()->GetExpectedFunctionParamCount (FName (UTF8_TO_TCHAR ( FunctionName. c_str ()) ));
110- };
108+ return YarnSubsystem ()->GetYarnLibraryRegistry ()->GetExpectedFunctionParamCount (FName (* FunctionName));
109+ }) ;
111110
112- VirtualMachine->CallFunction = [this ](const std::string & FunctionName, const std::vector <Yarn::FValue>& Parameters) -> Yarn::FValue
111+ VirtualMachine->OnCallFunction . BindLambda ( [this ](const FString & FunctionName, const TArray <Yarn::FValue>& Parameters) -> Yarn::FValue
113112 {
114113 return YarnSubsystem ()->GetYarnLibraryRegistry ()->CallFunction (
115- FName (UTF8_TO_TCHAR ( FunctionName. c_str ()) ),
116- TArray<Yarn::FValue>( Parameters. data (), Parameters. size ())
114+ FName (* FunctionName),
115+ Parameters
117116 );
118- };
117+ }) ;
119118
120- VirtualMachine->CommandHandler = [this ](Yarn::Command& Command)
119+ VirtualMachine->OnCommand . AddLambda ( [this ](const Yarn::Command& Command)
121120 {
122- UE_LOG (LogYarnSpinner, Log, TEXT ( " Received command \" %s\" " ), UTF8_TO_TCHAR ( Command.Text . c_str ()) );
121+ YS_LOG ( " Received command \" %s\" " , * Command.Text );
123122
124- FString CommandText = FString ( UTF8_TO_TCHAR ( Command.Text . c_str ())) ;
123+ const FString& CommandText = Command.Text ;
125124
126125 TArray<FString> CommandElements;
127126 CommandText.ParseIntoArray (CommandElements, TEXT (" " ));
128127
129128 if (CommandElements.Num () == 0 )
130129 {
131- TArray<FString> EmptyParameters;
132130 UE_LOG (LogYarnSpinner, Error, TEXT (" Command received, but was unable to parse it." ));
133- OnRunCommand (FString (" (unknown)" ), EmptyParameters );
131+ OnRunCommand (FString (" (unknown)" ), TArray<FString>() );
134132 return ;
135133 }
136134
137- FName CommandName = FName (CommandElements[0 ]);
135+ const FName CommandName = FName (CommandElements[0 ]);
138136 CommandElements.RemoveAt (0 );
139137
140- auto Lib = YarnSubsystem ()->GetYarnLibraryRegistry ();
138+ const UYarnLibraryRegistry* const Lib = YarnSubsystem ()->GetYarnLibraryRegistry ();
141139
142140 if (Lib->HasCommand (CommandName))
143141 {
@@ -150,23 +148,23 @@ void ADialogueRunner::PreInitializeComponents()
150148
151149 // Haven't handled the function yet, so call the DialogueRunner's handler
152150 OnRunCommand (CommandName.ToString (), CommandElements);
153- };
151+ }) ;
154152
155- VirtualMachine->NodeStartHandler = [this ](std::string NodeName)
153+ VirtualMachine->OnNodeStart . AddLambda ( [this ](const FString& NodeName)
156154 {
157- UE_LOG (LogYarnSpinner, Log, TEXT (" Received node start \" %s\" " ), UTF8_TO_TCHAR ( NodeName. c_str ()) );
158- };
155+ UE_LOG (LogYarnSpinner, Log, TEXT (" Received node start \" %s\" " ), * NodeName);
156+ }) ;
159157
160- VirtualMachine->NodeCompleteHandler = [this ](std::string NodeName)
158+ VirtualMachine->OnNodeComplete . AddLambda ( [this ](const FString& NodeName)
161159 {
162- UE_LOG (LogYarnSpinner, Log, TEXT (" Received node complete \" %s\" " ), UTF8_TO_TCHAR ( NodeName. c_str ()) );
163- };
160+ UE_LOG (LogYarnSpinner, Log, TEXT (" Received node complete \" %s\" " ), * NodeName);
161+ }) ;
164162
165- VirtualMachine->DialogueCompleteHandler = [this ]()
163+ VirtualMachine->OnDialogueComplete . AddLambda ( [this ]()
166164 {
167165 UE_LOG (LogYarnSpinner, Log, TEXT (" Received dialogue complete" ));
168166 OnDialogueEnded ();
169- };
167+ }) ;
170168}
171169
172170
@@ -223,7 +221,7 @@ void ADialogueRunner::StartDialogue(FName NodeName)
223221 return ;
224222 }
225223
226- bool bNodeSelected = VirtualMachine->SetNode (TCHAR_TO_UTF8 (* NodeName.ToString () ));
224+ bool bNodeSelected = VirtualMachine->SetNode (NodeName.ToString ());
227225
228226 if (bNodeSelected)
229227 {
@@ -289,64 +287,44 @@ void ADialogueRunner::SelectOption(UOption* Option)
289287 }
290288}
291289
292-
293- void ADialogueRunner::Log (std::string Message, Type Severity)
294- {
295- FString MessageText = FString (UTF8_TO_TCHAR (Message.c_str ()));
296-
297- switch (Severity)
298- {
299- case Type::INFO:
300- YS_LOG (" YarnSpinner: %s" , *MessageText);
301- break ;
302- case Type::WARNING:
303- YS_WARN (" YarnSpinner: %s" , *MessageText);
304- break ;
305- case Type::ERROR:
306- YS_ERR (" YarnSpinner: %s" , *MessageText);
307- break ;
308- }
309- }
310-
311-
312- void ADialogueRunner::SetValue (std::string Name, bool bValue)
290+ void ADialogueRunner::SetValue (const FString& Name, bool bValue)
313291{
314- YS_LOG (" Setting variable %s to bool %i" , UTF8_TO_TCHAR ( Name. c_str ()) , bValue)
292+ YS_LOG (" Setting variable %s to bool %i" , * Name, bValue);
315293 YarnSubsystem ()->SetValue (Name, bValue);
316294}
317295
318296
319- void ADialogueRunner::SetValue (std::string Name, float Value)
297+ void ADialogueRunner::SetValue (const FString& Name, float Value)
320298{
321- YS_LOG (" Setting variable %s to float %f" , UTF8_TO_TCHAR ( Name. c_str ()) , Value)
299+ YS_LOG (" Setting variable %s to float %f" , * Name, Value);
322300 YarnSubsystem ()->SetValue (Name, Value);
323301}
324302
325303
326- void ADialogueRunner::SetValue (std::string Name, std::string Value)
304+ void ADialogueRunner::SetValue (const FString& Name, const FString& Value)
327305{
328- YS_LOG (" Setting variable %s to string %s" , UTF8_TO_TCHAR ( Name. c_str ()), UTF8_TO_TCHAR ( Value. c_str ()))
306+ YS_LOG (" Setting variable %s to string %s" , * Name, * Value);
329307 YarnSubsystem ()->SetValue (Name, Value);
330308}
331309
332310
333- bool ADialogueRunner::HasValue (std::string Name)
311+ bool ADialogueRunner::HasValue (const FString& Name)
334312{
335313 return YarnSubsystem ()->HasValue (Name);
336314}
337315
338316
339- Yarn::FValue ADialogueRunner::GetValue (std::string Name)
317+ Yarn::FValue ADialogueRunner::GetValue (const FString& Name)
340318{
341319 Yarn::FValue Value = YarnSubsystem ()->GetValue (Name);
342- YS_LOG (" Retrieving variable %s with value %s" , UTF8_TO_TCHAR ( Name. c_str ()) , *Value.ConvertToString ());
320+ YS_LOG (" Retrieving variable %s with value %s" , * Name, *Value.ConvertToString ());
343321 return Value;
344322}
345323
346324
347- void ADialogueRunner::ClearValue (std::string Name)
325+ void ADialogueRunner::ClearValue (const FString& Name)
348326{
349- YS_LOG (" Clearing variable %s" , UTF8_TO_TCHAR ( Name. c_str ()))
327+ YS_LOG (" Clearing variable %s" , * Name);
350328 YarnSubsystem ()->ClearValue (Name);
351329}
352330
@@ -364,7 +342,7 @@ UYarnSubsystem* ADialogueRunner::YarnSubsystem() const
364342
365343void ADialogueRunner::GetDisplayTextForLine (ULine* Line, const Yarn::Line& YarnLine)
366344{
367- const FName LineID = FName (YarnLine.LineID . c_str () );
345+ const FName LineID = FName (* YarnLine.LineID );
368346
369347 // This assumes that we only ever care about lines that actually exist in .yarn files (rather than allowing extra lines in .csv files)
370348 if (!YarnProject || !YarnProject->Lines .Contains (LineID))
@@ -379,9 +357,9 @@ void ADialogueRunner::GetDisplayTextForLine(ULine* Line, const Yarn::Line& YarnL
379357
380358 // Apply substitutions
381359 FFormatOrderedArguments FormatArgs;
382- for (auto Substitution : YarnLine.Substitutions )
360+ for (const FString& Substitution : YarnLine.Substitutions )
383361 {
384- FormatArgs.Emplace (FText::FromString (UTF8_TO_TCHAR ( Substitution. c_str ()) ));
362+ FormatArgs.Emplace (FText::FromString (Substitution));
385363 }
386364
387365 const FText TextWithSubstitutions = (LocalisedDisplayText.IsEmptyOrWhitespace ()) ? FText::Format (NonLocalisedDisplayText, FormatArgs) : FText::Format (LocalisedDisplayText, FormatArgs);
0 commit comments