0 votes

HEY! i tried making a multiplayer game, and in the calculateDirection() function i put rpc_unreliable but it doesn't work
here's the code:

extends KinematicBody2D

onready var maxTimeBetweenShots= global.gun_dict[global.holding]["time_between_shots"]
export var speed:= Vector2(30,30)

var bullet = load("res://gameObjects/bullet.tscn")

var direction: Vector2
var velocity: Vector2
var old_direction: Vector2

var timeBetweenShots: int
var in_body: bool

var old_speed
var b

signal bullet_hit

slave var slave_position = Vector2()
slave var slave_movement = Vector2.ZERO



slave func setPosition(pos):
    Network.update_position(get_tree().get_network_unique_id(), pos)
    print("updating position")

func _ready() -> void:
    old_speed = speed


func calculateVelocity(direction1) -> void:
    if Input.is_action_pressed("Shift"):
        speed = speed * 2
    velocity = direction1 * speed


func calculateDirection() -> void:
    if is_network_master():
        direction = Vector2( Input.get_action_strength("right") - Input.get_action_strength("left"), 
        Input.get_action_strength("down") - Input.get_action_strength("up"))

        rpc_unreliable('setPosition', Vector2(position) + velocity)
        calculateVelocity(direction)


func createBulletInstance() -> void:
    if timeBetweenShots <= 0:
        b = bullet.instance()
        get_parent().add_child(b)
        var dir = Vector2(1,0).rotated(get_parent().get_node("gun/gun").global_rotation)
        b.start(global.bullet_spawn_pos, dir)
        maxTimeBetweenShots= global.gun_dict[global.holding]["time_between_shots"]
        timeBetweenShots = maxTimeBetweenShots
    else:
        timeBetweenShots -= get_process_delta_time()


func _physics_process(delta: float) -> void:
    calculateDirection()
    move_and_slide(velocity)
    if direction != old_direction:
        if direction.x > 0 && $player_sprite.flip_h == true:
            $player_sprite.flip_h = false
        elif direction.x < 0 && $player_sprite.flip_h == false:
            $player_sprite.flip_h = true
    old_direction = direction
    speed = old_speed


func _process(delta: float) -> void:
#   $Label.text = Network.players[get_tree().get_network_unique_id()]["name"]
    var labelSize = $Label.rect_size
    $Label.rect_global_position = position + Vector2(-labelSize.x/2, -75)
    if Input.is_action_pressed("left_click") && global.holding != "none":
        createBulletInstance()
    if in_body && Input.is_action_pressed("E"):
        global.gun_throwing = false
        global.holding = "Pistol"
    set_global_playerpos()


func set_global_playerpos() -> void:
    global.player_pos = position


func _on_detector_body_entered(body: Node) -> void:
    if body.is_in_group("Weapons"):
        in_body = true


func _on_detector_body_exited(body: Node) -> void:
    if body.is_in_group("Weapons"):
        in_body = false


func _on_detector_area_entered(area: Area2D) -> void:
        if area.is_in_group("Bullet"):
            $HealthPlayer.current -= area.damage
            area.queue_free()
        elif area.is_in_group("Food"):
            $HealthPlayer.current += 5
            area.queue_free()


func _on_HealthPlayer_depleted() -> void:
    _die()

sync func _die():
    global.holding = "none"
    queue_free()

Thanks for checking this out!

in Engine by (120 points)

What exactly do you mean by "not working"? Does it throw an error? Did you expect it to behave differently? Did you make sure the package actually arrived? (It's called "unreliable" for a reason)

it's supposed to call the setPosition but it doesn't call it at all, there are no errors though

In the script you provided I can't see any reason why it shouldn't. Can you provide an example project?

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.