Any ENet example without rpc? (send packets between clients)

Godot Version

4.2.2

Why Help Me? (extra incentives :+1:)

  1. If I figure this out, I’ll contribute to the docs with examples and clarifications.
  2. I want to make a template project for anyone to use and learn from.

Question

How can I send and receive raw packets (likely with put_packet and get_packet) between clients, using a client-server structure where packets are relayed between all clients?

Using ENet, specifically without rpc, it must be possible since we have an ENetPacketPeer that inherits PacketPeer (unless it’s broken :stuck_out_tongue:).

(I tried this for ~15 hours already :sweat_smile: , read tons of docs, searched forums, issues, read C++ source code, and created 5 blank test projects to try everything that came to mind… no success.)

Could anyone create a tiny minimal repro code that proves this is even possible in Godot 4.2?

SceneMultiplayer — Godot Engine (stable) documentation in English is probably what you want.

:sparkles::sparkles: Thank you!!! :sparkles::sparkles:

I created a repository with the code:
marcospb19/godot-enet-packet-server-example

With buttons for connecting and messaging:

Here is the code (without the color stuff):

extends Node

# ---------- Setup server or client ----------
func setup_network(is_server: bool):
    var peer := ENetMultiplayerPeer.new()
    
    if is_server:
        peer.create_server(55556)
    else:
        peer.create_client("127.0.0.1", 55556)
    
    multiplayer.multiplayer_peer = peer
    multiplayer.peer_connected.connect(peer_connected_callback)
    multiplayer.peer_packet.connect(received_message_callback)

# ---------- Button signals ----------
func _on_start_server_button_pressed():
    setup_network(true)

func _on_start_client_button_pressed():
    setup_network(false)

func _on_send_message_button_pressed():
    var msg = "Hi from %d" % local_id()
    multiplayer.send_bytes(msg.to_utf8_buffer())

# ---------- Network signal callbacks ----------
func received_message_callback(id: int, packet: PackedByteArray):
    print(
        "peer %d: received msg from peer %d '%s'"
            % [local_id(), id, packet.get_string_from_utf8()]
    )

func peer_connected_callback(connected_peer_id: int):
    print('peer %d: received connection signal from (peer %d)' % [local_id(), connected_peer_id])

# ---------- Utils ----------
func local_id() -> int:
    return multiplayer.multiplayer_peer.get_unique_id()

(Your link points to send_bytes, it took me a minute to find out that I also needed to use the SceneMultiplayer.peer_packet signal :slightly_smiling_face:, now I need to put an example in the docs)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.