Skip to content

Player Singleton (NetworkCommonData) to store important immutable variables (player_id, session_id) #256

@TheYellowArchitect

Description

@TheYellowArchitect

Once a player joins, he has access to multiplayer.get_peers() to get the peer ids.
However, almost every online game requires player ids at some point. Most commonly in videogames, there is a player table, with their IDs to the left (think pressing Tab in FPS, or RTS lobbies before starting a scenario/map)
The usage of a player id should be included with netfox, and stored in a singleton, let's name it NetworkCommonData

It currently has:

var local_player_id: int

# This is for converting peer ids to player ids, either using this or a get function
# <peer_id, player_id>
var peer_ids_to_player_ids: Dictionary
var player_ids_to_peer_ids: Dictionary
var local_session_id: int

player_id is also useful in serialization and deserialization, as it's only 1 byte (may I say, half a byte, since 0-16 is the common)
compared to sending peer_id (4 bytes, u_32)

A good practical example is also sharing IDs. See #242 and its solution which requires having the player id.

session_id is useful for the server, to store replays, or save files. Personally for the replay manager I have made, I use this to store replays in the following path user://gamenamefolder/Session_ID/replaydata.replay
and I plan to implement a similar system to netfox, and session id is required.

The variable player_joined_at_tick: int is required for the replay system, and for implementing state diffs. So our above singleton now looks like this, in its final form:

var local_player_id: int
var local_session_id: int
var local_player_joined_at_tick: int
var local_avatar: Node #You can change the type, but basically you can reference your local avatar from any class

# This is for converting peer ids to player ids, either using this or a get function
# <peer_id, player_id>
var peer_ids_to_player_ids: Dictionary
var player_ids_to_peer_ids: Dictionary

var players_joined_at_tick: Dictionary #<player_id, tick>

This singleton's values are filled before/after NetworkTime.is_initial_sync_done()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions