Sending a request for data from within Unity

Post here all your questions related with SmartFoxServer .Net/Unity3D API

Moderators: Lapo, Bax

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

Sending a request for data from within Unity

Postby MaxHammer » 11 Sep 2010, 00:55

Hello. I've made it quite far without having to ask any questions, but after a few nights of research I'm at a loss.

I'm using Unity3D, and am building off the Islands Demo.

I have sfs installed and running.

I have mysql installed and running.

I created a database and a table named USERS.

All I would like to be able to do (at this point) is be able to read a value from the mysql database from within Unity3d.

For example, when a player presses the 'G' key, I would like to get the record count of the table USERS. Something fairly easy.

I read Chapter 8 of the sfs docs and understand what I need to do. My problem is implementing it on the client side in Unity3d.

What I've tried to do is detect for the key press, and then execute this command:

smartFox.sendXtMessage(extensionName, "getData", {}, "xml");

Then of course I would modify this command to take the appropriate actions:

smartfox.onExtensionResponse = function(resObj:Object, type:String)
{
...
}


Do I need to write the script in C# or JavaScript?

If someone can help me out with this, then the sky's the limit.

All I'm looking for is a simple nudge in the right direction.

Thanks,

Kelly
Kingston, Ontario
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 11 Sep 2010, 05:55

OK - if I understand the problem correctly, then I think you need to do this:

* User press G
* Unity client code reads keypress with e.g. Input.GetKeyDown("g");
* Unity client sends off a sfs.SendXtMessage() to request data

* SmartFoxServer extension is triggered and fires the getData method
* SFS extension opens db connection and does a select count(*) from Users;
* SFS extension packs up result as a extension response and labels the response with an id=getDataResponse
* SFS extension sends off the resoponse to the calling client

* Unity client gets a OnExtensionResponse callback
* Unity client checks what type it is (the id=getDataResponse)
* Unity client does something with the count

So - to the question of what language to use - thats up to you. In Unity its the standard - C#/unityscript/boo. For SFS its Java/AS3 (and some 3rd option too?).

Hope that helps

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

Postby MaxHammer » 11 Sep 2010, 09:53

Thanks Thomas!

I'm going to work at this for the next few days. I'll let you know how it goes.

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

Postby MaxHammer » 12 Sep 2010, 13:18

Well, I am making progress, but I feel kind of foolish because it's not working 100% and I know the solution is probably something very simple.

Note that all other aspects of SFS work. I am able to connect to zones and rooms with multiple users. The only issue I am having difficulty with is receiving a response from my extension.

I created a new extension based on simpleExt.as. It is called "simp" in config.xml code.


Here is what I have working so far:

1. I call smartFox.SendXtMessage()
2. A msg to the Unity console window confirms this was called
3. The simp extension receives the msg
4. the SFS log confirms that the extension received the msg
5. Unity does not get a call back message from the extension.


SFS Configuration

Code: Select all


<Zone name="CitySim" uCountUpdate="true" buddyList="20" maxUsers="4000" customLogin="false">
<Rooms>      
<Room name="Kingston" maxUsers="50" isPrivate="false" isGame="false" isTemp="false" />
</Rooms>
         
<Extensions>
<extension name="simp" className="simpleExt.as" type="script" />
</Extensions>
         
<Moderators status="on">
<Mod name="modName" pwd="modPass" />
</Moderators>
</Zone>






Client Side
I've modified the original LoginGUI.cs script that ships with the Island Demo. Essentially, I'm simply calling the smartFox.SendXtMessage() function when the user logs in.

Code: Select all


void OnGUI() {

   GUI.skin = gSkin;
   GUI.Label(new Rect(2, -2, 680, 70), "", "SFSLogo");   

   if (!connectionAttempt) {
      GUI.Label(new Rect(10, 116, 100, 100), "IP: ");
      serverIP = GUI.TextField(new Rect(100, 116, 200, 20), serverIP, 25);
         
      GUI.Label(new Rect(10, 136, 100, 100), "Port: ");
      serverPort = GUI.TextField(new Rect(100, 136, 200, 20), serverPort, 25);
         
      if (GUI.Button(new Rect(100, 166, 100, 24), "Connect")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) {
         connectionAttempt = true;
         smartFox.Connect(serverIP, Convert.ToInt32(serverPort));
      }
      
      }
      else if (smartFox.IsConnected()) {
         // MySQL database test here...
         Debug.Log("Sending message to simp extension");
         smartFox.SendXtMessage("simp", "DataRequest", null, SmartFoxClient.XTMSG_TYPE_STR);   
         
         // Login
         GUI.Label(new Rect(10, 116, 100, 100), "Username: ");
         username = GUI.TextField(new Rect(100, 116, 200, 20), username, 25);

         GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);

         if ( GUI.Button(new Rect(100, 166, 100, 24), "Login")  || (Event.current.type == EventType.keyDown && Event.current.character == '\n')) {
            smartFox.Login("CitySim", username, "");  // This SHOULD be "zone", but I needed to hard code it to work         
         }

      } else {
         GUI.Label(new Rect(10, 150, 100, 100), "Waiting for connection");
         GUI.Label(new Rect(10, 218, 100, 100), loginErrorMessage);
      }
   }





Server Side

I created a new extension based on the simpleExt.as script. Very simple, it should just send a response.

Code: Select all

function handleRequest(cmd, params, user, fromRoom)
{
   trace("Received data request from client")
   var response = {}
   response._cmd = "LogOK"
   
   _server.sendResponse(response, -1, null, [user])   
}



Back to the Client Side

Again, this function is very generic and should simply send a message to the Unity Console if the simp extension sent the response.

Code: Select all

   public void OnExtensionResponse(object data, string type) {
        // Response
        Debug.Log("Response from Server");
    }



So I know that this is ALMOST working... Unity sends the request, the Server receives the request, but Unity is not receiving the response.

The issue is either the server is not sending the message, or the LoginGUI script is not receiving the response.

Thanks again for any help!

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

Postby MaxHammer » 12 Sep 2010, 22:44

It sure helps to read and read and read some more...

I came across this thread viewtopic.php?t=7435 which was answered by Kiyaku and shows me that I was missing a vital piece of code.

I was never adding the OnServerResponse listener.

Once I added the following line of code, my client script started receiving the responses from the Server.

Code: Select all

SFSEvent.onExtensionResponse += OnExtensionResponse;


This is starting to get pretty cool.

Kelly
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 13 Sep 2010, 05:50

Perfect - sometimes its the small details that matter :-D

Good luck
Fishypants
Posts: 57
Joined: 26 Oct 2010, 14:03
Location: South Pasadena California
Contact:

Postby Fishypants » 05 Dec 2010, 07:13

Hey MaxHammer, I am trying to do the same thing that you are, but I still cannot get this to work. Doesn't seem like Unity is actually sending anything.

I have smartfox up and running, and the extension appears loaded, but I'm not sure why unity is not calling the extension . . . Any ideas?

I believe my code looks like yours, but if necessary I can post what I have to help track down why this isn't working.
Fishypants
Posts: 57
Joined: 26 Oct 2010, 14:03
Location: South Pasadena California
Contact:

Postby Fishypants » 05 Dec 2010, 07:55

BAH! of course RIGHT after I post this I figure it out. My extension name was not the same as the file name, it was slightly different so it couldn't figure out what I was talking about.

file name: mcdb.as
extension name was: mcDb

changing it to the same as the file name (minus the .as extension) made magic happen, and I am a happy camper. :D

Return to “.Net / Unity3D API”

Who is online

Users browsing this forum: No registered users and 20 guests