Hello, everyone:
I'm experiencing an issue, where my client won't connect to my server. I adopted my code from the official networking "Multiplayer Bomber" demo project.
My singleton (global) script is gamestate.gd and here is its contents:
extends Node
# Handles multiplayer functionaly for both client and server
class Player extends Node:
"""
Temporary (logged in) player.
"""
# general information
var username = ""
var faction = null
var level = null
var experience = null
var position = null
var scene = null
var health = null
var player_class = null
# pvp stats
var alive = null
var last_death_time = null
var last_attacked_by = null
var slain_by = null
var last_person_attacked = null
# dueling
var is_dueling = null
var dueling_with = null
# character stats
var armor = null
var parry = null
var strength = null
var intellect = null
var agility = null
var spirit = null
var critical = null
# guild/clan (named Warband)
var warband = null
var warband_leader = false
# appearance
var body = null
var face = null
var head = null
var arms = null
var torso = null
var legs = null
var hands = null
var weapon = null
var offhand = null
# lists
var friends = []
var active_quests = []
var quests_completed = []
# networking
var ping = null
func _init():
pass
# Constants
const DEFAULT_PORT = 4500
const SERVER_IP = "127.0.0.1"
const MAX_CLIENTS = 600
const DEFAULT_USERNAME = "Anonymouse"
# Client (local/controllable) player instance
var myPlayer = null
# Names for remote players in id:name format
var players = {}
# Signals to let lobby GUI know what's going on
signal player_list_changed()
signal connection_failed()
signal connection_succeeded()
signal game_ended()
signal game_error(what)
func create_server():
# Create server code.
var host = NetworkedMultiplayerENet.new()
host.create_server(DEFAULT_PORT, MAX_CLIENTS)
get_tree().set_network_peer(host)
func create_client():
# Create client code.
var host = NetworkedMultiplayerENet.new()
host.create_client(SERVER_IP, DEFAULT_PORT)
get_tree().set_network_peer(host)
# other code was too large to fit into thread body
And the script that handles the actual connections is "MainMenu.gd":
extends TextureFrame
var CAN_TRY_LOGIN = true
var login_timer = Timer.new()
var timer_callback_amount = 0
func _ready():
login_timer.connect("timeout", self, "_on_login_timer")
login_timer.set_wait_time(1)
add_child(login_timer)
# hook onto signals emitted from gamestate.gd
gamestate.connect("connection_failed", self, "_on_connection_failed")
gamestate.connect("connection_succeeded", self, "_on_connection_success")
# connect to server
get_node("/root/gamestate").create_client()
func _on_connection_failed():
# Called when client fails to connect to server
get_node("WaitIndicator1/TextureProgress").set_value(0)
get_node("WaitIndicator1/Description").set_text("Failed to connect to server.")
get_node("WaitIndicator1/Description").add_color_override("font_color", Color(255, 0, 0))
get_node("WaitIndicator1/Description").set_pos(Vector2(25, 14))
get_node("WaitIndicator1/TextureProgress").hide()
get_node("WaitIndicator1/Sprite").show()
login_timer.stop()
CAN_TRY_LOGIN = true
func _on_connection_success():
# Called when client connects to server successfully.
check_user_credentials()
# stop timer, and reset instance variable(s).
login_timer.stop()
CAN_TRY_LOGIN = true
func check_user_credentials():
# Check if user an account, and credentials are valid.
var username = get_node("VBoxContainer/LineEdit").get_text()
var password = get_node("VBoxContainer/LineEdit2").get_text()
print("CONNECTED:", username, password)
func login_successful():
# Player logged into server successfully.
get_node("/root/globals").setScene("res://resources/scenes/CharacterSelection.tscn")
func _on_login_timer():
update_description()
func update_description():
# Update log-in progress text on dialog, every second.
var indicator = "."
var description = get_node("WaitIndicator1/Description")
var current_value = description.get_text()
if timer_callback_amount == 3:
current_value = current_value.replace(".", "")
timer_callback_amount = 1
else:
timer_callback_amount += 1
var next_value = current_value + indicator
description.set_text(next_value)
func _on_HButtonArray_button_selected( button_idx ):
# Handles main menu button clicks
if CAN_TRY_LOGIN == true:
var LOGIN = 0
var REGISTER = 1
if button_idx == LOGIN:
var username = get_node("VBoxContainer/LineEdit").get_text()
var password = get_node("VBoxContainer/LineEdit2").get_text()
if username.length() > 0 and password.length() > 0:
get_node("WaitIndicator1").show()
login_timer.start()
CAN_TRY_LOGIN = false
else:
pass
else:
get_node("/root/globals").setScene("res://resources/scenes/Registration.tscn")
else:
pass
func _on_TextureButton_pressed():
# Quit game.
get_tree().quit()
func _on_ServerButton_pressed():
# Start server button.
var screen_size = OS.get_screen_size(0)
var window_size = Vector2(1024, 600)
get_node("/root/globals").setScene("res://resources/scenes/Server.tscn")
OS.set_window_fullscreen(false)
OS.set_window_maximized(false)
OS.set_window_size(window_size)
OS.set_window_position(screen_size*0.5 - window_size*0.5)
When the client is ran, it immediately attempts to establish a connection with the server, however when the user clicks on "Start Server" (button) it runs gamestate.create_server() function. And that's how I'm testing to see if i can establish a connection between my client and server (at least for now).