example-channel-information using Pub NUb

Hi,
I am trying the example mentioned and it is getting real time errors. Has anyone experimented with this example? I have not changed it at all. Here is the error and the code:

[code]
Runtime error
…4eb5/lua-corona/example-channel-information/main.lua:22: attempt to index global ‘my_player_id’ (a nil value)
stack traceback:
[C]: ?
…4eb5/lua-corona/example-channel-information/main.lua:22: in function <…4eb5>
(tail call): ?
(tail call): ?
Runtime error
…4eb5/lua-corona/example-channel-information/main.lua:63: table index is nil
stack traceback:
[C]: ?
…4eb5/lua-corona/example-channel-information/main.lua:63: in function ‘callback’
…b5/lua-corona/example-channel-information/pubnub.lua:115: in function <…b5>
(tail call): ?
Number of Players in channel: game_room_1234
0
[code]
require "pubnub"

multiplayer = pubnub.new({
publish_key = “demo”,
subscribe_key = “demo”,
secret_key = nil,
ssl = nil,
origin = “pubsub.pubnub.com
})


– Player ID (Generated Later)

my_player_id = nil
my_player_name = “John Smith”


– Generate Player ID

multiplayer:time({
callback = function(time)
my_player_id.id = time … ‘-’ … math.random( 1, 999999 )
end
})


– Players Per Channel

players_per_game_room = {
game_room_1234 = {},
game_room_5678 = {}
}

game_room_1234 = “game_room_1234”

– Join Game Room 1234

multiplayer:subscribe({
channel = game_room_1234,
callback = function(message)


– Ping? then Pong!
– This sends a message from all players, to all players.
– This lets you know who and how many players are on a channel.

if message.action == “ping” then
multiplayer:publish({
channel = game_room_1234,
message = {
action = “pong”,
id = my_player_id,
name = my_player_name
}
})


– Collect Live Players who are in this Game Room.

elseif message.action == “pong” then
players_per_game_room[game_room_1234][message.id] = message


– Some Other Game Event

elseif message.action == “something-else” then

end
end
})

– Request Info About Connected Players

multiplayer:publish({
channel = game_room_1234,
message = { action = “ping” }
})


– Print info on Game Room 1234

function wait_for_info_from_players()

– Print Number of Players

