godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.94k stars 3.22k forks source link

godot 4 beta 8 MultiplayerSpawner usage #6475

Closed flobotics closed 1 year ago

flobotics commented 1 year ago

hi, i use godot 4 beta 8 and want to know the usage of MultiplayerSpawner ?

var gltf := GLTFDocument.new()
var gltf_state := GLTFState.new()

var path = "C:/Users/SuperUserName/Documents/godot-workspace/godot-data/Fox.glb"
if FileAccess.file_exists(path):
    print("File to spawn exists")
else:
    print("File to spawn does not exist")
    return

var snd_file = FileAccess.open(path, FileAccess.READ)

var fileBytes = PackedByteArray()
fileBytes = snd_file.get_buffer(snd_file.get_length())

gltf.append_from_buffer(fileBytes, "base_path?", gltf_state)

var node = gltf.generate_scene(gltf_state)

# save gltf as a scene with res:// path, to later add the scene to MultiplayerSpawner.add_spawnable_scene ??
var scene = PackedScene.new()
var result = scene.pack(node)
if result == OK:
    var error = ResourceSaver.save(scene, "res://myscene.scn")  # Or "user://..."
    if error != OK:
        print("An error occurred while saving the scene to disk.")

var mpspawner := MultiplayerSpawner.new()
mpspawner.add_spawnable_scene("C:/Users/SuperUserName/Documents/godot-workspace/Godot-4-Networking-Demonstration-Complete-main/myscene.scn")
 # mpspawner.add_spawnable_scene("res://myscene.scn")
print("spawnable scene count after adding myscene.scn:" + str(mpspawner.get_spawnable_scene_count()) )

mpspawner.spawn()

The doc https://docs.godotengine.org/en/latest/classes/class_multiplayerspawner.html#class-multiplayerspawner-property-spawn-path says

void add_spawnable_scene ( [String](https://docs.godotengine.org/en/latest/classes/class_string.html#class-string) path )

Adds a scene path to spawnable scenes, making it automatically replicated from the multiplayer authority to other peers when added as children of the node pointed by [spawn_path](https://docs.godotengine.org/en/latest/classes/class_multiplayerspawner.html#class-multiplayerspawner-property-spawn-path).

Does the spawner sends the myscene.scn to the peers and "add_child" it there ? If so , how do i do that ?

I also want to use the MultiplayerSynchronizer later to update e.g. the position of the imported scenes.?

Calinou commented 1 year ago

cc @Faless

Dragoncraft89 commented 1 year ago

@flobotics I figured it out: You need a MultiplayerSpawner Node in both the server and the client

Then you need to add your scenes to the "Auto Spawn List" on both server and client (either use set_spawnable_scene or via the UI)

Whenever you add a node from one of theese scenes to the SceneTree, it will automatically replicated.

This usage was quite surprising to me. This should definitely be mentioned in the docs.

Maybe in the form of a small usage example?

flobotics commented 1 year ago

hi @Dragoncraft89 , thanks.

At the moment i am more interrested in https://github.com/godotengine/godot-docs/issues/6529 . I am writing a , i call it distributor, where a client can request a glb (not-known-at-compile time e.g. from sketchfab or AI generated) and distribute it across all clients.

mhilbrunner commented 1 year ago

Closing as duplicate of https://github.com/godotengine/godot-docs/issues/5755 and https://github.com/godotengine/godot-docs/issues/5754

FYI, the multiplayer docs are in the process of being reworked. Until then, have a look at the Bomberman and TPS demos to see how the new multiplayer features in Godot 4.0 can be used.