There may be a time when you want to store some data on web servers, to create multiplayer and such, or maybe to store the results of data gathering. Here is a little example of how to do it using Google App Engine, which is a pretty nice cloud processing and storage service. You can set up a Google App Engine account and submit apps for free as long as they don’t have a ton of traffic. And if your game becomes a best seller, Google can easily handle the traffic and the prices are pretty cheap.
This is example is an online highscore handler. You can submit a highscore and store it on the web server, and retrieve a list of the top scores. Here is a Codea program that connects to the service:
function setup()
gameName = "vegagame" --change this to make a new list
playerName = "vega" -- This is the name sent with the highscore.
http.get("https://vega-highscore.appspot.com/listscores?game=" .. gameName,gotScores, gotError)
--notice I use the SSL address so no one can intercept the password
state = 1
end
function draw()
background(0, 0, 0, 255)
if state == 1 then
text("touch screen to submit a random highscore.", WIDTH/2, HEIGHT/2)
elseif state == 2 then
text("Submitting highscore to cloud server.", WIDTH/2, HEIGHT/2)
else
text("Touch screen to download highscore list.", WIDTH/2, HEIGHT/2)
end
end
function touched(touch)
if touch.state == ENDED then
if state == 1 then
dataStr = "name=" .. playerName .. "&score=" .. math.random(20000)
dataStr = dataStr .. "&game=" .. gameName
dataStr = dataStr .. "&secret=abcdef"
local tbl={["method"]="POST",["data"]=dataStr}
http.get("https://vega-highscore.appspot.com/addscore",sentScore,gotError,tbl)
state = 2
elseif state == 3 then
http.get("https://vega-highscore.appspot.com/listscores?game=" .. gameName,gotScores, gotError)
state = 1
end
end
end
function gotScores(data,status,headers)
print("Current top 10 scores")
print(data)
end
function gotError(error)
print("Error:"..error)
end
function sentScore(data,status,headers)
print(data)
state = 3
end
You can download and test that as-is, or change the gameName to create a new list. It was set up that way so you can deploy the web app once and run all of your games off of it.
If you are using this, of course, you will want to deploy your own version of the web app that you are in control of rather than using mine. So here is a little explanation of how to deploy it and then the source code will follow.
- Go to appengine.google.com and either setup a new google account or register yours for appengine.
- After registering your can hit the button that says “Create an application” and you may be required to verify your identity using a text message on a cell phone.
- When you finally get to the create app form, choose a unique app identifier, mine is vega-highscore, yours can be anything that isn’t already taken. After you get that chosen write that down, because your app will be accessed at “yourIdentifier.appspot.com”
- Leave it open to all google users, and choose “High Replication.”
- Go to https://developers.google.com/appengine/downloads and download the SDK for Python. There are versions for Windows, Linux, and Mac here.
- Run the appEngine and proceed to the next post.