Wednesday, January 9, 2013

Integration of MOAI Leaderboard

I have been experimenting with MOAI to solve some issues that are difficult to resolve in Codea, namely, storage and retrieval of data.  One of the first examples was to create a simple scoring storage system, which worked well as a custom solution, but it felt 'flimsy'. 

Reading up, I found that the system has a basic Leaderboard, which I felt would suit my needs much better. Excited, I jumped in, to try to integrate with it. 

It didn't go so well. After ever all days of trying on again off agin, I pretty much gave up on it, but not without a post to their forum, mostly as a last resort. I was hoping for some response similar to the energy I get I the Codea forums, but no luck - no one was biting on the post after two weeks.

Eventually, someone did post and renewed my interest in solving this. WHILE the post was not helpful (and actually was wrong), It made me look at the problem agai with fresh eyes. Below is the solution.

The problem: 
Whenever I attempted to post a score to my configured leaderboard, I would get 'authentication required'.

According to MOAI, authentication requires sending in the Client Key as a part of your POST. Eventually I was able to determine that this meant it had to be in the header. I made every attempts to post the client key, but was not able to get past the error.

The solution:
I finally got this to work from Codea. The trick, as mentioned in another post on their forums, was to force the client key into the headers, and place it in the x-clientKey field. Note that this isn't the same as putting the field in the http post table at the flat level and posting it; this requires making a "headers" table and posting that as a parameter of the post table. I wasn't doing that correctly, so I wasn't authenticating. 

Additionally, the body of the post is supposed to be a well-formed JSON string, which is stated but not very clearly.

The Example:

Tis example is more about the concepts than a huge area of working code. As I do more with MOAI, I'll have working code to post. For now I have chunks of my test project here.

 JSON data to post: 
{"username":"my user","unique":"false","sort":"desc","userid":"my userid","score":125000}

Codea:
--populated jsonData elsewhere with JSON:encode() function.
-- important to make this a seperate table!!!
local headers = {}
headers["x-clientKey"] = clientKey. -- this variable has the client key
-- now, make a pare terms table that will be posted. The headers table will get packed into this one.
local params = {}
params.data = jsonData -- properly JSON formatted data. This is the body.
params.headers = headers
params.method="POST"
--now do the post...define the functions Loaded to dump data,  httpError to trap for failure
http.get(url,
function(data,status,headers)
result = {}
result= JSON:decode(data)
Loaded(result) -- rank comes back for free!
end,
httpError, params)
end
Summary:
Knowing now that the headers have to be packed as a seperate table and be called "headers" in the http params and that the body is posted as "data" was the solution.  I'm now ready to dive into more MOAI experiments!

Friday, December 28, 2012

About Codea

About 5 months ago, I downloaded a fun-looking package called "Codea". I don't recall where I read about it or what piqued my interested, but something did and I grabbed it.

I'm a C# / Sharepoint developer (see http:// squarepoint.blogspot.com) by trade, so a new language (LUA) was not of interest to me. However, game development has been a passing interest for many many years, so it was worth a look.

The first thing that was interesting was that there wasn't a lot to learn about LUA, which helped - it uses simple variables and tables to do most of it's work. Coupled with an OpenGL 2D/3D library, it was pretty quick and easy to get a simple concept up and running. In my case, that concept was implementing an multi-character A* shooting game. It looked sloppy, but was fast to put together.

About a month later and a few late nights, I was noticing that there's a lot more under the hood that could be accomplished. The TwoLivesLeft Codea forums are one of the most active and participatory ones I've been on (for example I also will blog about MOAI; that forum is like a sinking tomb) with people who are genuinely interested in helping others get their concepts from head to device.

One of the most challenging issues with Codea is archival of code; there's not real way to do it in the application (as of 1.4.6, version 1.5 is supposed to resolve that). I've many projects with code that works but I couldn't recall how to write it from scratch; there's other template projects that are useful for certain things like game frameworks, etc. Losing that code would be a disaster. Coupled with my desire to stay fully iPad, it's a concern. There are workarounds to this issue, but it's still a challenge.

Additionally, accessing things like textboxes and sliders can't be done (yet); it's a blank canvas and doesn't really access the default IOS UI. This tends to be unimportant until it is. Once again, the community has put together some basic solutions to resolve that issue.

With those issues mentioned, there were too many positives from a game developers POC mindset to pass up:
  • A blank canvas with 60 fps heartbeat built in 
  • Box2D support for physics platformers
  • OpenGL 3D support for visualization or complex gaming 
    • Update: Shaders added in 1.5!
  • Simple Dropbox integration to get assets into your iPad
  • A runtime to allow people to compile native apps - this was the biggest draw for me
So, several months and a few too many late nights, I'm still coding away occasionally on a few projects and I plan on releasing a few of them into the App Store in the next few months.

I'd encourage anyone who wants to dabble in game development or 3D to get a copy and play around. There's lots of support out there to help make your ideas reality!


Welcome to Up Two Late!

Welcome!

I've started this blog so that I can post links to other development efforts and software development processes while I am doing Codea-based development. Therre are a lot lot of open questions on how to achieve goals using the excellent Codea software package by TwoLivesLeft.

As I learn new things and techniques, I will post them here.