Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Game Session

Dylan Dumesnil edited this page Jul 19, 2020 · 3 revisions

Game Session and Player Info

Game Session Basics

The MDGameSession class is initialized by the MDGameInstance and can by accessed by any Node by calling this.GetGameSession() or elsewhere by using MDStatics.GetGameSession().

The class has 3 useful methods for managing your networked session.

For starting the server you can call:

bool StartServer(int Port, int MaxPlayers);

For connecting to the server from the client:

bool StartClient(string Address, int Port);

And finally for disconnecting (as either server or client)

void Disconnect();

There's also a method to start the session without the network but still trigger the Game Session events to make it easier when implementing your game:

bool StartStandalone();

Those 4 methods are all available as console commands to make it easier for you to start testing.

Game Session Events

These events are all on MDGameSession.

OnSessionStartedEvent()

Triggered when the session begins for any mode (standalone, server, client)

OnSessionFailedEvent()

Triggered when the session cannot start for server or client

OnSessionEndedEvent()

Triggered when the session ends (disconnects)

OnPlayerJoinedEvent(int PeerId)

Triggered on all clients whenever a player joins before initializing the player, including for the local player and existing players when joining in progress

  • PeerId - The PeerId of the player that joined

OnPlayerInitializedEvent(int PeerId)

Triggered on all clients when a player has completed initialization

  • PeerId - The PeerId of the player that completed initialization

OnPlayerLeftEvent(int PeerId)

Triggered on all clients when a player leaves, right before the PlayerInfo is destroyed, also fires locally for each player when disconnecting from the server

  • PeerId - The PeerId of the player that left

OnNetworkNodeAdded(Node NetworkedNode)

Triggered on all clients when a networked node is spawned

  • NetworkedNode - The Node that was just spawned

OnNetworkNodeRemoved(Node NetworkedNode)

Triggered on all clients right before the node is removed

  • NetworkedNode - The Node is about to be removed

Player Info

The MDPlayerInfo class is the location for you to store all the information specific to a player. A player info instance is created for each player on each player's game.

The player info is meant to be extended.

To do this, you must set the PlayerInfo type in your config file, it should match the C# class name (with namespace):

[GameInstance]
PlayerInfoType="MyPlayerInfo"

Then in your MyPlayerInfo class, override void OnServerRequestedInitialization(). In MDPlayerInfo you can see it sends a player name to the server.

Then once initialization is complete, call MarkPlayerInitializationCompleted() which will notify the session that the player info is initialized and ready to be used.

UPNP

By default, UPNP is disabled. To enabled UPNP, change UseUPNP in your config.