GetWorld()->IsServer() from GameInstance

How to recognize on server or on client currently running game?

In the Init method of GameInstance object I used the

GetWorld()->IsServer();

In the editor Init methods runs twice (from server and from client (?) ). But in both situations IsServer returns true
On Pawn object in BeginPlay method GetWorld()->IsServer() gives me different results: true and false.
On GameState I also got true and false.

But GameInstance is also available on server and on client. Whats wrong with it? Status IsServer may be also used on client?

My task is crating caller to other server. And I need to differentiate work with this caller on server and on client. GameInstance is best solution I think because I can get access to it in anytime.

This is my understanding:

There are two ways to create the Server. If you look at the drop down arrow next to the Play button, you will see a check-mark for “Dedicated Server” and “Number of Players”, these control how the server/client for Player1 is setup.

Here is what happens when you press Play :

Server is created first

Server runs GameInstance Init

THEN the Server creates Players/Clients if “Dedicated Server” is checked, or if “Number of Players” is 2 or more.

Players/Clients will NOT run GameInstance Init themselves.

  • Using IsServer? during GameInstance Init will always be TRUE because of this!
  • Use IsServer? to find the Player who is HOST/Server
  • Use IsDedicated? to find Player0/Server when “Dedicated Server” is checked (IsServer? will also be TRUE)
  • If you plan on creating a multiplayer game where a Client/Player can become a Server/Player, use IsServer?
  • If you plan on creating a multiplayer game where ALL clients connect to a Central Server and that Server SHOULD NOT be able to become a Client/Player use IsDedicated? .

Here are the checked/unchecked scenarios for “Dedicated Server” :

Dedicated Server is checked :

At GameInstance Init

 Player0 : Server

IsServer Yes

IsDedicated Yes

IsClient No

AFTER GameInstance Init

Player0/Server creates Player1,2,3,4,5 ect

 Player1,2,3,4,5 ect : Client

IsServer No

IsDedicated No

IsClient Yes


Dedicated Server NOT checked :

At GameInstance Init

 Player0 : Server

IsServer Yes

IsDedicated No

IsClient No

AFTER GameInstance Init

Player0/Server becomes Player1/Server

Server creates Player2,3,4,5 ect

 Player2,3,4,5 ect : Clients

IsServer No

IsDedicated No

IsClient Yes

Only two things can happen here to change this:

A. ANY Player runs “Create Session”
If PlayerX is a Client:

 PlayerX becomes a Server and creates a Session

B. ANY Player connects to a Server using “Join Session”.

If PlayerX is a Server:

PlayerX now becomes a Client

If PlayerX is a Client:

PlayerX Joins Server
1 Like