Windows C++ SDK syntax questions

kblanch09
edited October 2014 in Native
I have a couple questions regarding syntax for the windows C++ sdk:

1.)I'm giving certain clients 2 peers that both connect to photon. I need to be able to differentiate which peer is getting calling the listener functions. Right now I have the below code creating the peers, but using the same listener.
mLitePeer_Lobby = new ExitGames::Lite::LitePeer(*this);
mLitePeer = new ExitGames::Lite::LitePeer(*this);
How do I create listeners for the peers? I tried the below code, but cannot instantiate an abstract class:
PhotonListener* mListener = new PhotonListener();
mLitePeer_Lobby = new ExitGames::Lite::LitePeer(*mListener);
I was able to create a workaround using state variables, but it would be a lot better to be able to differentiate using which listener called the function.


2.)I'm pretty sure that you can send an event to multiple clients by sending an array of the client actor numbers in the opRaiseEv function. But when I do, only the first client receives the event. I checked the server logs and indeed only one even is being sent (using the photon lite server):
int arrayTargets[10] = {1, 2, 3, 4};
mLitePeer_Lobby->opRaiseEvent(true, ev, EventCode::EV_DATA_RECEIVED, 0, ExitGames::Lite::EventCache::DO_NOT_CACHE, arrayTargets, 1);
I was able to create a workaround using a simple for loop, but wanted to see if the syntax was wrong.

Comments

  • Hi kblanch09.

    1.)
    PhotonListener is an ABC, an abstract base class. This is a fundamental C++ concept, that is explained for example here: http://www.learncpp.com/cpp-tutorial/12 ... e-classes/
    As the compiler rightfully complains, you CAN NOT instantiate an ABC, but you MUST derive from it, implement all inherited pure virtual functions in the derived class and then instantiate the derived class.

    2.)
    The parameter after "arrayTargets" is the array size. You are passing "1" for the size of the array, so of course only the first element of the array will be sent.
  • Thanks for the reply!

    1.) So i'm looking at the advanced demo where they create their own listener.
    Listener* pListener = new Listener(*this);
    pLitePeer = new ExitGames::Lite::LitePeer(*pListener);
    
    Would copying that code twice create two separate listeners even though they would both be using *this?
    Listener* pListener1 = new Listener(*this);
    pLitePeer1 = new ExitGames::Lite::LitePeer(*pListener1);
    Listener* pListener2 = new Listener(*this);
    pLitePeer2 = new ExitGames::Lite::LitePeer(*pListener2);
    
    Or would I still need to contruct it using something other than *this?

    2.)Thanks! Didn't realize there was an array size parameter in the function
  • The Listener class in demo_advanced takes a reference to its owner. This is not a requirement of the PhotonListener interface, but a design decision of the demo.
    If you don't need access to the owner from within that Listener instance, then just don't have such an parameter in your Listener class at all. If you need to access an owning class from within the Listener (useful to avoid global variables and still be able to access other classes - for example your Listener could call different functions on it's owner for different operation responses, but the real code to react to the responses would be implemented within the owner, not in the Listener, so that the listener can stay agnostic about the existence of every other class in the application except for it's owner), then your exact needs will also determine which class and which instance of that class will be the owner, but the most common case is that the instance that owns the listener also creates that listener.
    This still holds true in the scenario of multiple listeners:
    One owning instance to rule them all, One owning instance to find them,
    One owning instance to bring them all and in the darkness bind them.

    So I recommend to only let different classes own the different listeners, if you actually have a need for different owners.
  • The whole purpose for have multiple listeners is because I have 2 peers for one client. They both join separate rooms and communicate with other peers in there.
    So, for example, when I get an event, I want to be able to know which peer the event is for so that I can handle it properly. Since both peers call the onEvent listener function, I need a way of differentiating the two. After talking with Stefen, having two sepearate listeners seemed like the best way. So it would look something like this:
    if(this == pListener_Lobby)
    ; // this is the lobby listener, so it got called by the lobby peer
    else
    ; // this is the "normal" listener, so it got called by "normal" peer
    }
    
  • If the owner needs to know from which of it's listeners it is getting called, then give the functions that the listeners are supposed to call an according parameter, through which the listener can identify itself, like an unique id or a pointer to the listener, so that the listener can pass 'this'.