Datastreams

From GMod Wiki

Jump to: navigation, search
Lua: Datastreams
Page white text.png Description:Teaches how to use the new datastream module
link=User:LuaBanana Original Author:LuaBanana
Group.png Contributors:TGP1994
Calendar.png Created:24th January 2009, last edited 2nd March 2010

Contents

What are they

Datastreams are much like usermessages except they allow for nonuniform sending of many data types. Not only can you send data to the client from the server, but you can send data to the server from the client. The datastream module is powered off of Deco da Man's GLON module, of which is available in Garry's Mod as well.

How do they work?

Examples

Client to Server

On the client:

datastream.StreamToServer( "TestHook", { ["text"] = "Hello world! The answer to everything is", ["number"] = "42", ["bool"] = true } );

On the server:

function GM:AcceptStream( pl, handler, id )
 
    return true; // Allow all streams
 
end
 
function TestHook( ply, handle, id, encoded, decoded )
 
    print( decoded.text );
    print( decoded.number );
    print( decoded.bool );
 
end
datastream.Hook( "TestHook", TestHook );
 

Server Output:

Hello world! The answer to everything is
42
true

Server to Client

On the client:

function TestHook( handle, id, encoded, decoded )
 
    print( decoded.text );
    print( decoded.number );
    print( decoded.bool );
 
end
datastream.Hook( "TestHook", TestHook );
 

On the server:

 
datastream.StreamToClients( player.GetAll( ), "TestHook", { ["text"] = "Hello world! The answer to everything is", ["number"] = "42", ["bool"] = true } );
 

Client Output:

Hello world! The answer to everything is
42
true
 

As you can see, the code is nearly identical in both ways. The main difference is that the client doesn't have to specify who he is streaming to and thus does not need player objects as it's first argument.

See Also

Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox