NetworkManager - RoyasDev/EzNet GitHub Wiki

Introduction

The NetworkManager class is the heart of EzNet. It manages all of the player connects and tracking of who owns what object. The NetworkManager is designed to be overridden though to change some core functionality based on your games needs. If the NetworkManager is autoloaded the name cannot conflict with an existing class.

Must be loaded into the scene before the NetworkObjects so that they can get the NetworkManager @onready by default. They will also need the path to where the NetworkManager is loaded

Constants

Some of the constants in the NetworkManager class are meant to be changed. These all have to do with how the NetworkManager handles RPC's for its core functionality. The constants that aren't listed here should not be changed unless you want to break something.

TICK_TYPE : Tick_Type #Manages what networking protocol the tick RPC's are sent over the network as. Default is UNRELIABLE, but it can be switched to RELIABLE or UNRELIABLE_ORDERED

SYNC_VAR_CHANNEL : int #Manages the channel that sends out the sync vars. Default is channel 2

TICK_CHANNEL : int #Manages the channel that sends out the network ticks. Default is channel 3

MANAGEMENT_CHANNEL : int #Manages the channel that handles core network logic e.g. spawning and object initialization. Default is channel 4

Exported Variables

networker : Networker #Needs to be assigned for the NetworkManager to work. The networker decided what type of multiplayerpeer should be used

ticks_per_second : int #Determines how many network ticks per second happen

Callables

validate_request_spawn_callable : Callable #Used to validate a spawn request that was received from a client returns true if the spawn is valid. If this isn't assigned the spawn is automatically pushed through. Takes in a Dictionary of the spawn args.

validate_spawn_callable : Callable #Used on the clients to determine if they want to spawn an object that was sent from the server. Takes in a dictionary that has the spawn args

spawn_batching_logic : Callable #If true the server will add this spawn to a batch that will go out next network tick. If unassigned the spawn will automatically be batched. Takes in a Dictionary of spawn args

Signals

on_server_started #Emitted when the server has started up or when the client successfully connects to the server 

on_tick(tick_number : int) #Emitted every network tick

Public Functions

public functions are functions that are expected to be called from outside the NetworkManager class

_create_server() #Called to start hosting a server using the current Networker

_connect_client() #Called to connect a client to a server using the current Networker

register_network_object(network_object : NetworkObject) #Used to register a NetworkObject to a specific player

_switch_network_object_owner(new_owner : int, network_object : NetworkObject) #Used to switch a NetworkObject to a new player on the local machine.

_remove_network_object(network_object : NetworkObject) #Used to remove a NetworkObject from a player. Only occurs on the local machine.

_refuse_new_connections(should_refuse : bool) #Is used to allow or disallow new connections to the network. Call on the server

_get_refuse_connections() -> bool #Returns the status of whether or not the server is accepting new connections

_disconnect() #Call on the server to shutdown the server. Call on the client to disconnect the client from the server

Helper Functions

_request_spawn_helper(resource_path : String, args : Dictionary = {}, owner_id : int = 1, object_id : int = -1) #Used to help create a spawn request in the correct format for the server to read. The resource_path should point to a spawnable object. The Dictionary is for your own custom spawn arguments. The owner_id and the object_id are only used if this is called from the server to assign the ownership of the object from the moment it spawns. This function isn't required, its just helpful

Overrideable Functions

This is a list of functions you are expected to override when extending the NetworkManager class

_spawn_object(spawn_args : Dictionary) -> Node #Called on each client when a new object is trying to spawn. Override this to add any custom spawn logic for your games

_on_tick(current_tick : int) #is connected to the on_tick signal so that all you have to do is define the function in your version of the network manager so you don't have to do the signal connection

_on_network_started() #is connected to the on_network_started signal so that all you have to do is define the function in your version of the network manager so you don't have to do the signal connection

RPC's

_request_spawn_object(spawn_args : Dictionary) #Sent from the client to the server to request an object to be spawned. Needs a resource_path key in dictionary to be set

_network_spawn_object(spawns : Array) #Sent from the server to the clients. Has a list of objects that should be spawned

_update_dirty_sync_vars(owner_id : int, variables : Array) #Sent from the server to the clients with a list of sync vars to update

_destroy_connected_player_data(player_id) #Sent from the server to clients when a player should stop being tracked

_add_connected_player(player_id : Array[int], network_objects : Array[Dictionary]) #Sent from the server to a newly connected client with all of the information on the current state of the game

_on_network_tick(current_tick : int) #Sent from the server to the clients to notify them of a network tick