Skip to content

Instantly share code, notes, and snippets.

@timoxley
Last active November 7, 2024 07:02
Show Gist options
  • Save timoxley/36a5e86662707d1b1acaa13958f4d4fd to your computer and use it in GitHub Desktop.
Save timoxley/36a5e86662707d1b1acaa13958f4d4fd to your computer and use it in GitHub Desktop.
Quick GetValidComponentByClass for Blueprint, with exec pins based on validity
UCLASS()
class MY_API UMyUtils : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
UFUNCTION(
BlueprintCallable,
meta = (
ComponentClass = "/Script/Engine.ActorComponent",
DeterminesOutputType = "ComponentClass",
ExpandBoolAsExecs = "IsComponentValid"
)
)
static UActorComponent* GetValidComponentByClass(
AActor* Actor,
TSubclassOf<UActorComponent> const ComponentClass,
bool& IsComponentValid
)
{
IsComponentValid = false;
if (!IsValid(Actor)) { return nullptr; }
if (const auto Component = Actor->FindComponentByClass(ComponentClass))
{
IsComponentValid = true;
return Component;
}
return nullptr;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment