Lua Cloud Code

We expect you to have followed the Cloud Code getting started tutorial.

This tutorial will demonstrate saving and loading persistent data against players, specifically the XYZ values from this previous tutorial. You can build an X and Y function instead of an XYZ function by simply omitting the Z variable for each of the steps in the tutorial.

The two main aspects of this tutorial are the Set and Get functions:

Set Function

The Set function is composed of three parts:


--Build request
local requestBuilder = gs.getRequestBuilder()
local saveLocationRequest = requestBuilder.createLogEventRequest()

--Set Values, pay attention to the value POS which is our attribute and the three values which we'll send inside it
saveLocationRequest:setEventKey("Set_Pos")
saveLocationRequest:setEventAttribute("POS",{X=23,Y=23,Z=23})

--Send request and check for errors
saveLocationRequest:send(function(response)
    if response:hasErrors() then
        for key,value in pairs(response:getErrors()) do print(key,value) end
  end

end)

Now the player will have a position variable saved in their scriptData object. You can double-check this by calling an AccountDetailsRequest:

  1. Through your game and printing out the value of scriptData's 'POS' variable.
  2. Through the Test Harness and checking the scriptData object's contents.

Get Function

As with the Set function, we build-up the Get function in three parts:

The important aspect of this request is the Response that is received from the backend once it's successfully submitted, processed, and sent back. The process will contain the values we saved because of the Cloud Code we configured in the previous tutorial. Once the response is received by the game it can be broken down and variables can be retrieved from it.

--Build Request
local requestBuilder = gs.getRequestBuilder()
local getLocationRequest = requestBuilder.createLogEventRequest()

--Set value for Event Key
getLocationRequest:setEventKey("Get_Pos")

--Send Request
getLocationRequest:send(function(response)
--Process Response
if response:hasErrors() then
    --If errors then print errors
      for key,value in pairs(response:getErrors()) do print(key,value) end
    --Else extract the POS variable (Type table) and print the contents within it
  else local POS = response:getScriptData().POS
      for key,value in pairs(POS) do print(key,value) end
  end
  end)

These are the basics for data exchange between the back end (GameSparks) and the front end (Corona Game) using the GameSparks SDK.

The tutorials in the next section will demonstrate basic Leaderboard creation and use.

Did this page help you? Please enter your feedback below. For questions about using this part of the platform, please contact support here