Difference between revisions of "Multiplayer"

From GiderosMobile
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]][[File:Platform linux.png]]<br/>
 
'''Supported platforms:''' [[File:Platform android.png]][[File:Platform ios.png]][[File:Platform mac.png]][[File:Platform pc.png]][[File:Platform html5.png]][[File:Platform winrt.png]][[File:Platform win32.png]][[File:Platform linux.png]]<br/>
  
=== Description ===
+
=== Multiplayer ===
 
There are three ways to implement multiplayer games:
 
There are three ways to implement multiplayer games:
 
* '''Server Mode''': Dedicated server with public IP
 
* '''Server Mode''': Dedicated server with public IP
 
* '''Host Mode''': One player is the host, all other players connect to them
 
* '''Host Mode''': One player is the host, all other players connect to them
 
* '''Shared Mode''': Cloud Room has StateAuthority
 
* '''Shared Mode''': Cloud Room has StateAuthority
 +
  
 
''Server Mode'' and ''Shared Mode'' can be daunting and won't be covered here. Some multiplayer platforms SDK you can check:
 
''Server Mode'' and ''Shared Mode'' can be daunting and won't be covered here. Some multiplayer platforms SDK you can check:
Line 18: Line 19:
  
  
Here we will cover the ''Host Mode'' using '''Gideros Unite Framework''' written by Arturs Sosins (@'''ar2rsawseen'''), which is the easiest way to get started with multiplayer.
+
=== Host Mode ===
 +
Host Mode is the easiest way to get started with multiplayer games. In order to implement it in Gideros, we will use the '''Gideros Unite Framework''' written by Arturs Sosins (@'''ar2rsawseen''').
  
=== Gideros Unite Framework ===
+
==== '''[[Gideros Unite Framework]]''' ====
<youtube>https://www.youtube.com/watch?v=WBFLSz34I4I</youtube>
+
The Gideros Unite framework allows a host (server) and clients to connect over a Local Area Network.
 
 
Gideros Unite framework provides a way to implement Multiplayer games. It uses LuaSocket to establish socket connections and even create server/client instances.
 
 
 
It provides the means of device discovery in Local Area Network, and allows to call methods of other devices through network.
 
 
 
About protocols, it is possible to use tcp, udp or both (by binding some method to tcp, if reliability is needed, and others to udp for faster data processing).
 
 
 
==== Unite Package '''[[Media:Unite.zip|Unite.zip]]''' ====
 
==== Gideros application project '''[[Media:DrawTogetherV2.zip|DrawTogetherV2.zip]]''' ====
 
  
 
Some demo code:
 
Some demo code:
Line 62: Line 55:
  
 
-- and when game is finished
 
-- and when game is finished
 +
serverlink:close()
 +
</syntaxhighlight>
 +
 +
'''Server example code'''
 +
<syntaxhighlight lang="lua">
 +
function onAccept(e)
 +
-- auto accept client with provided id
 +
serverlink:accept(e.data.id)
 +
end
 +
 +
-- create a server instance
 +
serverlink = Server.new({username = 'myServer'})
 +
-- add event to monitor when new client wants to join
 +
serverlink:addEventListener('newClient', onAccept)
 +
-- start broadcasting to discover devices
 +
serverlink:startBroadcast()
 +
 +
-- and then before entering game logic
 +
-- if we are ready to play stop broadcasting
 +
serverlink:stopBroadcast()
 +
-- and start only listening to clients
 +
serverlink:startListening()
 +
</syntaxhighlight>
 +
 +
'''Client example code'''
 +
<syntaxhighlight lang="lua">
 +
function onJoin(e)
 +
-- auto connect to server with provided id
 +
serverlink:connect(e.data.id)
 +
end
 +
 +
-- create client instance
 +
serverlink = Client.new({username = 'IAmAClient'})
 +
-- create event to monitor when new server starts broadcasting
 +
serverlink:addEventListener('newServer', onJoin)
 +
 +
-- event to listen if server accepted our connection
 +
serverlink:addEventListener('onAccepted', function()
 +
print('server accepted our connection')
 +
end)
 +
</syntaxhighlight>
 +
 +
'''Game logic example'''
 +
The game logic is the same for server and clients
 +
<syntaxhighlight lang="lua">
 +
-- we can get all devices that are connected to our network
 +
local devices = {}
 +
serverlink:addEventListener('device', function(e)
 +
print(e.data.id, e.data.ip, e.data.host)
 +
devices[e.data.id] = {}
 +
devices[e.data.id].ip = e.data.ip
 +
devices[e.data.id].name = e.data.host
 +
end)
 +
