#P2P Multiplayer: ray query not configured anymore for second player after _ready has finished

6 messages · Page 1 of 1 (latest)

steep shadow
#

Hey, I'm making a peer-to-peer battleship game and I use a ray query to get the position of the mouse on a plane (the "board"). To configure this ray query, I am calling the function configure_raycast.

When the first player creates the server and becomes a peer, this function works just fine. Then the second player joins and the function is called again, this time for the second player, obviously.

However, after the _ready function is finished and the _process function is called, the ray query is just not configured anymore for the second player. For the first player the ray query is still configured.

This is the relevant code from the player script:

var ray_query = PhysicsRayQueryParameters3D.new()

func _enter_tree():
    var parent_name = str(get_parent().name)
    set_multiplayer_authority(parent_name.to_int()) 
    # the script is not on the root of the player scene, 
    # hence the get_parent().name

func _ready():
    if !is_multiplayer_authority():
        return
    
    configure_raycast.rpc_id(multiplayer.get_unique_id())

@rpc("call_local")
func configure_raycast(collision_mask_ray_query: int = 2):
    ray_query.collision_mask = collision_mask_ray_query
    ray_query.collide_with_areas = true
    ray_query.collide_with_bodies = false

I'm not sure if I used the @rpc correctly here, but I tried without and that has the exact same result.

This also happens with any variable assigning I do in _ready, but anything that has an @onready tag, or is a field variable in general, works as expected.

For example this works:

var ray_query = PhysicsRayQueryParameters3D.new()

And this does not:

var ray_query

func _ready():
    ray_query = PhysicsRayQueryParameters3D.new()

Is there something wrong with my multiplayer setup if this is happening, or how do I solve this?
If you need any more code, or there are any questions, I will happily clarify. Thanks for any help.

steep shadow
#

Okay, I have found something strange (or something I don't understand). When I put the call of configure_raycast before the if !is_multiplayer_authority(), it works.

#

It seems that the second peer that connects never executes something past the if

#

What is strange to me is that something else later in the _ready function has to be executed on the peer side. This is more of my _ready function:

func _ready():
    if !is_multiplayer_authority():
        return
    
    camera.make_current()
    
    if multiplayer.get_unique_id() != 1:
        move_to_second_player_location()

    configure_raycast()
    # I've now seen that I don't need 
    # to use an rpc here
#

The camera.make_current and the if after that is executed, but as it seems not by the second peer?

#

My multiplayer script is fairly simple and comes from YouTube:

const PORT: int = 9999
var peer = ENetMultiplayerPeer.new()

func on_host_pressed() -> void:
    peer.create_server(port, count_players)
    multiplayer.multiplayer_peer = peer
    multiplayer.peer_connected.connect(add_player)
    add_player()

func on_join_pressed() -> void:
    var potential_error: Error = peer.create_client(ip, PORT) 
    # the ip comes from a UI that I removed from the code for this
    
    multiplayer.multiplayer_peer = peer

func add_player(id = 1) -> void:
    var player = load(player_scene).instantiate()
    player.name = str(id)
    call_deferred("add_child", player)