ArticyXImporter
ArticyXImporter for Unreal Engine
Loading...
Searching...
No Matches
ArticyExpressoScripts.h
1//
2// Copyright (c) 2023 articy Software GmbH & Co. KG. All rights reserved.
3//
4
5#pragma once
6
7#include "Internationalization/Regex.h"
8#include "ArticyObject.h"
9#include "ArticyDatabase.h"
10
11#include "ArticyExpressoScripts.generated.h"
12
13class UArticyString;
14class UArticyInt;
15class UArticyBool;
17struct ExpressoType;
18
25struct ARTICYRUNTIME_API ExpressoType
26{
27 union
28 {
29 bool BoolValue;
30 int64 IntValue = 0;
31 double FloatValue;
32 };
33 FString StringValue;
34
38 enum EType
39 {
40 Undefined, Bool, Int, Float, String
41 } Type = Undefined;
42
48 virtual bool& GetBool();
49
55 virtual const bool& GetBool() const;
56
62 virtual int64& GetInt();
63
69 virtual const int64& GetInt() const;
70
76 virtual double& GetFloat();
77
83 virtual const double& GetFloat() const;
84
90 virtual FString& GetString();
91
97 virtual const FString& GetString() const;
98
104 virtual FString ToString() const;
105
106
107 //---------------------------------------------------------------------------//
108
112 ExpressoType();
113
117 virtual ~ExpressoType();
118
127 ExpressoType(UArticyBaseObject* Object, const FString& Property);
128
129 // ReSharper disable CppNonExplicitConvertingConstructor
130
131 //implicit conversion from value type
132
138 ExpressoType(const bool& Value);
139
145 ExpressoType(const int64& Value);
146
152 ExpressoType(const int32& Value);
153
159 ExpressoType(const int16& Value);
160
166 ExpressoType(const int8& Value);
167
173 ExpressoType(const uint64& Value);
174
180 ExpressoType(const uint32& Value);
181
187 ExpressoType(const uint16& Value);
188
194 ExpressoType(const uint8& Value);
195
201 ExpressoType(const double& Value);
202
208 ExpressoType(const float& Value);
209
215 ExpressoType(const FString& Value);
216
222 ExpressoType(const FText& Value);
223
229 ExpressoType(const FName& Value);
230
238 ExpressoType(const UArticyPrimitive* Object);
239
245 ExpressoType(const UArticyString& Value);
246
252 ExpressoType(const UArticyInt& Value);
253
259 ExpressoType(const UArticyBool& Value);
260
266 ExpressoType(const FArticyId& Value);
267
268 //implicit conversion to value type
269
277 explicit operator bool() const;
278
286 explicit operator int64() const;
287
295 explicit operator int8() const;
296
304 explicit operator uint8() const;
305
313 explicit operator int16() const;
314
322 explicit operator uint16() const;
323
331 explicit operator int32() const;
332
340 explicit operator uint32() const;
341
349 explicit operator uint64() const;
350
358 explicit operator double() const;
359
367 explicit operator float() const;
368
376 explicit operator FString() const;
377
385 explicit operator FText() const;
386
394 explicit operator FName() const;
395
403 explicit operator FArticyId() const;
404
405 // ReSharper restore CppNonExplicitConvertingConstructor
406
407 //---------------------------------------------------------------------------//
408
416 ExpressoType operator-() const;
417
426 bool operator==(const ExpressoType& Other) const;
427
436 bool operator!=(const ExpressoType& Other) const;
437
446 bool operator<(const ExpressoType& Other) const;
447
456 bool operator>(const ExpressoType& Other) const;
457
466 bool operator<=(const ExpressoType& Other) const;
467
476 bool operator>=(const ExpressoType& Other) const;
477
486 ExpressoType operator&&(const ExpressoType& Other) const;
487
496 ExpressoType operator||(const ExpressoType& Other) const;
497
506 ExpressoType operator^(const ExpressoType& Other) const;
507
516 ExpressoType operator+(const ExpressoType& Other) const;
517
526 ExpressoType operator-(const ExpressoType& Other) const;
527
536 ExpressoType operator*(const ExpressoType& Other) const;
537
546 ExpressoType operator/(const ExpressoType& Other) const;
547
556 ExpressoType operator%(const ExpressoType& Other) const;
557
558 //NOTE: No assignment/mutation operators are defined, as those are not
559 //allowed anywhere where a ExpressoType is used!
560 /*ExpressoType& operator++();
561 ExpressoType operator++(int);
562 ExpressoType& operator--();
563 ExpressoType operator--(int);*/
564
565 //---------------------------------------------------------------------------//
566
572 struct Definition;
573
580 static TMap<FName, Definition> Definitions;
581
590 const Definition& GetDefinition(const FName& CppType) const;
591
600 template<typename T>
601 static void AddDefinition(const FName& CppType);
602
603 //---------------------------------------------------------------------------//
604
613 void SetValue(UArticyBaseObject* Object, FString Property) const;
614
625 static UArticyBaseObject* TryFeatureReroute(UArticyBaseObject* Object, FString& Property);
626};
627
634{
638 TFunction<ExpressoType(UArticyBaseObject*, FProperty*)> Factory;
639
643 TFunction<void(UArticyBaseObject*, FProperty*, const ExpressoType&)> Setter;
644};
645
654template<typename T>
655void ExpressoType::AddDefinition(const FName& CppType)
656{
657 Definition def;
658
659 def.Factory = [](UArticyBaseObject* Object, FProperty* Property)
660 {
661 if (Object && Property)
662 {
663 T* ptr = Property->ContainerPtrToValuePtr<T>(Object);
664 if (ptr)
665 return ExpressoType(*ptr);
666 }
667
668 return ExpressoType{};
669 };
670
671 def.Setter = [](UArticyBaseObject* Object, FProperty* Property, const ExpressoType& Value)
672 {
673 if (!Object || !Property)
674 return;
675
676 T* ptr = Property->ContainerPtrToValuePtr<T>(Object);
677 if (ptr)
678 (*ptr) = (T)Value;
679 };
680
681 Definitions.Add(CppType, def);
682}
683
693FORCEINLINE int operator+(const int& Lhs, const ExpressoType& Rhs)
694{
695 return Lhs + (int)Rhs;
696}
697
707FORCEINLINE int operator-(const int& Lhs, const ExpressoType& Rhs)
708{
709 return Lhs - (int)Rhs;
710}
711
721FORCEINLINE int operator*(const int& Lhs, const ExpressoType& Rhs)
722{
723 return Lhs * (int)Rhs;
724}
725
735FORCEINLINE int operator/(const int& Lhs, const ExpressoType& Rhs)
736{
737 return Lhs / (int)Rhs;
738}
739
749FORCEINLINE int operator%(const int& Lhs, const ExpressoType& Rhs)
750{
751 return Lhs % (int)Rhs;
752}
753
763FORCEINLINE float operator+(const float& Lhs, const ExpressoType& Rhs)
764{
765 return Lhs + (float)Rhs;
766}
767
777FORCEINLINE float operator-(const float& Lhs, const ExpressoType& Rhs)
778{
779 return Lhs - (float)Rhs;
780}
781
791FORCEINLINE float operator*(const float& Lhs, const ExpressoType& Rhs)
792{
793 return Lhs * (float)Rhs;
794}
795
805FORCEINLINE float operator/(const float& Lhs, const ExpressoType& Rhs)
806{
807 return Lhs / (float)Rhs;
808}
809
817UCLASS(abstract)
818class ARTICYRUNTIME_API UArticyExpressoScripts : public UObject
819{
820 GENERATED_BODY()
821
822public:
823
828 {
829 //add empty condition and instruction
830 Conditions.Add(GetTypeHash(FString{ "" }), [&] { return true; });
831 Instructions.Add(GetTypeHash(FString{ "" }), [&] { return; });
832 }
833
841 virtual UClass* GetUserMethodsProviderInterface() { return nullptr; }
842
850 virtual void Init(UArticyDatabase* DB) { OwningDatabase = DB; }
851
859 UArticyDatabase* GetDb() const { return OwningDatabase; }
860
861
869 void SetCurrentObject(UArticyPrimitive* Object) { self = Object; }
870
878 void SetSpeaker(UArticyObject* Speaker) { speaker = Speaker; }
879
891 bool Evaluate(const int& ConditionFragmentHash, UArticyGlobalVariables* GV, UObject* MethodProvider) const;
892
904 bool Execute(const int& InstructionFragmentHash, UArticyGlobalVariables* GV, UObject* MethodProvider) const;
905
914 void SetDefaultUserMethodsProvider(UObject* MethodProvider);
915
923 UObject* GetDefaultUserMethodsProvider() const;
924
932 UObject* GetUserMethodsProviderObject() const;
933
942 virtual UArticyGlobalVariables* GetGV() { return nullptr; }
943
944protected:
945
953 virtual void SetGV(UArticyGlobalVariables* GV) const { }
954
955 //========================================//
956
963
970
976 TMap<uint32, TFunction<bool()>> Conditions;
977
983 TMap<uint32, TFunction<void()>> Instructions;
984
994 UArticyObject* getObj(const FString& NameOrId, const uint32& CloneId = 0) const;
995
1005 static void setProp(UArticyBaseObject* Object, const FString& Property, const ExpressoType& Value);
1006
1016 void setProp(const ExpressoType& Id_CloneId, const FString& Property, const ExpressoType& Value) const;
1017
1027 static ExpressoType getProp(UArticyBaseObject* Object, const FString& Property);
1028
1038 ExpressoType getProp(const ExpressoType& Id_CloneId, const FString& Property) const;
1039
1049 int random(int Min, int Max);
1050
1059 int random(int Max);
1060
1070 float random(float Min, float Max);
1071
1080 float random(float Max);
1081
1091 ExpressoType random(const ExpressoType& Min, const ExpressoType& Max);
1092
1101 ExpressoType random(const ExpressoType& Max);
1102
1112 static void incrementProp(UArticyBaseObject* Object, const FString& Property, const float Value = 1);
1113
1123 void incrementProp(const ExpressoType& Id_CloneId, const FString& Property, const float Value = 1) const;
1124
1134 static void decrementProp(UArticyBaseObject* Object, const FString& Property, const float Value = 1);
1135
1145 void decrementProp(const ExpressoType& Id_CloneId, const FString& Property, const float Value = 1) const;
1146
1157 static bool isInRange(float valueToTest, float lowerBound, float upperBound);
1158
1170 static bool isPropInRange(UArticyBaseObject* Object, const FString& Property, float lowerBound, float upperBound);
1171
1183 bool isPropInRange(const ExpressoType& Id_CloneId, const FString& Property, float lowerBound, float upperBound) const;
1184
1195 static bool isInRange(const FString& valueToTest, const FString& lowerBound, const FString& upperBound);
1196
1208 static bool isPropInRange(UArticyBaseObject* Object, const FString& Property, const FString& lowerBound, const FString& upperBound);
1209
1221 bool isPropInRange(const ExpressoType& Id_CloneId, const FString& Property, const FString& lowerBound, const FString& upperBound) const;
1222
1228 void resetAllSeenCounters();
1229
1238 int getSeenCounter(UArticyBaseObject* Object = nullptr);
1239
1248 int getSeenCounter(const FString& NameOrId);
1249
1258 int setSeenCounter(const int Value = 1);
1259
1269 int setSeenCounter(UArticyBaseObject* Object, const int Value = 1);
1270
1280 int setSeenCounter(const FString& NameOrId, const int Value = 1);
1281
1290 bool fallback(UArticyBaseObject* Object);
1291
1300 bool fallback(const FString& NameOrId);
1301
1309 bool fallback();
1310
1320 template<typename ...ArgTypes>
1321 static void print(const FString& Msg, ArgTypes... Args);
1322
1332 template<typename ...ArgTypes>
1333 static void print(const ExpressoType& Msg, ArgTypes... Args)
1334 {
1335 print(Msg.ToString(), Args...);
1336 }
1337
1346 static const bool& ConditionOrTrue(const bool& Condition) { return Condition; }
1347
1356 static const bool ConditionOrTrue(const int& Condition) { return Condition > 0; }
1357
1365 static bool ConditionOrTrue(void /*JustAComment*/) { return true; }
1366
1372 mutable UObject* UserMethodsProvider = nullptr;
1373
1380 TWeakObjectPtr<UObject> DefaultUserMethodsProvider = nullptr;
1381
1382private:
1383
1389 UArticyDatabase* OwningDatabase = nullptr;
1390
1399 UArticyObject* getObjInternal(const ExpressoType& Id_CloneId) const;
1400
1408 static void PrintInternal(const FString& msg);
1409};
1410
1420template <typename ... ArgTypes>
1421void UArticyExpressoScripts::print(const FString& Msg, ArgTypes... Args)
1422{
1423 auto msg = Msg;
1424
1425 auto arr = TArray<ExpressoType>{ Args... };
1426 for (int i = 0; i < arr.Num(); ++i)
1427 msg = msg.Replace(*FString::Printf(TEXT("{%d}"), i), *FString{ arr[i] });
1428
1429 PrintInternal(msg);
1430}
Definition ArticyBaseObject.h:25
Definition ArticyGlobalVariables.h:320
Definition ArticyDatabase.h:172
The UArticyExpressoScripts class manages script conditions and instructions.
Definition ArticyExpressoScripts.h:819
static ExpressoType getProp(UArticyBaseObject *Object, const FString &Property)
Retrieves the value of a property on an Articy object.
Definition ArticyExpressoScripts.cpp:1262
bool fallback(UArticyBaseObject *Object)
Performs a fallback operation for an Articy object.
Definition ArticyExpressoScripts.cpp:1660
static void setProp(UArticyBaseObject *Object, const FString &Property, const ExpressoType &Value)
Sets the value of a property on an Articy object.
Definition ArticyExpressoScripts.cpp:1233
virtual UArticyGlobalVariables * GetGV()
Retrieves the active global variables instance.
Definition ArticyExpressoScripts.h:942
UArticyObject * getObj(const FString &NameOrId, const uint32 &CloneId=0) const
Retrieves an Articy object by name or ID.
Definition ArticyExpressoScripts.cpp:1137
static void print(const FString &Msg, ArgTypes... Args)
Prints a formatted message to the log.
Definition ArticyExpressoScripts.h:1421
int setSeenCounter(const int Value=1)
Sets the seen counter for the current object.
Definition ArticyExpressoScripts.cpp:1595
UObject * UserMethodsProvider
Cache of the current methods provider set during evaluation.
Definition ArticyExpressoScripts.h:1372
UArticyPrimitive * self
The current object for script evaluation.
Definition ArticyExpressoScripts.h:962
static void incrementProp(UArticyBaseObject *Object, const FString &Property, const float Value=1)
Increments the value of a property on an Articy object.
Definition ArticyExpressoScripts.cpp:1397
UArticyObject * speaker
The speaker for dialog fragments.
Definition ArticyExpressoScripts.h:969
static const bool & ConditionOrTrue(const bool &Condition)
Script conditions that are not empty return the condition value.
Definition ArticyExpressoScripts.h:1346
int random(int Min, int Max)
Generates a random integer between Min and Max.
Definition ArticyExpressoScripts.cpp:1290
static void print(const ExpressoType &Msg, ArgTypes... Args)
Prints a formatted message to the log.
Definition ArticyExpressoScripts.h:1333
UArticyExpressoScripts()
Default constructor for UArticyExpressoScripts.
Definition ArticyExpressoScripts.h:827
TWeakObjectPtr< UObject > DefaultUserMethodsProvider
Default methods provider for script evaluation.
Definition ArticyExpressoScripts.h:1380
void SetSpeaker(UArticyObject *Speaker)
Sets the speaker for dialog fragments.
Definition ArticyExpressoScripts.h:878
virtual void Init(UArticyDatabase *DB)
Initializes the expresso scripts with the specified database.
Definition ArticyExpressoScripts.h:850
static bool isInRange(float valueToTest, float lowerBound, float upperBound)
Checks if a value is within a range.
Definition ArticyExpressoScripts.cpp:1460
TMap< uint32, TFunction< void()> > Instructions
Map of instruction fragments.
Definition ArticyExpressoScripts.h:983
TMap< uint32, TFunction< bool()> > Conditions
Map of condition fragments.
Definition ArticyExpressoScripts.h:976
virtual void SetGV(UArticyGlobalVariables *GV) const
Sets the global variables instance for script execution.
Definition ArticyExpressoScripts.h:953
static bool isPropInRange(UArticyBaseObject *Object, const FString &Property, float lowerBound, float upperBound)
Checks if a property value is within a range.
Definition ArticyExpressoScripts.cpp:1476
int getSeenCounter(UArticyBaseObject *Object=nullptr)
Retrieves the seen counter for an Articy object.
Definition ArticyExpressoScripts.cpp:1571
static void decrementProp(UArticyBaseObject *Object, const FString &Property, const float Value=1)
Decrements the value of a property on an Articy object.
Definition ArticyExpressoScripts.cpp:1428
void SetCurrentObject(UArticyPrimitive *Object)
Sets the current object for script evaluation.
Definition ArticyExpressoScripts.h:869
void resetAllSeenCounters()
Resets all seen counters in the global variables.
Definition ArticyExpressoScripts.cpp:1554
UArticyDatabase * GetDb() const
Retrieves the Articy database.
Definition ArticyExpressoScripts.h:859
static bool ConditionOrTrue(void)
Script conditions that are empty or only contain a comment always return true.
Definition ArticyExpressoScripts.h:1365
virtual UClass * GetUserMethodsProviderInterface()
Retrieves the user methods provider interface.
Definition ArticyExpressoScripts.h:841
static const bool ConditionOrTrue(const int &Condition)
Script conditions that should evaluate to bool, but conditions evaluates to int.
Definition ArticyExpressoScripts.h:1356
Definition ArticyGlobalVariables.h:479
Definition ArticyGlobalVariables.h:168
Definition ArticyObject.h:19
Definition ArticyPrimitive.h:17
Definition ArticyGlobalVariables.h:359
The Definition struct represents a type definition for ExpressoType.
Definition ArticyExpressoScripts.h:634
TFunction< void(UArticyBaseObject *, FProperty *, const ExpressoType &)> Setter
Setter function to set a property from an ExpressoType value.
Definition ArticyExpressoScripts.h:643
TFunction< ExpressoType(UArticyBaseObject *, FProperty *)> Factory
Factory function to create an ExpressoType from a property.
Definition ArticyExpressoScripts.h:638
The ExpressoType struct represents a flexible data type used in the Articy runtime.
Definition ArticyExpressoScripts.h:26
static TMap< FName, Definition > Definitions
Map of type definitions for ExpressoType.
Definition ArticyExpressoScripts.h:580
EType
Enumeration for the type of the ExpressoType.
Definition ArticyExpressoScripts.h:39
int64 IntValue
Integer value of the ExpressoType.
Definition ArticyExpressoScripts.h:30
ExpressoType()
Default constructor for ExpressoType.
Definition ArticyExpressoScripts.cpp:12
static void AddDefinition(const FName &CppType)
Adds a type definition for ExpressoType.
Definition ArticyExpressoScripts.h:655
static UArticyBaseObject * TryFeatureReroute(UArticyBaseObject *Object, FString &Property)
Attempts to reroute a property to a feature object.
Definition ArticyExpressoScripts.cpp:802
virtual FString ToString() const
Converts the ExpressoType instance to a string representation.
Definition ArticyExpressoScripts.cpp:254
FString StringValue
String value of the ExpressoType.
Definition ArticyExpressoScripts.h:33
bool BoolValue
Boolean value of the ExpressoType.
Definition ArticyExpressoScripts.h:29
double FloatValue
Float value of the ExpressoType.
Definition ArticyExpressoScripts.h:31
const Definition & GetDefinition(const FName &CppType) const
Retrieves the definition of the specified C++ type.
Definition ArticyExpressoScripts.cpp:722
void SetValue(UArticyBaseObject *Object, FString Property) const
Sets the value of a property on an object.
Definition ArticyExpressoScripts.cpp:769
Definition ArticyBaseTypes.h:18