0 votes

So, in my code there are 4 created players on stage. Control over each player is given to another player. First, the server gives control, and then the client.
I make a script:

enter code here>    if get_tree().is_network_server(): # # For the server, give control of players to the other peers.
    if players >= 2:
        player2.set_network_master(get_tree().get_network_connected_peers()[0])
    if players >= 3:
        player3.set_network_master(get_tree().get_network_connected_peers()[1])
    if players >= 4:
        player4.set_network_master(get_tree().get_network_connected_peers()[2])
else : ## For the client, give control of players to itself.
    if players >= 2:
        player2.set_network_master(get_tree().get_network_unique_id())
    if players >= 3:
        player3.set_network_master(get_tree().get_network_unique_id())
    if players >= 4:
        player4.set_network_master(get_tree().get_network_unique_id())

The server gives control correctly, but the client does not receive correctly. As a result, customers control 3 players at once. It is LAN-wifi game. I dont know how correctly give control to he clients. Sorry for my English. Thanks in advance!

in Engine by (41 points)

1 Answer

0 votes

I don't know if this helps, but you can take a look at my code from one of my games:

extends KinematicBody2D

const MAXSPEED = 400
const ACCELERATION = 500
const FRICTION = 200

var motion = Vector2.ZERO

onready var player_label = $Label
onready var camera = $Camera2D

func _ready():
    player_label.set_as_toplevel(true)
    set_player_name()

func _physics_process(delta):
    if is_network_master():
        camera.current = true
        player_label.rect_position = Vector2(position.x - 60, position.y - 40)
        var input_vector = get_input_vector()
        # print(input_vector)
        apply_movement(input_vector, delta)
        apply_friction(input_vector, delta)
        motion = move_and_slide(motion)

        rpc_unreliable_id(1, "update_player", global_transform)
        Server.send_updated_pos(global_position)

remote func update_remote_player(transform):
    if not is_network_master():
        global_transform = transform
        player_label.rect_position = Vector2(position.x - 60, position.y - 40)

func get_input_vector():
    var input_vector = Vector2.ZERO
    input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
    input_vector.y = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
    return input_vector.normalized()

func apply_movement(input_vector, delta):
    look_at(get_global_mouse_position())
    if input_vector != Vector2.ZERO:
        motion = motion.move_toward(input_vector * MAXSPEED, ACCELERATION * delta)

func apply_friction(input_vector, delta):
    if input_vector == Vector2.ZERO:
        motion = motion.move_toward(Vector2.ZERO, FRICTION * delta)

func set_player_name ():
    player_label.text = Server.players[int(name)]["Player_name"]
by (87 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.