5 – Networking behaviors

PRO users now have access to networking behaviors which opens up new options for game and app creation!

You can now add asynchronous multiplayer to games! This allows for turn-based gameplay between two or more participants. Gaming is just the start. These behaviors also allow you to create client/server apps, store data remotely or sync with your own data server.

These are the new behaviors are some examples of things you can do with them.

Note: These behaviors are currently only available in Mac Creator. They will be added to Windows Creator in version 14.

 

Getting Started

 

Before you can access Network behaviors, you must complete two steps in your project.

  • Make sure you are signed in with a PRO account in Creator.
  • On the Project info (home) screen, make sure you choose “My own server” from the “multiplayer” selection list.

You should now have a new category for network behaviors in the Library.

 

Usage

 

Network Send Table To URL :

Sends a table, encoded as a JSON string, to a web server.

 

Parameters:

URL: target url for the data you are sending

Table: the table you want to send

Callback Attribute: an integer attribute used to determine if the command succeeded.

As the command is asynchronous, you can’t immediately know if the command worked or not. The attribute is initialized to 0. If the command succeeds, it will be set to 1, and if it fails, it will be set to -1. The callback attribute must be set, if it is left blank, the behavior will not run.

The server will receive a POST message, with the JSON string as a parameter named “params”, and a SHA1 checksum of the JSON string as a parameter named “sig”.

The JSON format is:

 
{
 "Children": [
  {
    "Children": [],
    "Name": "<table id>_headers",
    "Properties": [
      {
        "Name": "rowCount",
        "Value": <number of rows>
      },
      {
        "Name": "columnCount",
        "Value": <number of columns>
       },
       {
         "Name": "0-<column number>-name",
         "Value": "<column name>"
       },
       {
         "Name": "0-<column number>-type",
         "Value": <column type>
       },
     ]    },
   {
     "Children": [],
     "Name": "<table id>",
     "Properties": [
       {
         "Name": "<row number>",
         "Value": "<row name>|<cell 1>|<cell 2>|<etc>|"
       },
     ]    }
 ],
 "Name": "",
 "Properties": [] }

 

Here’s an example:

 

 
{
 "Children": [
   {
     "Children": [],
     "Name": "id940081_headers",
     "Properties": [
       {
         "Name": "rowCount",
         "Value": 2
       },
       {
         "Name": "columnCount",
         "Value": 3
       },
       {
         "Name": "0-1-name",
         "Value": "c1"
       },
       {
         "Name": "0-1-type",
         "Value": 1
       },
       {
         "Name": "0-2-name",
         "Value": "c2"
       },
       {
         "Name": "0-2-type",
         "Value": 2
       },
       {
         "Name": "0-3-name",
         "Value": "c3"
       },
       {
         "Name": "0-3-type",
         "Value": 4
       }
     ]    },
   {
     "Children": [],
     "Name": "id940081",
     "Properties": [
       {
         "Name": "1",
         "Value": "r1|abc|123|False|"
       },
       {
         "Name": "2",
         "Value": "r2|def|456|True|"
       }
     ]    }
 ],
 "Name": "",
 "Properties": [] }

 

If the server receives the JSON data successfully, it should return the following JSON string:

{“Status”:”Success”}

If it fails, it should return:

{“Status”:”Fail”}

For reference, the “column type” values are:

1 = string
2 = integer
3 = real
4 = boolean
5 = angle

 

Network Get Table From URL:

Retrieves a JSON encoded string from a web server, and converts it to table data

 

Parameters:

URL: source url of the data you are getting

Table: the table to fill in with the data

Callback Attribute: an integer attribute used to determine if the command succeeded.

As the command is asynchronous, you can’t immediately know if the command worked or not. The attribute is initialized to 0. If the command succeeds, it will be set to 1, and if it fails, it will be set to -1. The callback attribute must be set, if it is left blank, the behavior will not run.

The server will receive a GET message, and is expected to return the JSON-encoded table data. If you wish to return a failed state, return an empty string.

The JSON data should have the same format as that sent via Network Send Table To URL. Note the table id must match the id of the table being written to.

 

Here is an example PHP server script:

If it receives the POST from Network Send Table To URL, it saves the JSON data in /var/sandbox/params.txt, and the SHA1 checksum in /var/sandbox/sig.txt. If it recieves the GET from Network Get Table From URL, it returns the saved params.txt data.
<?php
           error_reporting(E_ALL);
           if (!empty($_POST))
           {
                       $paramsFile = '/var/sandbox/params.txt';
                       $sigFile = '/var/sandbox/sig.txt';
                       $sig = $_POST["sig"]; // this is the SHA1 checksum
                       $params = $_POST["params"]; // This is the Json data you can work with
                       $result = file_put_contents($paramsFile, $params);
                       $result = file_put_contents($sigFile, $sig);
                       echo '{"Status":"Success"}';
                       // if there is a failure, return '{"Status":"Fail"}'
           }
           else
           {
                       $paramsFile = '/var/sandbox/params.txt';
                       $result = file_get_contents($paramsFile);
                       if ($result === FALSE)
                       {
                                   echo '';
                       }
                       else
                       {
                                   echo $result;
                       }
           }
?>