print("Number of Players in channel: " … game_room_1234)
print(#players_per_game_room[game_room_1234])


– Print Info about Each Player

for player_id, player in pairs(players_per_game_room[game_room_1234]) do
print("Player: " … player_id "'s Name is " … player.name)
end
end


– Wait at 1 to 3 seconds for player response
– before we assume we’ve received messages from everyone.

timer.performWithDelay( 2000, wait_for_info_from_players )
[import]uid: 95689 topic_id: 18049 reply_id: 318049[/import] </…b5></…4eb5>

I have not tested this myself in a long time so am a bit rusty, but were you meant to enter any of your information? (For the keys, etc, I mean.)

Peach :slight_smile: [import]uid: 52491 topic_id: 18049 reply_id: 69084[/import]

Hi Peach, thanks again for responding.

I little bit of more info on this:
Found two statements causing the errors.

  1. line 41, that is trying to get the player id. I believe its the “my_player_id.id”. The way I got around this, is by taking how the other Pub Nub example “MultiDrum” gets the player id. See below. (I really don’t understand it but it works)
-- create a unique drummer ID  
my\_id = crypto.digest( crypto.md4, system.getInfo("deviceID"))  
  
  1. line 116
-- create a unique drummer ID  
my\_id = crypto.digest( crypto.md4, system.getInfo("deviceID"))  
  

This one I just commented out, so I need to know why its getting an error. If you could please see why, I would appreciated.

Thanks
(I don’t get how to switch between code and text yet) [import]uid: 95689 topic_id: 18049 reply_id: 69308[/import]

I fixed that code tags. What you do is just put < code > before your code and < / code > after it. (Without the spaces of course.)

For PubNub, can you take a look at the sample code you already have on your computer in CoronaSDK > SampleCode> Networking ?

I just gave that a go and got no errors.

OOI, where is the above example from? (I wouldn’t mind testing it.)

Peach :slight_smile: [import]uid: 52491 topic_id: 18049 reply_id: 69328[/import]

Peach

The above example is from the Pub Nub example for lua corona. It is called “example - channel information”.

It has two errors. One I overcame but the other I need your help. I contacted the Pub Nub people but no response yet. I got these files from GitHub.

And the second error, I noticed I posted wrong above. Here it is:

line 116:

 print("Player: " .. player\_id "'s Name is " .. player.name)  

Thanks Again

I will take a look at the example you mentioned under network. [import]uid: 95689 topic_id: 18049 reply_id: 70595[/import]

Peach,

Took a look at the example under “Networking”, the Mathmatica game, and I am having problems with the messaging that is used there. Maybe you can help me to decipher it or point me to tutorial that does. Here is how they compose the msg:

-- publish message to pubnub  
function send\_message ( action, text )  
 multiplayer:publish ({  
 channel = gameroom,  
 message = {  
 player\_id = my\_player\_id,  
 player\_name = my\_player\_name,  
 player\_mode = my\_player\_mode,  
 player\_score = my\_player\_score,  
 player\_last\_delta = my\_player\_last\_delta,  
 player\_correct = my\_player\_correct,  
 player\_wrong = my\_player\_wrong,  
 action = action,  
 dialog = text  
 },  
 callback = function(info)  
 if info[1] then  
 --print ("(pubnub) published")  
 -- clear the roster for a new roll call  
 if action == "rollcall" then  
 print ("Waiting for all players to respond")  
 player\_roster = { }  
 player\_stats = { }  
 timer.performWithDelay( 3000, show\_player\_roster )  
 elseif action == "hosting" then  
 my\_player\_mode = "host"  
 print ( "I am now the host" )  
 title.text = "You're now hosting the room."  
 elseif action == "guesting" then  
 my\_player\_mode = "guest"  
 print ( "I am no longer the host" )  
 title.text = "You are a guest in the room."  
 end  
 else  
 print ("(pubnub) send error: " .. info[2])  
 end  
 end  
 })  
end  
-- promote myself to host  
function promote\_myself()  
 print ("No game host available, promoting myself")  
 send\_message ( "hosting" )  
end  
-- remove myself as host  
function demote\_myself()  
 print ("Multiple game hosts available, demoting myself")  
 send\_message ( "guesting" )  
end  
-- get the roster and check for host  
function show\_player\_roster()  
 host\_count = 0  
 print ( "Number of players in room: " .. #player\_stats )  
 for i=1, #player\_stats do  
 -- print("#" .. i .. ". " .. player\_stats[i].name .. " (" .. player\_stats[i].mode .. ")")  
 if player\_stats[i].mode == "host" then  
 host\_count = host\_count + 1  
 end  
 end  
 if host\_count == 0 then  
 promote\_myself()  
 elseif host\_count \> 1 then  
 demote\_myself()  
 end  
end  

Could you please point me to a tutorial to compose these types of messages? [import]uid: 95689 topic_id: 18049 reply_id: 70632[/import]

I have filed a bug report with pubnb regarding the issues with the included samples.

I will update the thread when i see a resolution (i have received responses) [import]uid: 84637 topic_id: 18049 reply_id: 70666[/import]

Thanks Danny !! [import]uid: 95689 topic_id: 18049 reply_id: 70716[/import]

No update to report from the pubnub guys yet. Hopefully it’s soon. [import]uid: 84637 topic_id: 18049 reply_id: 70897[/import]

Update: They have now fixed the samples (and pubnub) and updated it. The updated code is available to download here : http://www.pubnub.com/game/multiplayer-gaming-iphone-lua-corona-sdk [import]uid: 84637 topic_id: 18049 reply_id: 71123[/import]