Skip to content

Expose node network IDs to GDScript #105217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

elementbound
Copy link
Contributor

@elementbound elementbound commented Apr 9, 2025

@@ -184,6 +185,9 @@ class SceneMultiplayer : public MultiplayerAPI {

const HashSet<int> get_connected_peers() const { return connected_peers; }

int get_network_id(Object* p_object, int p_peer_id) const;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy to implement has_network_id() and has_instance() if needed.

// return -1;
// }

return nc->cache_id;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, this is the ID that's ultimately used in the RPC packet, but correct me if I'm wrong.

@elementbound elementbound force-pushed the expose-network-node-id branch from b8dbe89 to 7c047bb Compare April 9, 2025 21:02
ObjectID oid = p_object->get_instance_id();
const NodeCache *nc = nodes_cache.getptr(oid);

// if (!nc->confirmed_peers.has(p_peer_id) || !nc->confirmed_peers.get(p_peer_id) || !nc->recv_ids.has(p_peer_id)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to check here if p_peer_id has confirmed the remote ID?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the peer has not confirmed the id? Is there anything the user can do? Or can the RPC be queued until the id has been confirmed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworked this a bit. Now getting the ID will always return the locally assigned ID, and an extra method was added to check if that ID was confirmed by a given peer.

@elementbound elementbound marked this pull request as ready for review April 9, 2025 21:16
@elementbound elementbound requested review from a team as code owners April 9, 2025 21:16
@Calinou Calinou added this to the 4.x milestone Apr 9, 2025
@JoNax97
Copy link
Contributor

JoNax97 commented Apr 10, 2025

If I understand the code correctly, the way the ids work is that each peer assigns an id to their own version of the node and broadcasts it to all other peers right? So each peer knows how to refer to a node to any other peer, but the ids themselves are not the same for all peers.

If this is to be used alongside RPCs, it means users cannot call rpc() once, but must instead iterate over all peers, get their own id, and then call rpc_id() for each peer individually.

This can be alleviated by having the engine do the conversion internally when a node is detected in the rpc parameters. I guess it might still incur some performance penalty, as the payload must be re-encoded for each peer separately.

Maybe the system can be reworked so that the peer that has authority over the node assigns the network id for all other peers? That would be way outside the scope of this PR in any case.

@fire
Copy link
Member

fire commented Apr 10, 2025

users cannot call rpc() once, but must instead iterate over all peers, get their own id, and then call rpc_id() for each peer individually.

This seems like a significant drawback.

@JoNax97
Copy link
Contributor

JoNax97 commented Apr 11, 2025

After thinking about this problem a bit more, I opened a new proposal here:
godotengine/godot-proposals#12217

This PR does not conflict with it, because even if the mechanism of the ids change, this is a lower-level API that would stay the same.

@elementbound
Copy link
Contributor Author

@JoNax97 I think the issue of having to re-encode the packet for every target can be avoided, by using the locally assigned ID when talking to peers that know about it, and using the node path for peers that don't. This way, you get at most two buffers to encode, instead of one per peer.

If memory serves right, Godot does this under the hood too for RPCs.

@fire
Copy link
Member

fire commented Apr 13, 2025

Would you say godotengine/godot-proposals#12217 supersedes this pull request?

@JoNax97
Copy link
Contributor

JoNax97 commented Apr 13, 2025

No, I don't think so. I think it's fairer to say that this PR partially implements it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants