[Photon] RPC with yield functions + Arguments + bonus question!

Hey!

So I’m working on the network of my game and I encountered a problem (not a big one because I have a solution, but I’m asking for something more… clean, you know!) :

I have a function like this:

public IEnumerator FunctionA() {
   // Some code here
   yield return StartCoroutine(FunctionB());
   // Some code there
}

Something really easy as you can see. The problem is that PhotonView.RPC returns void so I can’t call it using StartCoroutine. The only solution I found it’s to do that:

public IEnumerator FunctionA() {
   // Send to the other clients that I called the FunctionB
   PhotonView.RPC("FunctionB", PhotonTargets.Others);
   // Then do it for myself
   yield return StartCoroutine(FunctionB());
}

And I’m not sure it’s the right way… but it works! Do you have any other solutions to solve this? I mean, I don’t want to create a variable and check, by using “while”, if it is true or false, blabla. I don’t want this:

public IEnumerator FunctionA() {
   // At the beginning of the function, set _wait to TRUE
   PhotonView.RPC("FunctionB", PhotonTargets.All);

    // Wait until FunctionB set _wait to FALSE
    while (_wait) {
        yield return null;
    }
}

I don’t even know if it’s gonna work, because I have to go through the server. So, if I want this code works, I have to set the variable _wait to true inside the FunctionA and not FunctionB. Great!


My second issue (and more important) is I can’t send my own classes (which inherits from MonoBehaviour). I have a class named “AObject” which represents every object that the player can interact with (other players, buildings, etc.).

public IEnumerator FunctionA() {
   AObject obj = getTarget(...);

   PhotonView.RPC("FunctionB", PhotonTargets.All, obj);
}

Unity tells me : “‘AObject’ can not be serialized”. So how I can do that ?


Bonus question: before I started using Unity3D to create my game, I was working on my own server (from scratch) with RakNet (C++). It works and I just need to create all RPCs (some are already created, like the connection).

Is it possible to use this server with Photon in Unity3D ? I don’t know what I’m gonna receive on my server if I send something through Photon/Unity3D.

Thank you!!

PS: Sorry for my english hopefully it’s OK.

Sbizz.

The method that “is” the RPC could be a coroutine. If you use PhotonView.RPC(“coroutinename”, PhotonTargets.All), it should be run as coroutine locally and on the other clients.
At least, that’s how it should work. Let me know if it doesn’t.
Don’t call PhotonView.RPC() in a coroutine! If you know something has to run each frame, just trigger the start/end of that routine by RPCs. In best case: Only start the coroutine as RPC and let it end with a fixed timing or trigger.

You can’t send classes. If you have to send some values from your own classes, you have to register them as Custom Type first and write de/serialization methods for the class. Check out CustomTypes.cs to see how we do that for Vector3.
If you simply want to target a specific object, the PhotonView will do that for you. RPCs (and OnPhotonSerializeView) will always target a specific networked object anyways.

Bonus answer: You can use the Photon Server SDK if you need server-side logic but you can’t combine PUN with other servers. The protocol and logic is not built to be re-implemented on top of another networking.

And as sugar coating on top: Your English is just fine! :slight_smile:

1 Like

Hi tobiass. Thank for your answer.

Hmm. If I understand well : currently, FunctionA and FunctionB are both coroutines. FunctionA calls FunctionB (RPC) and FunctionA waits until FunctionB ends to continue what it has to do. What you’re saying is FunctionA shouldn’t be a coroutine because it executes a RPC ? I’m gonna check how I can do that.
It’s a spell cast system, so it’s a bit hard to get what I want without using coroutines… everywhere. Maybe I should see with delegates or events. Here is the “real” code:

void OnClick() {
    if (/*some conditions*/) {
        StartCoroutine(playerCastSpell(spell));
    }
}

IEnumerator playerCastSpell(Spell spell) {
    AObject target = getTarget(...);

    if (/*check if target is NOT ok*/) {
        yield break;
    }

    if (/*check spell conditions are NOT ok*/) {
        yield break;
    }

    // character.getPhotonView().RPC("channeling", PhotonTargets.Other, spell.spellName);
    // Channel the spell for himself
    StartCoroutine(character.channeling());

    if (/*everything went well*/) {
        // character.getPhotonView().RPC("cast", PhotonTargets.All, target);
    } else {
        // Otherwise..
    }
}

