I am trying to create a parser that can call functions. I would like to store specific functions that can then be iterated through and call.
To store my function, I created this class, however, I crash when I tried to add the function to my class:
DECLARE_DELEGATE_RetVal(FString, FFunction);
UCLASS()
class UParseFunction:public UObject
{
GENERATED_BODY()
public:
UParseFunction(){};
UPROPERTY()
TArray<FName> PossibleStringName;
//UPROPERTY()
FFunction FunctionToCall;
};
Actor constructor:
See edit
Also open to idea if you have other technics to call functions by text.
EDIT:
After more testing, I had to make some changes but still get an error. I moved the construction in begin play since after reading the doc, during construction time the pointer to the function does not exist supposedly. Also, since i’m no longer in the constructor, I replaced the CreateDefaultSubObject() with NewObject()
Tester.cpp
void AParserTester::BeginPlay()
{
Super::BeginPlay();
auto obj = NewObject<UParseFunction>(this);
if (obj)
{
UE_LOG(LogTemp,Warning,TEXT("[ParserTester] OBJ existe"))
obj->PossibleStringName = { "Test", "test", "Test()" };
obj->FunctionToCall.BindSP(this, &AParserTester::Test);//This line make me crash
Functions.Add(obj);
}
}
Tester.h:
UCLASS()
class THAGAME_API AParserTester : public AActor , public IParserObject, public TSharedFromThis<AParserTester>
{
GENERATED_BODY()
public:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
UFUNCTION()
FString Test();
}
And now i get this error:
Assertion failed: SharedThis.Get() == this [File:D:\Unreal\Engine\UE_5.0EA\Engine\Source\Runtime\Core\Public\Templates/SharedPointer.h] [Line: 1329]
UnrealEditor_ThaGame_0001!TSharedFromThis::AsShared() [D:\Unreal\Engine\UE_5.0EA\Engine\Source\Runtime\Core\Public\Templates\SharedPointer.h:1329]
UnrealEditor_ThaGame_0001!TDelegate::CreateSP() [D:\Unreal\Engine\UE_5.0EA\Engine\Source\Runtime\Core\Public\Delegates\DelegateSignatureImpl.inl:228]
UnrealEditor_ThaGame_0001!AParserTester::BeginPlay() [D:\Unreal\Project\UE5\ThaGame\Source\ThaGame\Parser\ParserTester.cpp:30]
UnrealEditor_Engine