Accessing returned values in SFSOBJECT

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

MaxHammer
Posts: 9
Joined: 07 Jul 2010, 23:37
Location: Kingston, Ontario, Canada
Contact:

Accessing returned values in SFSOBJECT

Postby MaxHammer » 30 Aug 2013, 04:53

I'm attempting to read from a MySQL table from within a sfs extension, which works as I've viewed the hex dump and the dump contains the data I expect:

Here's my server side code:

Code: Select all

        IDBManager dbManager = getParentExtension().getParentZone().getDBManager();   
        String sql = "SELECT NAME AS FROM USERS WHERE ID = 2";
        ISFSObject response = new SFSObject();
        ISFSArray res = dbManager.executeQuery(sql, new Object[]{});   
           
        // Populate the response parameters
        response.putSFSArray("name", res);
           
        // Send it back to requester
        send("ReturnScore", response, user);


In Unity3D i'm trying to get at the data that resides in the SFSOBJECT. Here's my non-functioning code:

Code: Select all

   void OnExtensionResponse(BaseEvent e)
   {
      string cmd = (string) e.Params["cmd"];
      ISFSObject objIn = (SFSObject)e.Params["params"];
      
      if (cmd == "name")
      {
         Debug.Log(objIn.GetHexDump());
         IList str = objIn.GetUtfStringArray("name");
         string str2 = str.ToString();
         Debug.Log(str2);   
      }
   }


I tried using an ICollection already. The above code tries to use the Ilist. The .GetHexDump() function was very helpful to assist me in troubleshooting the actual db connection and query to ensure that everything was working as desired.

All that's left to do is actually 'access' the data that's been sent!

Thanks in advance! It's 12:25am and after many hours of scratching my head (and using my good pal Google) I can say I'm stumped.

MaxHammer
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Accessing returned values in SFSOBJECT

Postby Lapo » 30 Aug 2013, 08:28

Have you tried the GetDump() method? It will show you the exact structure of the object with all the types.

Anyways the query returns an Array of database rows, minimum 1 row if the query succeeds. So, first thing to do is checking if the array has size() > 0. If not your DB query return nothing.

Then you either grab the one and only row in the Array or you loop through it. Each element will be an SFSObject and will have as keys the names of the fields in the database.

To be honest, looking at this specific case it seems like your just extracting one piece of information from the DB, the name of the user given his/her ID.
In this case I would probably recommend to extract the result on the server side and just send it to the client instead of sending the whole nested SFSObject -> SFSArray -> SFSObject returned by the query.

In any case your code doesn't work because it's not extracting the SFSArray contained in the top SFSObject you have sent as a response.
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Justinzeaidew and 52 guests