Use MultiplayerSynchronizer via a script Godot 4

here I would like to know if it is possible to use the MultiplayerSynchronizer node via a script because I make instances of scenes and I would then like to synchronize these instances, if it is impossible to use MultiplayerSynchronizer via a script please can you give me another solution to synchronize these instances?

I put you the code of my scripts below

(i want to synchronize the bullet in the player code)

Player.gd

`extends CharacterBody2D

var smoothed_mouse_pos: Vector2

export var SPEED = 300.0

export var bullet_scene: PackedScene



func _ready():

`DisplayServer.mouse_set_mode(DisplayServer.MOUSE_MODE_HIDDEN)`
func _enter_tree():

`set_multiplayer_authority(name.to_int())`
func shooted():

`if Input.is_action_pressed("shoot"):`

    `var bullet = bullet_scene.instantiate()`

    `add_child(bullet)`


func _physics_process(delta):

`# Get the input direction and handle the movement/deceleration.`

`# As good practice, you should replace UI actions with custom gameplay actions.`

`if is_multiplayer_authority():`

    `var direction = Input.get_axis("ui_left", "ui_right")`

    `var vertical = Input.get_axis("ui_up", "ui_down")`

    `if direction:`

        `#velocity.x = direction * SPEED`

        `velocity.x = direction * SPEED`

    `else:`

        `velocity.x = move_toward(velocity.x, 0, SPEED)`

        

    `if vertical:`

        `velocity.y = vertical * SPEED`

    `else:`

        `velocity.y = move_toward(velocity.y, 0, SPEED)`



    `move_and_slide()`


func _process(delta):

`if is_multiplayer_authority():`

    `smoothed_mouse_pos = lerp(smoothed_mouse_pos, get_global_mouse_position(), 0.3)`

    `look_at(get_global_mouse_position())`

    `shooted()`
`

Bullet.gd

extends Area2D

func _ready():

`pass # Replace with function body.`




# Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta):

`transform.x += Vector2(15 * delta, 0)`

OnlineArena.gd (the multiplayer spawn node)

extends Node2D

var peer = ENetMultiplayerPeer.new()

export var player_scene: PackedScene



func _on_host_pressed():

`peer.create_server(135)`

`multiplayer.multiplayer_peer = peer`

`multiplayer.peer_connected.connect(_add_player)`

`_add_player()`
func _add_player(id = 1):

`var player = player_scene.instantiate()`

`player.name = str(id)`

`call_deferred("add_child",player)`
func _on_join_pressed():

`peer.create_client("localhost", 135)`

`multiplayer.multiplayer_peer = peer`

`var player = player_scene.instantiate()`

I tried to watch others solutions online but no one resolved my problem

Leave a Comment