004. Foray into Photon - Part 03 Operations

It took me a while to understand what Photon meant when they said Events and Operations. I ran across their glossary and it says this:

Event - Events are asynchronous messages sent to clients. They can be triggered by operations (as sideeffect) or raised (as main purpose of an operation) and are identified by an event code. The origin is identified as ActorNumber.

Operation - Another word for RPC functions on the Photon server side. Clients use operations to do anything on the server and even to send events to others.

So in Unity3D we send operations to the server, and we receive events from the server and then sender receives an OperationResponse which tells us what happened according to the server. This can be a simple OK message meaning it was successful, or it could be an error with something like the operation cannot be processed.

So there is one change we need to make on the server to return to us a message so we know the server got our request. If you open up AegisBornPeer the return statement of OnOperationRequest should be changed to:

            return new OperationResponse(operationRequest, 0, "Ok", new Dictionary<short, object> { {100, "We get signal." } });

Build the server and then head back into unity3d. In the OnGUI of Login we add this line under the first button:

            if (GUI.Button(new Rect(200, 60, 100, 30), "Send Operation"))
            {
                engine.sendOp();
            }

With that in place, jump over to Game and lets create the function.

    public void sendOp()
    {
        peer.OpCustom(100, new Hashtable(), true, 0);
    }

This creates an Operation, gives it an OpCode of 100 and sends no data. It sends it reliably and uses channel 0. Channels are like telephone lines. Lets say you have 3 channels. each channel operates independently of each other. All events and operations on a channel are guaranteed to be in order.

Lastly we need to do something when we get our OperationResult. I chose to read through the return values for an item numbered 100 and display the string contained there:

     public void OperationResult(byte opCode, int returnCode, System.Collections.Hashtable returnValues, short invocID)
    {
        var debug = (string)returnValues[(byte)100];

        status = debug;
    }

This concludes this section. if you run the code, you can click the connect button to connect to the server and then send an operations which returns an OperationResult that contains the message "We get signal". This concludes this small section. Next we will look at using the OperationDispatcher to handle multiple operations without having to use a giant switch statement.

Comments

I was getting an error with new dictionary in Visual Studio 2010, so I did some poking around on photons examples and found out I had to add ' <short, object> ' after dictionary to remove the error.

{
return new OperationResponse(operationRequest, 0, "Ok", new Dictionary <short, object> { {100, "We get signal."} });
}

I am new to coding so I hope this is correct, but it did clear the error and allowed for a clean server build.

Nemo

Yeah, sorry about that, if i miss a generic in c# the internet thinks it's a tag, i've corrected it.

Kotzu's picture

for this return i get error too

return new OperationResponse(operationRequest, 0, "Ok", new Dictionary { {100, "We get signal." } });

Error:

Error 1 The type or namespace name 'Dictionary' could not be found (are you missing a using directive or an assembly reference?) PATH\Photon Server\deploy\GameName\GameNamePeer.cs 27 73 GameName

what should i do? :(

Kotzu's picture

i kinda fixed the error and visual studio let me build after i set that part to "using System.Collections.Generic;

may ruin everything?

No, you need collections.Generic, because your code should be like the previous poster OperationResponse(operationRequest, 0, "Ok", new Dictionary<short, object> { {100, "We get signal." } });

Kotzu's picture

now i get
Assets/Scripts/Game.cs(68,21): error CS0111: A member `Game.OperationResult(byte, int, System.Collections.Hashtable, short)' is already defined. Rename this member or use different parameter types

after i add this in game.cs
public void OperationResult(byte opCode, int returnCode, System.Collections.Hashtable returnValues, short invocID)
{
var debug = (string)returnValues[(byte)100];

status = debug;
}

It sounds like you have the same function twice, check the file and make sure you only have this function listed once.

Kotzu's picture

Oh, ok, i will, do u have a YM/Skype ID? something to ease the posting and w8ting for response?