Where does the object go to?

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Georgie
Posts: 22
Joined: 09 Dec 2010, 23:10

Where does the object go to?

Postby Georgie » 19 Dec 2010, 14:52

If i send this from the server

send("add", resObj, sender);

How do i then get the resObj in unity?
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 19 Dec 2010, 17:22

I dont think thats really a valid use of the send method.

What you do is e.g.

smartFox.Send( new ExtensionRequest("MyExt", parameters) );
or
smartFox.Send( new JoinRoomRequest("Lobby") );

So you send one of the request classes

/Thomas
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 19 Dec 2010, 17:22

in the OnExtensionResponse handler :

Code: Select all

   private void OnExtensionResponse(BaseEvent evt) {
      try {
         string cmd = (string)evt.Params["add"];
         ISFSObject dt = (SFSObject)evt.Params["params"];
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 19 Dec 2010, 18:29

Argh - very true. I should slow down when reading. You said from server->client and not client->server. Sorry.

OnExtensionResponse is the right answer yes. Dont forget to subscribe to the callback.

/Thomas
Georgie
Posts: 22
Joined: 09 Dec 2010, 23:10

Postby Georgie » 19 Dec 2010, 21:00

ThomasLund wrote:Argh - very true. I should slow down when reading. You said from server->client and not client->server. Sorry.

OnExtensionResponse is the right answer yes. Dont forget to subscribe to the callback.

/Thomas


he he, yes I got it from the docs :D
Georgie
Posts: 22
Joined: 09 Dec 2010, 23:10

Postby Georgie » 19 Dec 2010, 21:21

Is there anything else I need to do?

this codes not being executed

public void OnExtensionResponse(BaseEvent evt) {

string cmd = (string)evt.Params["cmd"];
SFSObject dataObject = (SFSObject)evt.Params["params"];

Debug.Log(cmd);
}

I'm managing to send to the server and do a trace, so I know that much is working, but the debug statment for the returned values is not being triggered.
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Postby tchen » 20 Dec 2010, 00:31

Checklist:

Did you include

Code: Select all

m_sfs.AddEventListener(SFSEvent.EXTENSION_RESPONSE, OnExtensionResponse);


somewhere during init?
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 20 Dec 2010, 06:53

you need to register for the callback so the API knows where to send the message.
Georgie
Posts: 22
Joined: 09 Dec 2010, 23:10

Postby Georgie » 20 Dec 2010, 08:30

Yeah sorry I had missed that.

Is there a way to send different events from the server to different functions?

Like one for "add" one for "subtract"? or do they all have to go to 1 function and use logic?

--

Also server side, is there a short hand way to do SEND to all users in room etc?

One more

Is there a list of all users in the room ready built in? if so how do i access it?
dragagon
Posts: 19
Joined: 05 Dec 2010, 00:23

Postby dragagon » 20 Dec 2010, 16:23

You can do one of two things, either you can add multiple if/else blocks so like

Code: Select all

public void OnExtensionResponse(BaseEvent evt) {

    string cmd = (string)evt.Params["cmd"];
    SFSObject dataObject = (SFSObject)evt.Params["params"];

    if(cmd == "add")
    {
       // add
    }
    else if(cmd == "subtract")
    {
     //subtract
    }
    Debug.Log(cmd);
}

Or you can do like my blog tutorial does and sets up a handler.

Code: Select all

        try
        {
            string cmd = (string)evt.Params["cmd"];
            ISFSObject dt = (SFSObject)evt.Params["params"];

            ExtensionHandler handler;

            if (handlers.TryGetValue(cmd, out handler))
            {
                handler(dt);
            }
            else
            {
                Debug.LogError("Got unhandled cmd: " + cmd);
            }
        }
        catch (Exception e)
        {
            Debug.Log("Exception handling response: " + e.Message + " >>> " + e.StackTrace);
        }


My personal preference is to use the handler so that I don't have a giant list of message types.
appels
Posts: 464
Joined: 28 Jul 2010, 02:12
Contact:

Postby appels » 20 Dec 2010, 17:13

yep, you can send anything and use cmd to extract them on the client side in the OnExtensionResponse handler.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 20 Dec 2010, 19:23

Handler pattern is my personal favorite too. And it will also be "similar" to how things look on the server. So double bonus of less cluttered code and less differences on both ends

Return to “SFS2X C# API”

Who is online

Users browsing this forum: No registered users and 56 guests