Skip to content

Typed RPC (generated from #[rpc] methods) #1158

Open
@ValorZard

Description

@ValorZard

Right now, to use a RPC, you have to do something like this

// Using unreliable to make sure position is updated as fast
            // as possible, even if one of the calls is dropped.
            let args = varray![self.base().get_position(), self._motion];
            let varargs = variant_array_to_vec(args);
            self.base_mut()
                .rpc("set_pos_and_motion", varargs.as_slice());

Here’s a function I used to make this easier

fn variant_array_to_vec(array: VariantArray) -> Vec<Variant> {
    let mut vec = Vec::new();
    for i in 0..array.len() {
        vec.push(
            array
                .get(i)
                .expect("Failed to get element from VariantArray"),
        );
    }
    vec
}

This is weird, because RPCs are already typed on the receiving end

#[godot_api]
impl Paddle {
    /*
    # Synchronize position and speed to the other peers.
    @rpc("unreliable")
    func set_pos_and_motion(pos: Vector2, motion: float) -> void:
        position = pos
        _motion = motion
     */
    #[rpc(unreliable)]
    fn set_pos_and_motion(&mut self, pos: Vector2, motion: f32) {
        self.base_mut().set_position(pos);
        self._motion = motion;
    }
}

Ideally we should do something like we do with signals

Probably something like

object.rpc().my_function();

For rpc_id, you could do

object.rpc_id(1).my_function();

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: registerRegister classes, functions and other symbols to GDScriptfeatureAdds functionality to the library

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions