client:myRoomActors()

I have been playing around with the photo_corona_plugin_sample trying to learn how things work. 

In the sample I added some simple code to view the current actors in a room. 

local textObject

--------   Added

function getActors()

    local  me= client:myActor()

    print ("ME I’m Player # "…me.actorNr)

    local   actors=client:myRoomActors()

    print ("Players Count "…#actors)

   for i = 1 , #actors do

    print ("My Actors count = "…actors[i].actorNr)

   end 

end

local actorsBut= display.newText(“Actors”, 10, 200, native.systemFont, 18)

actorsBut:addEventListener(“tap”,getActors)


if display then

 I also added a this :

function client:onActorJoin(actor)

   print (“Actor Joined”…actor.actorNr)

end

  Sometime everything works fine , other times I get a new actor joined message but when I go to display how many actors are in the room I get 0. Here’s my last run:

 Actor Joined6       —client:onActorJoin() 

 ME I’m Player # 4    --client:myActor().actorNr

 Players Count 0       —  #client:myRoomActors()

I don’t understand why myRoomActors does not return at least 1 actor when an actor just joined. 

i have been using the simulator and a device to test this sample. I removed the max send and receive  so the program just keeps running.  I would connect with both and then close one down. Start it up again and reconnect. Do this a couple of times and that’s where I got myActor.actorNr= 4 .  How does one remove an actor from the room so that the player count remains accurate? 

If I join first I should be actor #1 .

Someone else joins they are actor #2

 Actor #2 leaves .

Someone else joins. They should be assigned actor#2 , not actor#3, 

Any help would be great. 

 Thanks

Chris

myRoomActors() returns table of actors with actor numbers as keys. That table is not a lua ‘array’. So you can not use ‘length’ operator (#) on it.

Instead, you should count elements in table:

function client:getMyRoomActorsCount() local count = 0 for actorNr, actor in pairs(self:myRoomActors()) do count = count + 1 end return count end 

Please refer http://www.lua.org/manual/5.1/manual.html#2.5.5 on ‘length’ operator usage. In short, it’s useful only for tables with numeric keys 1,2,3… (lua ‘arrays’)

Thank you so much. That did the trick .  I did want to ask if there was a way to reset the actorNr for an actor?  

 Actor Joined 3    

 ME I’m Player # 1    

 Actor # 1    

 Actor # 2    

 Actor # 3    

 Count of Actors 3    

 Actor Left 2    

 ME I’m Player # 1    

 Actor # 1    

 Actor # 3    

 Count of Actors 2

I would like the next actor to join be assign to actorNr=2 but the next person to join will be actorNr=4

The game I am working on will have two opposing sides.  I was planning on using odd vs even but that won’t work. If someone leaves the game and jumps back in they have effectively changed sides and I would like to have the game as balanced as possible. 

 Any idea how I can do this?

Chris

You can’t affect the order in which IDs are assigned. 

Also, to level teams after someone left, the game would have to re-assign IDs, which would make things unreasonably complex.

The IDs are fixed but you can assign a property per player or use 2 room properties: teamA and teamB, each listing the players in it.

If I setCustomerProperty to an Actor will that property be available when I call myRoomActors() ?

What I want to do is set a custom property like  myActor:setCustomProperty(playerNum=1)  when the user creates a room. When other actors join they would call myActor:myRoomActors() .  I want new players to be able to tell what playerNum the other actors are to determine what playerNum they should be .  Does that make sense?

 Chris

Set actor properties before room creation or join:

client1:myActor():setCustomProperty("playerNum", 1) client1:createRoom(...) ... client2:myActor():setCustomProperty("playerNum", 2) client2:joinRoom(...)

Each joined client will have myRoomActors() list with those properties:

for actorNr, actor in pairs(client:myRoomActors()) do local num = actor:getCustomProperty("playerNum") end 

Note: by design client can change actor properties while connected, but current sdk version has bug and doesn’t update others clients actors list. This will be fixed soon. Currently you can rely only on actor properties set initially,

Thank you for such a concise response. Any idea how soon that bug might get fixed?  I can find a work around but having this ability would really make things smooth.   Does that mean the call to onActorPropertiesChange(actor) does not get called?

Chris

Plugin with fixed actor setCustomProperty() is available now.

All actors properties in myRoomActors() list are updated on every joined client as any actor properties get changed.

onActorPropertiesChange(actor) is called on every joined client on each such change.

myRoomActors() returns table of actors with actor numbers as keys. That table is not a lua ‘array’. So you can not use ‘length’ operator (#) on it.

Instead, you should count elements in table:

function client:getMyRoomActorsCount() local count = 0 for actorNr, actor in pairs(self:myRoomActors()) do count = count + 1 end return count end 

Please refer http://www.lua.org/manual/5.1/manual.html#2.5.5 on ‘length’ operator usage. In short, it’s useful only for tables with numeric keys 1,2,3… (lua ‘arrays’)

Thank you so much. That did the trick .  I did want to ask if there was a way to reset the actorNr for an actor?  

 Actor Joined 3    

 ME I’m Player # 1    

 Actor # 1    

 Actor # 2    

 Actor # 3    

 Count of Actors 3    

 Actor Left 2    

 ME I’m Player # 1    

 Actor # 1    

 Actor # 3    

 Count of Actors 2

I would like the next actor to join be assign to actorNr=2 but the next person to join will be actorNr=4

The game I am working on will have two opposing sides.  I was planning on using odd vs even but that won’t work. If someone leaves the game and jumps back in they have effectively changed sides and I would like to have the game as balanced as possible. 

 Any idea how I can do this?

Chris

You can’t affect the order in which IDs are assigned. 

Also, to level teams after someone left, the game would have to re-assign IDs, which would make things unreasonably complex.

The IDs are fixed but you can assign a property per player or use 2 room properties: teamA and teamB, each listing the players in it.

If I setCustomerProperty to an Actor will that property be available when I call myRoomActors() ?

What I want to do is set a custom property like  myActor:setCustomProperty(playerNum=1)  when the user creates a room. When other actors join they would call myActor:myRoomActors() .  I want new players to be able to tell what playerNum the other actors are to determine what playerNum they should be .  Does that make sense?

 Chris

Set actor properties before room creation or join:

client1:myActor():setCustomProperty("playerNum", 1) client1:createRoom(...) ... client2:myActor():setCustomProperty("playerNum", 2) client2:joinRoom(...)

Each joined client will have myRoomActors() list with those properties:

for actorNr, actor in pairs(client:myRoomActors()) do local num = actor:getCustomProperty("playerNum") end 

Note: by design client can change actor properties while connected, but current sdk version has bug and doesn’t update others clients actors list. This will be fixed soon. Currently you can rely only on actor properties set initially,

Thank you for such a concise response. Any idea how soon that bug might get fixed?  I can find a work around but having this ability would really make things smooth.   Does that mean the call to onActorPropertiesChange(actor) does not get called?

Chris

Plugin with fixed actor setCustomProperty() is available now.

All actors properties in myRoomActors() list are updated on every joined client as any actor properties get changed.

onActorPropertiesChange(actor) is called on every joined client on each such change.