RoomMasterActor()

Hi, this is a little help that can help. ; )

the function reports if the client that connects is considered the Master client of the room.

Master Client (from photonengine)

Master Client is a “special” client per room. In absence of custom server code, it can be made responsible for handling logic that should only be executed by one client in a room (e.g. starting a match when everyone is ready). A new Master Client is automatically assigned when the former leaves. Unless explicitly set, the Master Client is the actor (player) with the least actor number among the active actors.

Starts from example found in photon SDK (photon_corona_plugin_sample) where you create the class: client

local client = LoadBalancingClient.new(appInfo.ServerAddress, appInfo.AppId, appInfo.AppVersion)

function client:checkInit()
    --return "S" slave or "M" master
    local nrMaster = 0
    local result = "S"

    if self:isJoinedToRoom() then
      nrMaster = self:myRoomMasterActorNr()
      
      for actorNr, actor in pairs(client:myRoomActors()) do
        if actor.actorNr == nrMaster and actor.isLocal == true then
          --I'm master
          result = "M"
          break
        else
          --I'm slave
        end
      end
    end
    
    return result
end

I think is better call also other 2 instances to have updated the state

function client:onActorJoin(actor)
self:checkInit()
end

function client:onActorLeave(actor)
self:checkInit()
end

I think the code should be better: avoid the ‘for’ loop.
But I’m not able to write a better one.

If someone can help is appreciate.

Renato

I just arrived (I’m growing up):

replace the form loop with

  if client:myRoomActors()[self:myRoomMasterActorNr()].isLocal == true then
      result = "M"
  end

et voilà!

Renato

There is no better way, please consider this:

be careful that the number of players in the room does not match the number of each player.

For example:
-> enter 2 players
[1] actor.actorNr 1 name Player 1
[2] actor.actorNr 2 name Player 2

in that case the code above will works

<-- exit player1
–> enter player3

you will have 2 actors on rom
[1] actor.actorNr 2 name Player 2
[2] actor.actorNr 3 name Player 3

so the myRoomMasterActorNr() will give you value 3 but on table you will find on row 2

Renato

1 Like