Native Game Center Information

I’ve got a pretty bad cold and am medded-up quite a bit, so I pre apologize if I’m in coherent.
I’m taking a break to figure out why some of my enemies are going the wrong direction in my update to Turkeys Revenge (Free, iTunes http://itunes.apple.com/us/app/turkeys-revenge/id481579390?mt=8) to work on getting Game Center integrated. With the docs, the blog post and the code posted in the Game Center tutorial forum thread, I thought I was doing really well.

In fact, now, after it authorizes you, I save your alias and playerID for later reference by calling loadLocalPlayer…

On my new high scores screen, not only do I give the players a button to get to the GameCenter leader board code, but I’m showing the top 5 scores on the page. Using the playerID I saved earlier, I’m bolding any scores that are yours.

So all is good, but I was having my frustrating moments, which is why I’m writing this.

event.errorCode is only set if there are errors. The documentation is clear on this, but I was writing:

 if event.errorCode == false then  
 native.showAlert("Success", json.encode(event.data), { "Ok"})  
 else  
 native.showAlert("Error", event.errorMessage, { "Ok"})  
 end  

This will not work. I like writing positive code, in that to me, the if block should contain the success code and the else block the error code.

In other words I would like to write:

if event.errorCode == false then  
 -- do my sucdess code  
end -- because I don't care about the failure  

But you can’t do this. You either end up writing a negative oriented block:

  
if event.errorCode then  
 -- do your error handling  
else  
 -- do your success stuff  
end  

or

if event.errorCode == nil then  
 -- do your success code  
end  

It would be really nice for event.errorCode to be set regardless or give us a positive way of writing the code. I mean I guess I could do:

if event.data then  
...  

since data doesn’t exist on error.

Minor detail, but it did frustrate me for a while.

Rob [import]uid: 19626 topic_id: 20798 reply_id: 320798[/import]

This is not Game Center specific, but in all kinds of lua/corona APIs, independent libraries, loading/saving from DBs, etc. I’ve found that too often writing those positive statements you speak of ends up not working for the stupidest of reasons (e.g. a library sending “false” instead of false).

I’ve pretty much resorted to specifically calling out the conditions I wish to handle and just ignoring anything else.
[import]uid: 36054 topic_id: 20798 reply_id: 81788[/import]

Several quick points:

  • event.data may return data depending on the situation even if there is an error, so watch out

  • errorCode is a number (integer) not a boolean. Testing for false seems dubious at best. But for the sake of argument, if we did go down this path, then even more dubious is errorCode will never equal true under any circumstance which will likely cause mass confusion.

  • The errorCode/errorMessage originates from two different parts. One part is from Apple itself. We are just passing Apple’s errorCode/errorMessage through. The other part is some implementation details in Corona. The code base is actually inconsistent about what to do with errors, but the base class I had to subclass from uses errorCode and errorMessage. We did have some talks about having a third boolean which would always be true or false just for denoting the error state but it was not already in this base class so a lot more code would have to be changed. (It seems a little redundant though and see the next item.)

  • It is a Lua language thing that setting a value to nil deletes the key. It is also a Lua language thing that (only) nil and false are evaluated to false. (Contrary to some other languages, 0 and “” evaluates to true.) As a result, ‘if foo then’ is a common Lua idiom.

  • You could do:

if not event.errorCode then  
 -- do your success code  
end  

[import]uid: 7563 topic_id: 20798 reply_id: 81789[/import]

I have an open post with no responses. gameNetwork.request(“loadAchievements”… does not seem to be working. I am testing it in sandbox and getting nothing in the callback. I need a confirmation from someone at ansca that they know it does not work or if they have it working, an example. [import]uid: 108813 topic_id: 20798 reply_id: 81881[/import]

I already responded in your other thread. [import]uid: 7563 topic_id: 20798 reply_id: 82068[/import]