Fandom

CraftStudio Wiki

Scripting Reference/CraftStudio.Network

< Scripting Reference

56pages on
this wiki
Add New Page
Comment1 Share

(This is documentation for an upcoming feature, it's not final nor usable)

Network play in CraftStudioEdit

Networking lets you make your games work over the Internet so that they can be played together by multiple players. Although made as simple as possible, networking will likely affect most aspects of your game so it's best to build it in along with your game rather than as an afterthought.

See also the NetworkSync component documentation.

Technical detailsEdit

Networking in CraftStudio uses the UDP protocol for communication.

CraftStudio.Network.Server.StartEdit

CS.Network.Server.Start( [number] port=CS.Network.DefaultPort )

Starts accepting connections on the specified port number (defaults to 4233).

You'll need to manually connect to your server locally with CS.Network.Connect (passing "127.0.0.1" as the hostname) if you want to treat your local game as just another network player.

This is a server-side function.

CraftStudio.Network.Server.StopEdit

CS.Network.Server.Stop()

Disconnects all players and stops listening to incoming connections.

This is a server-side function.

CraftStudio.Network.ConnectEdit

CS.Network.Connect( [string] hostname, [number] port=CS.Network.DefaultPort, [function] callback=nil )

Opens a connection to the specified hostname (or IP) on the specified port number.

When the connection has been established, the specified callback function will be called.

This is a client-side function.

CraftStudio.Network.DisconnectEdit

CS.Network.Disconnect()

Closes any open connection.

This won't call your OnDisconnected handler.

This is a client-side function.

CraftStudio.Network.OnDisconnectedEdit

CS.Network.OnDisconnected( [function] callback )

Defines which function to call (if any) when a disconnection happens. You can pass nil to disable it.

This is a client-side function.

CraftStudio.Network.Server.OnPlayerJoinedEdit

CS.Network.Server.OnPlayerJoined( [function] callback )

Defines which function to call (if any) when a player has joined. You can pass nil to disable it.

The callback will be passed a table containing player info with the following keys:

  • id: a unique ID attributed to this player
  • name: the player's name (alphanumeric)
  • authenticated: a boolean stating whether the player is authenticated against the master server or not

This is a server-side function.

Example: Printing the player's ID when they joinEdit

function PlayerJoinHandler( player )
    print( "Player with ID " .. player.id .. " has joined!" )
end

CraftStudio.Network.Server.OnPlayerLeftEdit

CS.Network.Server.OnPlayerLeft( [function] callback )

Defines which function to call (if any) when a player has left (or been disconnected). You can pass nil to disable it.

The callback will be passed the disconnected player's ID.

This is a server-side function.

Example: Printing the player's ID when they leaveEdit

​function PlayerLeftHandler( playerId )
    print( "Player with ID " .. playerId .. " has left!" )
end

CraftStudio.Network.Server.DisconnectPlayerEdit

CS.Network.Server.DisconnectPlayer( playerId, reason="" )

Disconnects the specified player, optionally specifying a reason as text that will be passed to the player's OnDisconnected handler.

CraftStudio.Network.RegisterMessageHandlerEdit

CS.Network.RegisterMessageHandler( [function] handler, [CS.Network.Side] server or players side )

Registers a function as callable on the players (client-side) or the server (server-side). Message handler functions must be registered before they can be used to respond to messages.

Possible values for the second parameter are CS.Network.Side.ServerCS.Network.Side.Players or CS.Network.Side.Any.

Example: Registering a server message handlerEdit

​function Behavior:DoSomethingFun( data, playerId )
    print( "Got a DoSomethingFun message from player with ID " .. playerId )
end
CS.Network.RegisterMessageHandler( Behavior.DoSomethingFun, CS.Network.Side.Players )

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.