[SmartFox] Sending objects
[ August 10, 2004 ] by Marco Lapi, a.k.a Lapo
Article 8: in this tutorial you will learn how to send Actionscript structured data in a multiuser/multiplayer application or game



One of the interesting and usefull feature in SmartFoxServer Lite is the ability to send and receive complex data
structures (arrays, objects ...) very easily and transparently.

In this article we'll show you how this feature can really speed up the process of creating complex multiuser applications and games.

[ DATA STRUCTURES ]
In the previous tutorial we concentrated our attention on the "User Variables" and we've learnt how to use them.
"User variables" are very usefull when you want to store settings for each client connected and have all the other users
receive notices about changes in those values.

There are cases in which this approach is not the best one, for example in a turn-based game.
Classic turn based games can be board games (chess, connect 4 ...), role play games, card games, strategy games etc...

Every time a user makes a move in a "connect 4" game or in a "chess" game we don't really need to store that move as a "User Variable". We would just need to be able to send the move data to the other clients, without storing it anywhere.
One more example can be a multiuser whiteboard application: anytime someone draws something on it we should just send the information about the lines and curves that were drawn in a convenient format and again no need to save that data.

The sendObject() method of the SmartFoxClient API allows us to do this very easily, check this example:

move = new Object()
move.px = 100
move.py = 200
move.color = "red"
 
sendObject(move)

That's all :-)

The other clients will receive an onObjectReceived event:

smartfox.onObjectReceived = function(obj, user)
{
// do something cool here
}

where obj is the object received and "user" is the User object of the sender.

The following list represents the type of variables that can be sent through the sendObject() method:

» object
» array
» number
» string
» boolean
» null

All the other variable types are ignored.

To give you an example that is a little more complex, imagine you're playing an rpg game and you have to send some data
about you character:

myChar = new Object()
myChar.name = "Tarkus"
myChar.position = {px: 100, py:100}
myChar.inventory = new Array("knife", "short sword", "long sword", "hammer", "shield")
 
sendObject(myChar)

As you can see you can nest other objects and arrays and create pretty complex data structures.

In the next tutorial we'll use this feature to implement our first simple multiplayer game.


[ WHAT'S BEHIND ]
It is not the purpose of this tutorial to show how this mechanism works, however I'd like to discuss the basics of it in order to better understand what's going on and how to get the best out of it.

The process of sending and receiving structured Actionscript objects is done through a serialization/deserialization mechanism.

To tell it in simple words your AS object is transformed in a more convenient format (xml in our case) and sent to the server which
in turn will transmit the received data to the other users. As soon as the client API receives the serialized data it will re-construct
the object and make it available to the application.

It's always important to take care of the amount of data that you're sending and receiving. The more complex and nested is the structure of your objects the more time it will take to send it through the network and if you exceed with informations your application may not respond as expected.

If your application/game is targeted to broadband users then this should not be much of an issue, however slower modem connections have limited bandwidth (4 to 7 KB/s) and you will also have to deal with the inevitable lag of internet connections.

Also if you waste bandwidth by sending unnecessary data you will steal bandwidth to other users connected to the server and if you
saturate it the general performance of the clients connected will deteriorate.

The previous rpg game example should not give problems to slower modem connections, however it can be optimized efficiently by
replacing those strings in the inventory array with numeric ids.

myChar = new Object()
myChar.name = "Tarkus"
myChar.position = {px: 100, py:100}
myChar.inventory = new Array(1, 2, 3, 4, 5)
 
sendObject(myChar)

where 1,2,3,4,5 represent the various inventory items.

You can save about 30 bytes by using this new object. Doesn't sound like it's so much ?
Well think about having 100 connected users and you're saving 3KB/s (24Kb/s) of bandwith and if the users were 1000 you
would be saving 240kb/s ... that's almost half of a DSL connection, not that bad! :-)

Now that we have seen the theory behind sending and receiving Actionscript data through the SmartFoxServer it's time to put
this new knowledge to work in a real world example.

In the next tutorial you will find a complete simple game of Tic-Tac-Toe.

Have fun! :-)

Lapo


    
 
 
Name: Marco Lapi, a.k.a Lapo
Location: Fossano, Italy
Age: 34
Flash experience: started out with Flash 4 back in 1999
Job: web designer/developer
Website: http://www.gotoandplay.it/
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048