How to connect global functions to world

Hi,
I am just getting my little toe a little bit wet with C++ and am really just minutes in it but have other programming language experience, so that helps a little. I just want to primarily use Blue Prints, but to make things easier, where I can, I want to make use of C++ functions and libs, and that is how I got here. This is just all experimenting and my questions are probably very easily answered by someone who knows what he/she is doing ;).

I followed this page “How to Make Global Functions and Structs? - Programming & Scripting - Unreal Engine Forums” and it works great and I get it. It makes blue printing really powerful when I can extend it this way.
The only question I have: The construction in the link above is for a global function lib. Can (and how can) I still get information from the engine for things like a log level that I created in the Game State Class.

For example: I want to make my own Debug Printing system, that prints to screen based on a logLevel that is stored in the GameState (not Game Mode, because that does not exist on clients).
Maybe this already exists but this is just for the learning experience.
I can use GEngine->AddOnScreenDebugMessage for the printing. That works great.
But how do I get to the game state? I realize when I inherit from Actor, I might get the world and then get it somehow, but I inherited from nothing because it is a global lib and do not want to place this actor in the world.
How do I get to the GameState? How do I make the initial connection with the game?

Unreal already supports custom Log categories.

As for accessing the GameState instance you can try:

// Get world
UWorld* world = GetWorld();
 
if(world)
{
		AGameGameState* gameState = world->GetGameState<AGameGameState>();
		
		// Do stuff
}

Thanks for the link about logging. I have found it also and I am already using parts of it, but like I said, this is more of an example to do some learning.

On that note: If I add your code to it, I get the following error:
UObject: GetWorld : illegal call of non-static member function
And the tooltip text says: Error : nonstatic member reference must be relative to a specific object.

Which I think means: You must have a reference to something in the world… which is exactly what I am trying to do.
Just to be sure I made the function static, which resulted in error messages: ‘static’ should not be used on member functions defined at file scope.

So I am still in the same boat: I need to connect to the GameState BluePrint which has my log level… how do I do that?
(Currently I have a local variable has the log level and that works fine, but again: “trying to learn something”)

Ah ok.

Have you tried GetWorld() from GEngine?

i.e.

UWorld* world = GEngine->GameViewport->GetWorld();

That was it! Thx.

I needed to add protection :


if (GEngine == NULL) return;
if (GEngine->GameViewport == NULL) return;


but then I get a world. Must test it better tonight but I think that was it.
UWorld* world = GEngine->GameViewport->GetWorld();