Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Client-server app: some problems

Discussion in 'Multiplayer' started by Simone, Sep 22, 2008.

  1. Simone

    Simone

    Joined:
    May 15, 2008
    Posts:
    5
    Hello again,
    i'm going on experimenting with unity's networking facilities and i'm quite happy with them, so far.

    For now i have a uber-easy client-server app. My game server broadcasts himself to the master server, the client connects to MS and then correctly to my GS.

    Then on the client i call an RPC @server, passing username and password. The server has a custom class to store the NetworkPlayer and all the other info i need.

    A class method uses WWWForm (along with yield) to check if user/pass are ok. I'd like to call this method from the RPC function, wait for him to finish and then go ahead with the RPC code.


    The problem is that if i try to call it using StartCoroutine(objectHash[index].myRoutine()), it works; if i try to call it using yield, it doesnt.


    What's wrong?

    Can i use yield inside an RPC function?
    Can i yield a custom object class method??
    Why i get two different behaviours with these two tecniques?
     
  2. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,304
    I'm not exactly sure I understand what you're trying to do and what the problem is, but maybe you're running into the same issue I've been running into, which is:

    You can't (easily) call CoRoutines from other CoRoutines. Of course, you can start another CoRoutine, but what I frequently run into is that I need the second CoRoutine to "block" the first one. So if that's what you're trying to do with yield, the solution you'll probably have to live with looks like this:

    Code (csharp):
    1.  
    2. IEnumerator otherCoRoutine = SomeOtherCoRoutine();
    3. while (otherCoRoutine.MoveNext()) { yield return otherCoRoutine.Current; }
    4.  
    It took me while to figure this out, and it's a bit cumbersome to use, but it works and allows you to create CoRoutines that call other CoRoutines (which allows much nicer software-design in many cases).

    Yeah, but the RPC needs to be declared with an IEnumerator return-type (at least in C#).

    I guess that was the question I hopefully previously answered ;-) ... you can, but it's a bit involved. Keep in mind, though, that CoRoutines are really kind of a magical thing. Before this enumerator-stuff was created, programming stuff like this was a real hell ;-)

    You refer to StartCoroutine(MyCoRoutine()) vs yield return MyCoRoutine() (which doesn't work), respectively the construct I'm using above? If so: StartCoroutine(...) "kind of" creates a new thread of control (it's not a real thread, but kind of behaves like that, with some exceptions). The other approach just uses the language features so that "control" is kept in the original method.

    Sunny regards,
    Jashan, who can't really express himself today ;-)
     
  3. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    How about starting the coroutine from the RPC function, and then putting all the time dependent code into the coroutine. Once its done, you could send an ACK RPC back to the client - I'm assuming after you verify the credentials you send message to the P2P client.
     
  4. Simone

    Simone

    Joined:
    May 15, 2008
    Posts:
    5
    Well.. you got my point: cant call a coroutine from a coroutine.. and you got the solution! Thanks!! :)

    It looks like black-magic-code for me.. but.. who cares?


    For future javascript users that piece of code becomes something like:
    Code (csharp):
    1. var otherCoRoutine : IEnumerator  = someCoroutine(parameters);
    2. while (otherCoRoutine.MoveNext()) {
    3.    yield otherCoRoutine.Current;
    4. }
    5.  

    Thanks also to shaun, your suggestion is exactly what i'm going to do.. just after the otherCoroutine returns!
     
  5. shaun

    shaun

    Joined:
    Mar 23, 2007
    Posts:
    728
    Usually simpler is better :) Good luck.