Defold and Colyseus Debugging

Hello, I thought I would post a little bit of the work I have been doing for my team. In this case allowing the running of builds and html5 builds from the Defold editor whilst still connecting to a Colyseus server.

If you just build the game as is, then most configurations won’t have the right server address to connect to. Since my team has a number of members, who are all remote, we needed a way to easily determine what the server address should be, and connect to that for the Colyseus functionality.

The key code is the following, which I will breakdown below:

-- CONNECTION VARS
local server_address = "localhost"
local server_port = "3553"
local server_location = ""
local cookie_data = ""
local info = sys.get_sys_info()
local cookie_table = {}
function Webpage.get_server_location()
	server_location = ""
	if (info.system_name == "HTML5") then
		server_location = html5.run("location.protocol.replace('http', 'ws') + '//' + window.document.location.host.replace(/:.*/, '') + (location.port ? ':' + location.port : '');")
		if string.find(server_location,"localhost",1,true) then
			server_location = sys.load_resource("/dev/arcade_ip.txt")
		end
	else
		server_location = sys.load_resource("/dev/arcade_ip.txt")
	end
	return server_location
end

Firstly we need to determine if the build is a HTML5 build or not, which is done from the sys.get_sys_into() function:

local info = sys.get_sys_info()

From here the system build is determined by:

info.system_name

If this is “HTML5” then we know we can run javascript code to determine the current url we are at.

server_location = html5.run("location.protocol.replace('http', 'ws') + '//' + window.document.location.host.replace(/:.*/, '') + (location.port ? ':' + location.port : '');")

The Javascript here is 100% stolen from @endel’s code example for Colyseus. Now since our games won’t be run from the host computer except during testing we know if that if the resulting name contains “localhost” it’s a test by one of the developers. From here I’ll detail how we setup a custom resource in game.project, although you can read more about it here, here and here.

You need to previously know the debug server’s address, in my particular case it’s a local computer at “192.168.0.20” ip address. Hence I have created a folder in the bundle root folder called “dev” which contains a file called “arcade_ip.txt”. This file only has a single line “ws://192.168.0.20/”.

In game.project in the text box “Custom Resources” I have entered “/dev/arcade_ip.txt” which gives access to that file in the build and bundles.

After you have done all that you can access the file from within your HTML5 or other builds through:

server_location = sys.load_resource("/dev/arcade_ip.txt")

Which means that in your non-HTML5 builds or in HTML5 builds where you have “localhost” in the server name, you can use your dev file to determine the ip address.

Now one of the first lines which are run in my game is:

local server_location = Webpage.get_server_location()

To grab me the proper server location for what kind of build is currently happening.

I hope this helps someone, let me know if it’s unclear at any point.

Cheers,
Spen

8 Likes

Thanks for sharing! I’m sure it will be helpful for remote teams.

You could also just check:

if html5 then

The html5 namespace will not exist in any other environment than an HTML5 build of Defold.

3 Likes