Hi! I have a blueprint function library with some common functions and I want to write the same in C++. I know for function libraries in C++ is important to mark the functions STATIC.
in my BLUEPRINT function library I have a very simple function that gets the gamemode, casts it and sends my gamemode casted like this:
And when you call this function by actors, use UMyBlueprintFunctionLibrary::myGamemode(GetWorld());
P.S. I remember trying to create this function in a function library, but I ended up just using something like AmyGamemodeCPP* GMRef = Cast<AmyGamemodeCPP>(GetWorld()->GetAuthGameMode());
directly in actors. It’s not like you need to do that often, probably once on BeginPlay() and that’s it.
That’s because blueprints basically have access to every library at any point, so it can gets World automatically in GetGameMode. GameplayStatics includes a lot of libraries itself. In c++ it’s a bit different.
If you run in game you can use the GEngine global pointer (GEngine->GetWorld())
If you run in editor you can use the GEditor global pointer to editor engine (GEditor->GetEditorWorldContext().World())
the last need #include “Editor.h” and UnrealEd as module depency.
oh ok…I use my own gamemodes and playercontrollers A LOT . mostly gamemode for general game mechanics and as a communication tool in between different actors. Thats why I use to implemente miGamemode and miPlayer in a blueprint library function… it saves me a couple nodes each time I need it.
some times as you said its just in the beginplay of actors and instead of
this gives me some errors. I included “Editor.h” and “UnrealEd.h”
unresolved external symbol "__declspec(dllimport) public: struct FWorldContext & __cdecl UEditorEngine::GetEditorWorldContext(bool)" (__imp_?GetEditorWorldContext@UEditorEngine@@QEAAAEAUFWorldContext@@_N@Z) referenced in function "public: static class AmyGamemodeCPP * __cdecl UMyBlueprintFunctionLibrary::myGamemode(void)" (?myGamemode@UMyBlueprintFunctionLibrary@@SAPEAVAmyGamemodeCPP@@XZ)
unresolved external symbol "__declspec(dllimport) class UEditorEngine * GEditor" (__imp_?GEditor@@3PEAVUEditorEngine@@EA) referenced in function "public: static class AmyGamemodeCPP * __cdecl UMyBlueprintFunctionLibrary::myGamemode(void)" (?myGamemode@UMyBlueprintFunctionLibrary@@SAPEAVAmyGamemodeCPP@@XZ)
2 unresolved externals
Microsoft.MakeFile.targets(45, 5): [MSB3073] The command ""C:\Program Files\Epic Games\UE_4.26\Engine\Build\BatchFiles\Build.bat" BlockardsEditor Win64 Development -Project="F:\00 UE\Blockards\Blockards.uproject" -WaitMutex -FromMsBuild" exited with code 6.
and an extra question (lol) you said if you use in the game or in editor…can’t use same in both? how do I do? what is in the editor will be in the game somehow
I’m sorry, please disregard my previous message. The thing is, GetEditorWorldContext() not only requires #include "Editor.h", but also an "UnrealEd" module
Open %YourProjectName%.build.cs, and in PublicDependencyModuleNames add "UnrealEd". Then it should work.