0 votes

Hi..
I have a 2D fighting game which i want players can fight each other from net (player vs player)
i read some high level networking tutorials/samples for that. this is my lobby code. there are two main problems (for now)
1- the one which is server move properly, but the client one does not move at all.
2- game pause does not work for synchronising the two screen.

Lobby Code:

extends Control
func _ready():
    get_tree().connect("network_peer_connected", self, "_player_connected")
    name_holder.text = global.Name
func _on_PlayerName_text_changed(new_text):
     name_holder.text = new_text
     global.Name = name_holder.text
func _on_PvPButton_pressed():
    print("Hosting network")
    global.client = false
    var host = NetworkedMultiplayerENet.new()
    var res = host.create_server(4242, 2)
    if res != OK:
        print("Error creating server")
        global.client = true
        host.create_client("127.0.0.1",4242)
        get_tree().set_network_peer(host)
    else:
        global.client = false
        get_tree().set_network_peer(host)
func _player_connected(id):
    print("Player connected to server!")
    global.connected_player = id
    pre_start_game()
func pre_start_game():
    var ring = load("res://scenes/Modes/Default/PvP-Default.tscn").instance()
    var player1 = ring.get_node("Player01")
    var player2 = ring.get_node("Player02")
    if global.client == false:
        player1.set_name(str(get_tree().get_network_unique_id()))
        player1.set_network_master(get_tree().get_network_unique_id())
        player2.set_name(str(global.connected_player))
        player2.set_network_master(global.connected_player)
    else:
        player2.set_name(str(get_tree().get_network_unique_id()))
        player2.set_network_master(get_tree().get_network_unique_id())
        player1.set_name(str(global.connected_player))
        player1.set_network_master(global.connected_player)
    post_start_game()
remote func post_start_game():
    get_tree().change_scene("res://scenes/Modes/Default/PvP-Default.tscn")

codes which i use in both player01 and player02 scripts:

    func _physics_process(delta):
    if is_network_master():
         rpc_unreliable("setPosition",Vector2(position.x - velocity.x, position.y - velocity.y))
    slave func setPosition(pos):
      position = pos

P.S1: i removed "gettree().setpause(false)" from lobby codes, because it freeze everything (idk how to use it in networking for synchronising)
P.S2: Im using one button for being host and join the server, so I mix host and join codes together (func onPvPButton_pressed). beside i use 2 kinematic characters (player01 and player02), server is player01 and client is player02

in Engine by (76 points)

1 Answer

0 votes

Hi,
First, using

 get_tree().change_scene("res://scenes/Modes/Default/PvP-Default.tscn")

will overwrite your previous code (it will reload the scene). So you can remove it.
I think it's better to load your world, then add it as a child to your main scene, so your change will stay.

var ring = load("res://PvP-Default.tscn").instance()
$World.add_child(ring)

Then, you don't need to change the node name, since you already hardcoded 2 players in your scene.

by (64 points)

hi dant4
thank you for your guidance, ive fixed these problems, but what you said about changing the scene, fixed another problem in the ring scene, i could click the main menu button in ring scene, that gettree().changescene() fixed this problem.. thank you for your time and guidance :)

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 webmaster@godotengine.org with your username.