serverlink:getDevices()
 +
 +
-- add some methods, that could be called by other clients or server through network
 +
-- draw something
 +
serverlink:addMethod('draw', self.drawLine, self)
 +
-- end drawing
 +
serverlink:addMethod('end', self.stopDrawing, self)
 +
-- clear drawing
 +
serverlink:addMethod('clear', self.reset, self)
 +
 +
-- then you can call these methods when needed
 +
serverlink:callMethod('clear')
 +
serverlink:callMethod('draw', someX, someY)
 +
 +
-- or call method of specific device using its id
 +
serverlink:callMethodOf('clear', 112233)
 +
serverlink:callMethodOf('draw', someX, someY, 112233)
 +
 +
-- when game is finished
 
serverlink:close()
 
serverlink:close()
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
{{GIDEROS IMPORTANT LINKS}}
 
{{GIDEROS IMPORTANT LINKS}}

Latest revision as of 07:53, 14 December 2023

Supported platforms: Platform android.pngPlatform ios.pngPlatform mac.pngPlatform pc.pngPlatform html5.pngPlatform winrt.pngPlatform win32.pngPlatform linux.png

Multiplayer

There are three ways to implement multiplayer games:

  • Server Mode: Dedicated server with public IP
  • Host Mode: One player is the host, all other players connect to them
  • Shared Mode: Cloud Room has StateAuthority


Server Mode and Shared Mode can be daunting and won't be covered here. Some multiplayer platforms SDK you can check:

And to get you started using Noobhub:


Host Mode

Host Mode is the easiest way to get started with multiplayer games. In order to implement it in Gideros, we will use the Gideros Unite Framework written by Arturs Sosins (@ar2rsawseen).

Gideros Unite Framework

The Gideros Unite framework allows a host (server) and clients to connect over a Local Area Network.

Some demo code:

-- we can get all devices that are connected to our network
local devices = {}
serverlink:addEventListener("device", function(e)
	print(e.data.id, e.data.ip, e.data.host)
	devices[e.data.id] = {}
	devices[e.data.id].ip = e.data.ip
	devices[e.data.id].name = e.data.host
end)

serverlink:getDevices()

-- add some methods, that could be called by other clients or server through network
-- draw something
serverlink:addMethod("draw", self.drawLine, self)
-- end drawing
serverlink:addMethod("end", self.stopDrawing, self)
-- clear drawing
serverlink:addMethod("clear", self.reset, self)

-- then you can call this methods when needed
serverlink:callMethod("clear")
serverlink:callMethod("draw", someX, someY)

-- or call method of specific device using its id
serverlink:callMethodOf("clear", 112233)
serverlink:callMethodOf("draw", someX, someY, 112233)

-- and when game is finished
serverlink:close()

Server example code

function onAccept(e)
	-- auto accept client with provided id
	serverlink:accept(e.data.id)
end

-- create a server instance
serverlink = Server.new({username = 'myServer'})
-- add event to monitor when new client wants to join
serverlink:addEventListener('newClient', onAccept)
-- start broadcasting to discover devices
serverlink:startBroadcast()

-- and then before entering game logic
-- if we are ready to play stop broadcasting
serverlink:stopBroadcast()
-- and start only listening to clients
serverlink:startListening()

Client example code

function onJoin(e)
	-- auto connect to server with provided id
	serverlink:connect(e.data.id)
end

-- create client instance
serverlink = Client.new({username = 'IAmAClient'})
-- create event to monitor when new server starts broadcasting
serverlink:addEventListener('newServer', onJoin)

-- event to listen if server accepted our connection
serverlink:addEventListener('onAccepted', function()
	print('server accepted our connection')
end)

Game logic example The game logic is the same for server and clients

-- we can get all devices that are connected to our network
local devices = {}
serverlink:addEventListener('device', function(e)
	print(e.data.id, e.data.ip, e.data.host)
	devices[e.data.id] = {}
	devices[e.data.id].ip = e.data.ip
	devices[e.data.id].name = e.data.host
end)
serverlink:getDevices()

-- add some methods, that could be called by other clients or server through network
-- draw something
serverlink:addMethod('draw', self.drawLine, self)
-- end drawing
serverlink:addMethod('end', self.stopDrawing, self)
-- clear drawing
serverlink:addMethod('clear', self.reset, self)

-- then you can call these methods when needed
serverlink:callMethod('clear')
serverlink:callMethod('draw', someX, someY)

-- or call method of specific device using its id
serverlink:callMethodOf('clear', 112233)
serverlink:callMethodOf('draw', someX, someY, 112233)

-- when game is finished
serverlink:close()