C# - How do i properly use the SpawnFunction of MultiplayerSpawner?

Godot Version

4.3

Question

SpawnFunction = Callable.From(() => nameof(EntityManager.Instance.SpawnPlayer));

error:

E 0:00:00:0935   Callable.generics.cs:20 @ void Godot.Callable.<ThrowIfArgCountMismatch>g__ThrowArgCountMismatch|0_0(int, int, string): System.ArgumentException: Invalid argument count for invoking callable. Expected 0 arguments, received 1. (Parameter 'args')
  <C# Error>     System.ArgumentException
  <C# Source>    /root/godot/modules/mono/glue/GodotSharp/GodotSharp/Core/Callable.generics.cs:20 @ void Godot.Callable.<ThrowIfArgCountMismatch>g__ThrowArgCountMismatch|0_0(int, int, string)
  <Stack Trace>  Callable.generics.cs:20 @ void Godot.Callable.<ThrowIfArgCountMismatch>g__ThrowArgCountMismatch|0_0(int, int, string)
                 Callable.generics.cs:16 @ void Godot.Callable.ThrowIfArgCountMismatch(Godot.NativeInterop.NativeVariantPtrArgs, int, string)
                 Callable.generics.cs:264 @ void Godot.Callable.<From>g__Trampoline|11_0<TResult>(object, Godot.NativeInterop.NativeVariantPtrArgs, Godot.NativeInterop.godot_variant&)
                 DelegateUtils.cs:86 @ void Godot.DelegateUtils.InvokeWithVariantArgs(nint, System.Void*, Godot.NativeInterop.godot_variant**, int, Godot.NativeInterop.godot_variant*)

i tried using
new Callable(this, MethodName);
which looks fine but then gives error of “need a valid callable property”

I don’t use Csharp but the docs say this.

// Default parameter values are not supported.
public void PrintArgs(Variant arg1, Variant arg2, Variant arg3 = default)
{
    GD.PrintS(arg1, arg2, arg3);
}

public void Test()
{
    // Invalid calls fail silently.
    Callable callable = new Callable(this, MethodName.PrintArgs);
    callable.Call("hello", "world"); // Default parameter values are not supported, should have 3 arguments.
    callable.Call(Vector2.Up, 42, callable); // Prints "(0, -1) 42 Node(Node.cs)::PrintArgs".
    callable.Call("invalid"); // Invalid call, should have 3 arguments.
}

Thanks @anon45973056!

no, this function needs be assigned to a MultiplayerSpawner node’s SpawnFunction which is a Callable class property. The function can be anything, with any number of parameters, but it has to return a node.

Gd script example

$MultiplayerSpawner.spawn_function = my_spawn

$MultiplayerSpawner.spawn(id, pos)

func my_spawn(id, pos) -> Node3D:
  var player = player node.instanciate()
  player.name = str(id)
  player.position = pos
  return player
1 Like

hello! thanks. i have made a very similar snippet but i still get the error
image


i dont really understand why, could be caused by parameters but to what i search the parameter is a variant so it should be correct.

i actually gave up and realized i believe to be the same thing but in the built in editor to set up what scenes would load up, instead of manually setting by code. the “auto spawn list”, which to my understand is that everytime i spawn the node in the AutoSpawnList in that same spawn path, it will auto spawn for peers.

Oh yeah,

I will say that custom spawns can solve some harder synchronizing puzzles. But basic spawning can be down with the auto spawn list.

Even though you might not need it, Im hoping @anon45973056 has some knowledge on Callable magic between C# and Godot.

that is very interesting, thanks. how would this work with a return type? “SpawnFunction” of MultiplayerSpawner requires a return type, which i have done

SpawnFunction = Callable.From<Variant>(EntityManager.Instance.SpawnPlayer);

but now i get "Node EntityManager.SpawnPlayer(Variant) has the wrong return type."


i believe this to be a C# knowledge but will be interesting to understand and specially help future users