Skip to content

Instantly share code, notes, and snippets.

@intaxwashere
Last active March 20, 2022 22:04
Show Gist options
  • Save intaxwashere/0effb65108766b1e2c35a39ab158d977 to your computer and use it in GitHub Desktop.
Save intaxwashere/0effb65108766b1e2c35a39ab158d977 to your computer and use it in GitHub Desktop.
Creating an instanced object to call UFUNCTIONs
void UFunctionCallerObject::Update()
{
if (CanUpdate() && IsValid(Function) && GetOuter())
{
GetOuter()->ProcessEvent(Function, nullptr);
}
}
#if WITH_EDITOR
void UFunctionCallerObject::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
{
UObject::PostEditChangeProperty(PropertyChangedEvent);
if (PropertyChangedEvent.GetPropertyName() == GET_MEMBER_NAME_CHECKED(UFunctionCallerObject, FunctionNameToCall))
{
Function = GetOuter() && GetOuter()->GetClass() ? GetOuter()->GetClass()->FindFunctionByName(FunctionNameToCall) : nullptr;
}
}
#endif
TArray<FName> UFunctionCallerObject::GetFunctionToCallOptions() const
{
if (GetOuter() && GetOuter()->GetClass())
{
const UObject* Outer = GetOuter();
// helper lambda to remove invalid functions with specified flags.
auto RemoveInvalidFuncs = [&](TArray<FName> InFunctionList)
{
for (int32 i = 0; i < InFunctionList.Num(); ++i)
{
const FName FuncName = InFunctionList[i];
const UFunction* Func = Outer->FindFunctionChecked(FuncName);
if (Func)
{
const bool bValid = !Func->HasAnyFunctionFlags(FUNC_BlueprintPure | FUNC_Delegate | FUNC_EditorOnly
| FUNC_UbergraphFunction | FUNC_Delegate | FUNC_HasOutParms);
if (bValid && Func->GetReturnProperty() != nullptr)
{
InFunctionList.RemoveAt(i);
}
}
}
};
TArray<FName> FunctionList;
Outer->GetClass()->GenerateFunctionList(FunctionList);
RemoveInvalidFuncs(FunctionList);
bool bReachedFinalClass = false;
const UClass* LastSuperClass = Outer->GetClass()->GetSuperClass();
while (!bReachedFinalClass)
{
if (LastSuperClass && LastSuperClass != AActor::StaticClass())
{
TArray<FName> SuperFunctionList;
LastSuperClass->GenerateFunctionList(SuperFunctionList);
RemoveInvalidFuncs(SuperFunctionList);
FunctionList.Append(SuperFunctionList);
LastSuperClass = LastSuperClass->GetSuperClass();
}
else
{
bReachedFinalClass = true;
}
}
return FunctionList;
}
return TArray<FName>{};
}
UCLASS(EditInlineNew, BlueprintType, NotBlueprintable)
class UFunctionCallerObject final : public UObject
{
GENERATED_BODY()
public:
void Update();
protected:
bool CanUpdate()
{
// write your own logic here
return true;
}
#if WITH_EDITOR
virtual void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) override;
#endif
private:
UPROPERTY(EditAnywhere, Category="Data", DisplayName="Function" ,meta=(GetOptions="GetFunctionToCallOptions"))
FName FunctionNameToCall;
UPROPERTY()
UFunction* Function;
UFUNCTION(CallInEditor)
TArray<FName> GetFunctionToCallOptions() const;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment