Network data access

Hi all,

I want to create a helper script to access my json data over my webpage. I would like to create another lua file to access this data, then use this data in my main.lua file. I want output to be actual time data from my URL instead of “i am not nil”

Main.lua

local jsonAccess = require("jsonAccess") print(jsonAccess["hereisdate"])

JsonAccess.lua

local json = require "json" local jsonClass={}; jsonClass.hereisdate = "i am not nil" local function networkListener( event ) if ( event.isError ) then print ("Network error!") else local jsonData = json.decode(event.response) jsonClass.hereisdate = jsonData["time"] end end network.request( "http://URL/json\_data.php", "GET", networkListener ) return jsonClass; 

I am new to Lua and having a bit of issue figuring out how to deal with scoping. If I do my assignments in networkListener function, due to scoping, variable value outside of this scope stays unchanged. Eventually my network data access function never updates my local variables. 

Should i be saving my data to sql or global variables instead?

Hi @umur.yilmaz,

Welcome to Lua/Corona! You should absolutely avoid global variables. There are several methods to avoid them, a few of which are outlined in this tutorial:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Best regards,

Brent

Hi @umur.yilmaz,

Welcome to Lua/Corona! You should absolutely avoid global variables. There are several methods to avoid them, a few of which are outlined in this tutorial:

http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/

Best regards,

Brent