0 votes

`Ok,

I am trying to write two scripts. A server and a client. The client will ask for a value, then send it to the server (which I was able to do). The server is to do a calculation with that value (for simplicity, let's say it multiplies it by 2) and return the result.

I'm currently trying to use rset_id(), but it doesn't seen to be working (as expected). Any help would be appreciated. Thank you.

Edit - Here is my code for all to see and use if like. And no, that is not my email
address. Just made it up for testing purposes.

Server.gd

extends Control

onready var chatLog = get_node("VBoxContainer/RichTextLabel")
onready var inputField = get_node("VBoxContainer/HBoxContainer/LineEdit")

var groups = [
    {'name': 'Server', 'color': '#e5e716'},
    {'name': 'User_Join', 'color': '#38f610'},
    {'name': 'User_Leave', 'color': '#f62610'},
    {'name': 'User_Request', 'color': '#f610f2'}
]

#-----***** Network Variables *****-----

var server_name = "Server"      # Holds the name of the server
var server_ip = "127.0.0.1"
var server_max_players = 5     # Maximum allowed connections
var server_port = 7376       # Listening port
var users = {}

#***************************************



#-----***** Networking Functions *****-----

func create_server():
    # Initialize the networking system
    var net = NetworkedMultiplayerENet.new()

    # Try to create the server
    if (net.create_server(server_port, server_max_players) != OK):
        add_message("Server Creation Failed", 0)
        return

    get_tree().set_network_peer(net)

    add_message("Server Creation Successfull", 0)
    add_message("Server id is "+str(get_tree().get_network_unique_id()), 0)

func _player_connected(id):
    add_message("User -"+str(id)+"- Connected Successfull", 1)
    #rpc_id(id, "register_player", my_info)
    # Everyone gets notified whenever a new client joins the server

func _player_disconnected(id):
    add_message("User -"+str(id)+"- Disconnected Successfull", 2)

func _connected_to_server():
    # Peer trying to connect to server is notified on success
    pass
func _connection_failed():
    # Peer trying to connect to server is notified on failure
    pass
func _disconnected_from_server():
    # Peer is notified when disconnected from server
    pass

#-----***** Networking Functions End *****-----

#-----***** Network Remote Functions *****-----

remote func get_num(id):
    var number=1234567890
    add_message("Attempting RPC "+str(id)+" "+ str(number), 3)

    rset_id(id, "number", 0987654321) #randi()%6)


#**********************************************

func _ready():

#-----***** Set network event signal functions *****-----

    get_tree().connect("network_peer_connected", self, "_player_connected")
    get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
#   get_tree().connect("connected_to_server", self, "_connected_to_server")
#   get_tree().connect("connection_failed", self, "_connection_failed")
#   get_tree().connect("server_disconnected", self, "_disconnected_from_server")

#********************************************************   

    inputField.connect("text_entered", self,'text_entered')
    inputField.grab_focus()

#-----***** Create Server *****-----

    add_message("Attempting to create server at ip", 0)
    create_server()
#***********************************    

func add_message(text, group):
    chatLog.bbcode_text += '\n' 
    chatLog.bbcode_text += '[color=' + groups[group]['color'] + ']'
    chatLog.bbcode_text += '[' + groups[group]['name'] + ']: '
    chatLog.bbcode_text += text
    chatLog.bbcode_text += '[/color]'

func text_entered(text):
    if text =='/h':
        add_message('Type /q to quit', 0)
        add_message('Type /u to view list co connected user IDs', 0)
        inputField.text = ''        
        return
    if text =='/q':
        get_tree().set_network_peer(null)
        get_tree().quit()
        return
    if text =='/u':
        add_message(str(get_tree().get_network_connected_peers()),0)
        inputField.text = ''
        return
    add_message(text, 0)
    inputField.text = ''

Client.gd

extends Control

#-----***** Network Variables *****-----

var server_name = "Server"      # Holds the name of the server
var server_ip = "127.0.0.1"     #Server IP Address
var server_max_players = 5   # Maximum allowed connections
var server_port = 7376       # Listening port
var users = {}

var my_info = { name = "cd@gmail.com", count = 0 }

var number = -1

#***************************************

#-----***** Networking Functions *****-----

func join_server():

    var net = NetworkedMultiplayerENet.new()

    if (net.create_client(server_ip, server_port) != OK):
        $c1.text="Failed to create client"
        get_tree().set_network_peer(null)
        return

    get_tree().set_network_peer(net)
    $c1.text="sucess "+str(get_tree().get_network_unique_id())

func _player_connected():
    #rpc_id(1, "register_player", my_info)
    pass
func _player_disconnected():
    pass
func _connected_to_server():    
    rpc_id(1, "get_num", get_tree().get_network_unique_id())
    $c2.text=str(number)
    number-=1
    # Peer trying to connect to server is notified on success
    pass
func _connection_failed():
    # Peer trying to connect to server is notified on failure
    pass
func _disconnected_from_server():
    # Peer is notified when disconnected from server
    pass

#-----***** Networking Functions End *****-----

#-----***** Network Remote Functions *****-----

func _input(event):
    if event is InputEventKey:
        if event.pressed and event.scancode == KEY_ENTER:
            rpc_id(1, "get_num", get_tree().get_network_unique_id())
            $c2.text=str(number)
            number-=1
        if event.pressed and event.scancode == KEY_ESCAPE:
            get_tree().set_network_peer(null)
            get_tree().quit()
        if event.pressed and event.scancode == KEY_TAB:
            pass


#**********************************************

func _ready():

#   randomize()



#-----***** Set network event signal functions *****-----

    #get_tree().connect("network_peer_connected", self, "_player_connected")
    #get_tree().connect("network_peer_disconnected", self, "_player_disconnected")
    get_tree().connect("connected_to_server", self, "_connected_to_server")
    #get_tree().connect("connection_failed", self, "_connection_failed")
    #get_tree().connect("server_disconnected", self, "_disconnected_from_server")

#********************************************************
    #var yess = 0

    join_server()

    pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#   pass
in Engine by (14 points)
edited by

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 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.