Skip to content

Instantly share code, notes, and snippets.

@mhilbrunner
Last active August 29, 2023 00:54
Show Gist options
  • Save mhilbrunner/31e79a20a11d7e5f0daf2ab92251d2d9 to your computer and use it in GitHub Desktop.
Save mhilbrunner/31e79a20a11d7e5f0daf2ab92251d2d9 to your computer and use it in GitHub Desktop.
Unofficial Godot 4.0 Pre-Alpha Multiplayer Changes Overview

Unofficial Godot 4.0 Pre-Alpha Multiplayer Changes Overview

Note: Everything may still change, if you need something stable and documented to depend on, use Godot 3.x/stable.

Last updated: 2021-10-06

Ask questions in #networking on chat.godotengine.org

Relevant Pull Requests (look at all):

Setup peer

# Create peer
var peer = ENetMultiplayerPeer.new()

# Connect some signals if you want them
peer.peer_connected.connect(_peer_connected)
peer.peer_disconnected.connect(_peer_disconnected)
peer.connection_failed.connect(_connection_failed)
peer.connection_succeeded.connect(_connection_succeeded)
# (You can also connect to get_tree().multiplayer.peer_connected and so on,
# that works even while the peer is null)

# peer.create_server(SERVER_PORT)
peer.create_client(SERVER_IP, SERVER_PORT)

# Assign peer to multiplayer API
# Multiplayer API now has most relevant functions, have a look at get_tree().multiplayer
get_tree().multiplayer.network_peer = peer

RPCs are @rpc annotations now

RSETs are gone, just use RPCs now.

You use the @rpc annotation to denote RPC functions. You can use arguments with that annotation for configuration, in any order. Arguments are:

  • sync/nosync (whether calls are called locally too, or only remote)
  • any/auth (whether the RPC can be called by any peer or only auth, the authority (previously master)
  • reliable/unreliable/ordered (transfer mode)

Examples:

remote func test() becomes

@rpc
test()

remotesync func test() becomes

@rpc(any, sync)
test()

mastersync would now be @rpc(auth, sync).

You can also still use Node.rpc_config():

rpc_config(&'move_and_slide', RPC_MODE_ANY)

Using the new multiplayer spawning and syncing

This enabled scene spawning/syncing replication for the scenes "Player" and "Box". For both, spawns are synced, so if you .instantiate() them and add them to the scene, that gets done on all other peers automatically as well, same for .queue_free()ing/deleting them. For "Player", position is kept synced as well, so if you move, position gets updated on peers. For real use, you want to use a custom RPC for movement (to be able to interpolate the position and smooth updates). But this is useful to keep less important things synchronized.

func _ready():
	_replication_setup(Player, ["position"], true)
	_replication_setup(Box, ["position"])

func _replication_setup(scene : PackedScene, params : Array, sync : bool = false):
  	# Scenes are identified using the new resource UIDs
	var id = ResourceLoader.get_resource_uid(scene.resource_path)
	
  	# Enable spawn/despawn syncing
	multiplayer.replicator.spawn_config(
		id,
		MultiplayerReplicator.REPLICATION_MODE_SERVER,
		params
	)
	
	if sync:
    		# Enable property syncing
		multiplayer.replicator.sync_config(id, 16, params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment