Godot Version
4.7
Question
Hello,
I’m mainly looking for a sanity check regarding reparenting scenes under MultiplayerSpawners.
As an example project, imagine a multiplayer card game like dominion, hearthstone or mtg. Playing cards are moved between play areas such as a deck, a player’s hand, the battlefield, discard pile, etc. I thought it made sense to lay out my scene tree where I have a MultiplayerSpawner under each of these play areas. Each spawner had its own “Cards” spawn-path node to spawn the cards in that area. This is nice because each of these areas have their own position and I need to access them under a predictable node path. I figured I could reparent cards between these areas as the game progressed.
Attempting to reparent spawned nodes however causes serious errors immediately despite identical auto-spawn lists. A commonly suggested workaround is to duplicate the card, add it to the new play area, and delete the old card. After all, that is pretty much all that reparenting does anyhow. Though cumbersome, this worked well enough at first.
As my project grew however, using this pattern became uglier and more error-prone. Unwanted lifecycle signals and functions fired causing all sorts unintuitive bugs and required goofy workarounds. Race conditions between clients arose around when old cards are actually deleted and duplicates appeared. Add in issues like potential network lag, client disconnects/reconnects, undo-redos and this reparenting workaround becomes very problematic and very costly to refactor.
I think a better approach is to have a single spawner higher up the scene tree to handle all cards. Moving between play areas can be tracked by Groups or by custom card properties. Yes, positioning and scene tree navigation needs more manual handling, but it avoids far worse issues. I’m surprised this sort of issue doesn’t come up more often since I would think that moving spawned nodes around the scene tree would be very common. I think it would have helped if the docs or somebody had said something like the following:
It is generally advisable to use only one MultiplayerSpawner for each class of spawned scene if you intend on moving instances of those scenes around in the scene tree.
Am I missing something? Is there another more elegant solution? I hope this doesn’t come off as a complaint, I’ve been very happy with Godot so far.
Thanks!