Last active
January 16, 2026 18:30
-
-
Save rtm223/f9b6416fd203ed25d810e600e2c854bc to your computer and use it in GitHub Desktop.
UFUNCTION(meta=(MustImplementInBlueprint))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The MIT License | |
| // Copyright (c) Richard Meredith AB | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| #include "RTMBlueprintCompiler_MustImplementFunction.h" | |
| #define LOCTEXT_NAMESPACE "RTMValidator_MustImplementFunction" | |
| bool URTMBlueprintCompiler_MustImplementFunction::CanValidateBlueprint(const UBlueprint* blueprint) const | |
| { | |
| switch(blueprint->BlueprintType) | |
| { | |
| case BPTYPE_Normal: | |
| case BPTYPE_Const: | |
| case BPTYPE_LevelScript: | |
| return true; | |
| case BPTYPE_MacroLibrary: | |
| case BPTYPE_Interface: | |
| case BPTYPE_FunctionLibrary: | |
| return false; | |
| default: | |
| checkNoEntry(); | |
| static_assert(BPTYPE_MAX == 6, "BlueprintType enum has changed size, please update switch statement to comply"); | |
| return false; | |
| } | |
| } | |
| void URTMBlueprintCompiler_MustImplementFunction::ProcessBlueprintCompiled(const FKismetCompilerContext& compilationContext, const FBlueprintCompiledData& data) | |
| { | |
| Super::ProcessBlueprintCompiled(compilationContext, data); | |
| const UBlueprint* blueprint = compilationContext.Blueprint; | |
| if(!CanValidateBlueprint(blueprint)) | |
| return; | |
| TArray<const UFunction*, TInlineAllocator<16>> foundBpFunctions; | |
| for(TFieldIterator<const UFunction> functionItr(blueprint->SkeletonGeneratedClass, EFieldIteratorFlags::IncludeSuper, EFieldIteratorFlags::ExcludeDeprecated, EFieldIteratorFlags::IncludeInterfaces); functionItr; ++functionItr) | |
| { | |
| if(*functionItr && functionItr->HasMetaData(MUST_IMPLEMENT_META_NAME)) | |
| { | |
| if(!functionItr->IsNative()) | |
| { | |
| // assume here that the iterator is going to find functions in reverse class inheritance order (children first -> BP first) | |
| // This does seem to be the case. If this proves false, will need to build two lists and then compare after a full iteration | |
| foundBpFunctions.Add(*functionItr); | |
| } | |
| else if(!foundBpFunctions.FindByPredicate([nativeFunc=*functionItr](const UFunction* function) { return function->GetFName() == nativeFunc->GetFName(); })) | |
| { | |
| if(ensureAlwaysMsgf(!functionItr->GetOuterUClass()->IsChildOf(UInterface::StaticClass()), TEXT("'%s' meta is not supported on Interface functions (found on I%s::%s()"), *MUST_IMPLEMENT_META_NAME.ToString(), *functionItr->GetOuterUClass()->GetName(), *functionItr->GetName())) | |
| { | |
| // Name comparison should be OK for UFUNCTIONs? Name clashes aren't allowed, except with Interfaces and we can't validate them | |
| // If not then it will be necessary to check each BP function by walking up functionItr->GetSuperFunction() to test we reach the same native UFunction | |
| const FText fmt = LOCTEXT("MustImplement", "Function '{0}' must be implemented in Blueprint"); | |
| const FText error = FText::Format(fmt, functionItr->GetDisplayNameText()); | |
| compilationContext.MessageLog.Error(*error.ToString()); | |
| } | |
| } | |
| } | |
| } | |
| } | |
| #undef LOCTEXT_NAMESPACE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // The MIT License | |
| // Copyright (c) Richard Meredith AB | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
| // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| #pragma once | |
| #include "CoreMinimal.h" | |
| #include "BlueprintCompilerExtension.h" | |
| #include "RTMBlueprintCompiler_MustImplementFunction.generated.h" | |
| UCLASS(NotBlueprintable, NotBlueprintType, MinimalAPI) | |
| class URTMBlueprintCompiler_MustImplementFunction : public UBlueprintCompilerExtension | |
| { | |
| GENERATED_BODY() | |
| protected: | |
| bool CanValidateBlueprint(const UBlueprint* blueprint) const; | |
| virtual void ProcessBlueprintCompiled(const FKismetCompilerContext& compilationContext, const FBlueprintCompiledData& data) override; | |
| private: | |
| const FName MUST_IMPLEMENT_META_NAME = TEXT("MustImplementInBlueprint"); | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds a UFUNCTION meta that will enforce child Blueprints need to implement the specified function. Blueprint will not compile until the implementation is present
Usage:
FBlueprintCompilationManager::RegisterCompilerExtension(UBlueprint::StaticClass(), NewObject<URTMBlueprintCompiler_MustImplementFunction>());Error Reporting:
Notes: