Search code examples
swiftgame-centermultiplayer

Accessing Player Attributes From GKMatch


I am making a hide and seek game using Game Center. I have set up two roles: hider and seeker.

class Role
{
    static let Seeker : UInt32 = 0xFFFF0000
    static let Hider : UInt32 = 0xFFFF0000
}

I am setting the playerAtrributes field of my GKMatchRequest.

gkMatchRequest.playerAttributes = Role.Seeker | Role.Hider

Here is the question: How can I tell which player is which role?


Solution

  • Check out this excellent tutorial (Not Swift, but the concepts are the same.)

    Basically what you do is the following:

    1. When the match starts send them both to a waiting stage where they generate a random number.
    2. Send the random number to the other person.
    3. Check if your random number is higher then set them as the Seeker or hider, and if it is lower do the opposite. The other player does the same comparison, so they should get the opposite event. In the event of a tie (by some rare chance) repeat the process.

    Now you have a player that is set as the hider and seeker. Just save a variable saying what they are, and you are done.