Custom GameState null in multiplayer

Hi there guys and gals,

I’m currently stuck with the mentioned problem. I have implemented a custom game mode which allows me to set up custom playerContollers, playerStates and so on.
I’ve created a blueprint for my custom GameMode and GameState and set them up in edit → project settings → Maps& Modes.
(I’ve tried to use plain c++ classes this didn’t see to change anything)

When I run the game in singleplayer I am able to get a reference for the used gameState via
getWorld()->getGameState() in my custom PlayerController which, as I understood, exists on the server and once per owning client.

When I run the game with multiple clients, the first one is able to get a reference to the gameState (since its the server I guess). But the second one only gets a null reference when trying to get the gameState via
getWorld()->getGameState().

Anyone got an idea why this happens?

Thanks for your help and have a nice one!

If I remember correctly, the GameState is accessible through all the clients but only the server side version of the client classes, like PlayerController.

You may have to only try and access it via ROLE_Authority. If you are not authority, you’ll need to call a server function to get to the server side and then access GameState.

I’m accessing it during gameplay. More specifically, every time when the player presses the “E” button. I’d assume this is way past begin play. So it should be available sometime soon. But it stays null.

As far as I know the gameStates purpose is to be available on client side?

It just came to my mind. I’m propably searching in the wrong place.
Thing is, I raytrace in multiplayer with every player character to check what they are looking at. Now, in singleplayer the tracing works. in multiplayer the trace doesn’t seem to work on the client. It doesn’t hit anything but itself. Which is strange since I tell the raytrace to ignore the character actor.
Since the gamestate which I try to access before raytracing is null for some time I misled myself into believing the gamestate would be null all the time and therefore would be the cause to the problem. I have to verify this after work.

Thanks for helping me along.

Can you show more of the code that is referencing the GameState from as well as how? With more detail than GetWorld( )->GetGameState( ), such as the classes you are using to reference the GameState.

So, I’ve just verified my guess. The gamestate is fine. It takes some time to replicate but it’ll be there eventually.
Is there anything to point me to for raytracing in multiplayer. Do I have to do something diffrent when raytracing in multiplayer?

Kyle since you’ve asked, here is some context for the GetWorld( )->GetGameState( ) call:
Within a APlayerController derived class:

AActor* AMyPlayerController::CheckLineOfSight() {

    UWorld* world = GetWorld();
    AMyGameState* gameState = world->GetGameState<AMyGameState>();
[...]

Im curious if the template version is failing to return the casted type.

Have you tried something like:

 AActor* AMyPlayerController::CheckLineOfSight() 
 {
     UWorld* world = GetWorld();
	 AMyGameState* gameState = nullptr;
	 if(world)
	 {
		gameState = Cast<AMyGameState>(world->GetGameState());
	 }  
}

The GameState is not available immediately on clients as it is not replicated right away.

When are you trying to get a reference to the GameState? If it is on BeginPlay this is most likely the reason that it is null. You will have to wait until it has been replicated.

I gave that a try and it worked as well. So the gameState will be replicated roughly two to six ticks after loading the map.

I did find out why my raytrace didn’t work in multiplayer as well. I’ve attached a sphere as a component for overlap checks to my character actor. The trace is set to ignore the character which starts the raytrace, but it did collide with the sphere of the second character which was close by and returned that actor.

I’ve created a custom trace channel for my collision checks. I guess the sphere object defaults to block on that channel. So in multiplayer the raytrace would collide with the quite large sphere of the close by second actor instead of the desired object.

Thanks for your help!

1 Like