Uploading/Downloading an int array using ES2Web

Post Reply
User avatar
Joel
Moodkie Staff
Posts: 3041
Joined: Wed Nov 07, 2012 10:32 pm

Uploading/Downloading an int array using ES2Web

Post by Joel »

Uploading an int array using ES2Web

In this simple tutorial we will show you how to upload an int[] to the ES2 MySQL database on your server.

We will create a method which accepts an int[] as a parameter, and automatically saves it to web when called. Will will also create a method which loads an int[] from the web and saves it to a file.

Requirements
Creating our Upload method

First, create a method which accepts our int[] array, and a string which we will use to identify this particular array when saving/loading. If using C#, you should specify a return type of IEnumerator as this method will be a coroutine, so it doesn't freeze the game whenever we're downloading/uploading.

Inside the method, we do the following:
  • Create an ES2Web object using the URL to our ES2.php file as the parameter. The URL should have a 'tag' parameter, which will be set to the tag supplied as the method's parameter.
  • We then start our Upload routine with our int[] as the parameter, using yield StartCoroutine to start the coroutine.
  • When finished, we use the ES2Web's 'isError' field to check if there are any errors which we need to handle. A list of error codes can be found HERE.
C#
public IEnumerator Upload(int[] intArray, string tag)
{
    string myURL = "http://www.server.com/ES2.php?tag=" + tag;
    ES2Web web = new ES2Web(myURL);
     
    // Start uploading our data and wait for it to finish.
    yield return StartCoroutine(web.Upload(intArray));
     
    if(web.isError)
    {
        // Enter your own code to handle errors here.
        // For a list of error codes, see the ES2Web Error Codes Page.
        Debug.LogError(web.errorCode + ":" + web.error);
    }
}
Javascript
function Upload(intArray : int[], tag : String)
{
    var myURL : String = "http://www.server.com/ES2.php?tag=" + myTag;
    var web : ES2Web = new ES2Web(myURL);
      
    // Start uploading our data and wait for it to finish.
    yield StartCoroutine(web.Upload(intArray));
      
    if(web.isError)
    {
        // Enter your own code to handle errors here.
        // For a list of error codes, see the ES2Web Error Codes Page.
        Debug.LogError(web.errorCode + ":" + web.error);
    }
}
Creating our Download method

Now we must create a method which accepts two strings; one will be the 'tag' we used to save, and the other will be the file we want to create to save our loaded data to. Again, if using C#, you should specify a return type of IEnumerator.

Inside the method, we do the following:
  • Create an ES2Web object using the URL to our ES2.php file as the parameter. Again, the URL should have a 'tag' parameter, which will be set to the tag supplied as the method's parameter.
  • We then start our Download routine, using yield StartCoroutine to start the coroutine.
  • When finished, we use the ES2Web's 'isError' field to check if there are any errors which we need to handle. A list of error codes can be found HERE.
  • Finally we use the ES2Web's Save() method to save the data to our file.
C#
public IEnumerator Download(string tag, string file)
{
    string myURL = "http://www.server.com/ES2.php?tag=" + tag;
    ES2Web web = new ES2Web(myURL);
     
    // Start uploading our data and wait for it to finish.
    yield return StartCoroutine(web.Download());
     
    if(web.isError)
    {
        // Enter your own code to handle errors here.
        // For a list of error codes, see the ES2Web Error Codes Page.
        Debug.LogError(web.errorCode + ":" + web.error);
    }
   // Now save our data to file so we can use ES2.Load to load it later.
   web.Save( file );
}
Javascript
function Upload(tag : String, file : String)
{
    var myURL : String = "http://www.server.com/ES2.php?tag=" + myTag;
    var web : ES2Web = new ES2Web(myURL);
      
    // Start uploading our data and wait for it to finish.
    yield StartCoroutine(web.Download());
      
    if(web.isError)
    {
        // Enter your own code to handle errors here.
        // For a list of error codes, see the ES2Web Error Codes Page.
        Debug.LogError(web.errorCode + ":" + web.error);
    }
   web.Save( file );
}
Using the methods we created

Now we've created our methods, we can use them as follows:

C#
/* To create an int[] and upload it */

int[] intArray = new int[]{1,2,3,4,5};
StartCoroutine( Upload(intArray, "myTag") );

/* And then to download this int[] at a later time and assign it back to our intArray variable */

StartCoroutine( Download("myTag", "saveFile.txt"));
intArray = ES2.LoadArray<int>("saveFile.txt?tag=myTag");
Javascript
/* To create an int[] and upload it */

var intArray : int[] = new int[]{1,2,3,4,5};
StartCoroutine( Upload(intArray, "myTag") );

/* And then to download this int[] at a later time and assign it back to our intArray variable */

StartCoroutine( Download("myTag", "saveFile.txt"));
intArray = ES2.LoadArray.<int>("saveFile.txt?tag=myTag");

Post Reply