Make realtime games with AppWarp and Corona SDK

In this post, I will walk you through the integration of AppWarp Lua SDK with a simple Corona application which allows user to move objects in realtime across multiple clients. It is based on the standard Drag sample that comes with Corona SDK.

ColorMove Make realtime games with AppWarp and Corona SDK

Project Setup and Running

  • Follow these steps and get your key pair and a room id
  • Download the color move sample code from our git repo
  • Copy the AppWarp folder from the latest version and put it in the Sample’s folder. It should look like this

LuaColorMoveDir Make realtime games with AppWarp and Corona SDK

  • Edit main.lua and replace the keys and roomid where indicated with the values you got from AppHq dashboard
  • Load the Sample in Corona Simulator and verify that it loads without any errors

Code Highlights

We first draw the objects (color vectors) on the screen and register for touch listeners. Users will be dragging these around. It is based on the DragMe sample available in the Corona SDK.

1
2
3
4
5
6
7
8
9
10
11
12
13
-- Iterate through arguments array and create rounded rects (vector objects) for each item
for _,item in ipairs( arguments ) do
	local button = display.newRoundedRect( item.x, item.y, item.w, item.h, item.r )
	button:setFillColor( item.red, item.green, item.blue )
	button.strokeWidth = 6
	button:setStrokeColor( 200,200,200,255 )
	-- Make the button instance respond to touch events
	button:addEventListener( "touch", onTouch )
 
  -- assign ids to buttons and insert in table
  button.id = tostring(item.id)
  myButtons[button.id] = button  
end

We begin by establishing a connection with the server with a unique name. We then join and subscribe the game room. Once it’s done, users can drag and drop the objects, and in realtime other users in the room can see the objects move.

main.lua

1
2
appWarpClient = require "AppWarp.WarpClient"
appWarpClient.initialize(API_KEY, SECRET_KEY)

We create a global instance of the appwarp client and initialize it. It is important to keep this instance global and access the same one in your interactions with appwarp. Initialization with your developer keys is important so that your communication with the server is authenticated. Hence it must be called up first and before you connect with the server.

This following snippet is important as it provides for the asynchronous communication with the server. It registers the “enterFrame” listener with the gameLoop callback. In this callback we call the Loop api of the appwarp client instance. This allows appwarp client to check if there are any pending messages from the server and if there are any, notify the application.

1
2
3
4
5
local function gameLoop(event)
  appWarpClient.Loop()
end
 
Runtime:addEventListener("enterFrame", gameLoop)

Once the setup is complete, we establish the connection with appwarp server. Concurrent connections to the same application in appwarp must have unique names. App developers are free to use any ID provider as long as it provides unique names such as Facebook, Google+ etc. or from their own user management system. In this case we simply use the time to get unique names.

1
2
-- start connecting with a random name
appWarpClient.connectWithUserName(tostring(os.clock()))

To receive callbacks from appwarp client, we need to register the corresponding listeners. The code for that is in warplisteners.lua

We register the listeners as follows:

1
2
3
appWarpClient.addRequestListener("onConnectDone", onConnectDone)
appWarpClient.addRequestListener("onJoinRoomDone", onJoinRoomDone)
appWarpClient.addRequestListener("onSubscribeRoomDone", onSubscribeRoomDone)

And define the callbacks as follows:

1
2
3
4
5
6
7
8
9
10
function onConnectDone(resultCode)
  if(resultCode == WarpResponseResultCode.SUCCESS) then
    statusText.text = "Joining Room.."
    appWarpClient.joinRoom(STATIC_ROOM_ID)
  elseif(resultCode == WarpResponseResultCode.AUTH_ERROR) then
    statusText.text = "Incorrect app keys"
  else
    statusText.text = "Connect Failed. Restart"
  end  
end

Upon success, we will join and subscribe the game room.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function onJoinRoomDone(resultCode)
  if(resultCode == WarpResponseResultCode.SUCCESS) then
    appWarpClient.subscribeRoom(STATIC_ROOM_ID)
    statusText.text = "Subscribing to room.."
  else
    statusText.text = "Room Join Failed"
  end  
end
 
function onSubscribeRoomDone(resultCode)
  if(resultCode == WarpResponseResultCode.SUCCESS) then    
    statusText.text = "Started!"
  else
    statusText.text = "Room Subscribe Failed"
  end  
end

We can now send updates to other players whenever the user drags and drops one of the pieces. We will use the update peers API for sending these updates. We send the object id and its x,y coordinates.

1
appWarpClient.sendUpdatePeers(tostring(t.id) .. " " .. tostring(t.x).." ".. tostring(t.y))

To receive updates so that we can update positions based on network events, we need to register for this notification and handle it.

1
appWarpClient.addNotificationListener("onUpdatePeersReceived", onUpdatePeersReceived)

We define the callback as follows. We will simply parse the received message according to how we send it i.e. the id and its x,y coordinates.

1
2
3
4
5
6
7
8
9
10
11
function onUpdatePeersReceived(update)
  local func = string.gmatch(update, "%S+")
  -- extract the sent values which are space delimited
  local id = tostring(func())
  local x = func()
  local y = func()
  local button = myButtons[id]
  button.x = tonumber(x)
  button.y = tonumber(y)
  print("someone moved button id ".. id)
end

That’s it. We walked through some of the key concepts of integrating AppWarp Lua SDK in your application. We illustrated how you register request and notification listeners. We illustrated the basic room operations of join/subscribe and update Peers.

Original Blog Source: https://developer.coronalabs.com/code/multiplayer-game-development-using-appwarp-cloud

twitter Make realtime games with AppWarp and Corona SDKfacebook Make realtime games with AppWarp and Corona SDKgoogle Make realtime games with AppWarp and Corona SDKlinkedin Make realtime games with AppWarp and Corona SDKpinterest Make realtime games with AppWarp and Corona SDKreddit Make realtime games with AppWarp and Corona SDKstumbleupon Make realtime games with AppWarp and Corona SDKtumblr Make realtime games with AppWarp and Corona SDK

Leave a Reply

Your email address will not be published. Required fields are marked *


− two = 1

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>