But I’m gonna check if I can do something good with delegates, I think it should be OK and more clean.

And for the other answers, I’m gonna check this evening, I’m at the office right now. The only thing about the classes is: how I can get the good target ? I mean, AObject is a component so… Do I have to send the object ID and get the good object using a list ? Hmm.

Thanks again!
Sbizz.

So I managed to do what you said: I created my own Coroutine class. It’s the same as StartCoroutine, but I can know the state of the coroutine (running, paused and finished) AND I use the delegate keyword to call a function when the coroutine ends. Here is the code:

using UnityEngine;
using System.Collections;

public class MyCoroutine  {
    public enum eState {
        NONE,
        RUNNING,
        PAUSED,
        FINISHED
    }

    // callback function type
    public delegate void callbackType();

    // Function which will be called when the coroutine ends
    public callbackType    callbackEnd;

    // State of the coroutine
    private eState            _state;

    // Coroutine currently running
    private IEnumerator        _coroutine;

    public MyCoroutine(IEnumerator coroutine) {
        _coroutine = coroutine;
        _state = eState.NONE;
    }

    // start() will launch the coroutine the first time
    // it is called. The other calls will unpause the
    // coroutine if it was paused
    public Coroutine start(MonoBehaviour mono) {
        if (_state == eState.NONE) {
            return mono.StartCoroutine(launch());
        } else if (_state == eState.PAUSED) {
            _state = eState.RUNNING;
        }

        return null;
    }

    public void stop() {
        _state = eState.FINISHED;
    }

    public void pause() {
        _state = eState.PAUSED;
    }

    private IEnumerator launch() {
        _state = eState.RUNNING;

        while (_coroutine.MoveNext()) {
            yield return _coroutine.Current;

            while (_state == eState.PAUSED) {
                yield return null;
            }
          
            if (_state == eState.FINISHED) {
                yield break;
            }
        }

        // Call the callback function if it was set
        if (callbackEnd != null) {
            callbackEnd();
        }

        _state = eState.FINISHED;
    }
}
    void Start() {
        MyCoroutine co = new MyCoroutine(coroutineFunction());

        co.callbackEnd = functionCallback;
        co.start(this);
    }

It works well.

I still need to find a solution if I wanna send my own classes through the RPC functions.

Sbizz.

What do you want to send? As said, if you want to send values of some instance of your classes, use Custom Types as we do in CustomTypes.cs in the package. Just register your type according to the blueprint there and you should be able to send your data.

I found a solution (I worked late yesterday, I forgot to tell you about that :stuck_out_tongue_winking_eye: Sorry!)

Okay, all my “selectable objects” have a component called “AObject”. They also have a component PhotonView. Conclusion: they all have a photonView variable and so a viewID.

My problem was the following: when a player cast a spell, the function must take 2 parameters: the spell (ASpell) and the target (AObject). I was able to replace ASpell by the spellName (string) and find the ASpell within a list. But I didn’t want to replace the AObject by a Vector3, because if the targeted player moves, the spell has to follow him. So I created a private static dictionary variable inside AObject and in the “Start” function I added the current object with his viewID and in the OnDestroy function I removed it. Now, I just have to send the viewID instead of the AObject.

It works, but I’m gonna look after CustomTypes, it will be more clean to do that, I guess.

Sbizz.

If you got it running this way, don’t change to CustomTypes. They might be “more clean” technically but are some overhead for the de/serialization.
If you know the target anyways, you could call the RPC directly on it’s PhotonView. The RPC will fire on said target object.
Of course, the usual concurrency issues make this more tricky: The networked object may become destroyed and the destroy message may arrive on the server before the RPC. Then, the RPC will not properly fire.
If the target is not a networked object, this will not work apparently.

So maybe it’s not worth the effort to refactor.

Glad you found it. Thanks for the update.