This isn’t exactly following the video, but I wanted to try to use a template function to do the “find and assign” operations which are already repetitive with 2 components.
Following the example here:
https://wiki.unrealengine.com/Templates_in_C%2B%2B
…I defined the function inline in my .h file. But I haven’t been able to get any variation of it to compile.
Anyone who understands template functions in C++/Unreal can take a look and let me know how to fix?
OS: OSX 10.13.3
XCode: 9.3
Unreal: 4.19.1
This is my Grabber.h file that contains the template function:
This is the log of errors I see:
udemy_unreal_template_function_wont_compile_error_log
Candidate modules for hot reload:
BuildingEscape
Launching UnrealBuildTool... [/Users/Shared/Epic Games/UE_4.19/Engine/Binaries/DotNET/UnrealBuildTool.exe BuildingEscape -ModuleWithSuffix=BuildingEscape,7250 Mac Development -editorrecompile -canskiplink "/Users/kirschner/projects/udemy_unreal/BuildingEscape/BuildingEscape.uproject" ]
Warning: Starting HotReload took 0.0s.
CompilerResultsLog: New page: Compilation - Apr 29, 2018, 3:29:57 AM
CompilerResultsLog: Running Mono...
CompilerResultsLog: Setting up Mono
CompilerResultsLog: /Users/Shared/Epic Games/UE_4.19/Engine /Users/Shared/Epic Games/UE_4.19/Engine/Binaries/Mac
CompilerResultsLog: Using 'git status' to determine working set for adaptive non-unity build.
CompilerResultsLog: WARNING: Failed to read makefile: Object reference not set to an instance of an object
This file has been truncated. show original
EXPECTATION
To compile this function and then be able to call it from Grabber.cpp to FindAndAssign PhysicsHandle and Input components
OBSERVED
Template function fails to compile with errors in gist above
WHAT I’VE TRIED
put the template function in .h or .cpp files
completely stub out the template function, leaving just the signature w empty body
add/remove ‘static’ and ‘FORCEINLINE’ in all combinations
compile a minimal template function, e.g. template FORCEINLINE void Foo(T p) {}
OK. The problem was my bad syntax. In case anyone’s interested, this is the correct syntax
template <typename T>
FORCEINLINE bool FindAndAssign(T*& AssignToProperty, FString CompName)
{
T* Comp = GetOwner()->FindComponentByClass<T>();
if(Comp) {
UE_LOG(LogTemp, Warning, TEXT("Found and assigned required component %s"), *CompName);
AssignToProperty = Comp;
return true;
}
UE_LOG(LogTemp, Error, TEXT("Missing required component %s"), *CompName);
return false;
}