Remote control variables

Hi All,

This form has been a great place for me and I got lot of help from here and really thankful to all. Now my app is near completion and I need help about something advanced. I have some variables in my app which are working as true/false flags. Now I need to remote control them. Like I need some way to control variables live without doing anything with code. Like someone has downloaded the free version and I want to set/unset variables remotely. I hope I have made my point clear.
I heard about parse which can do the job. I have downloaded the API and doing some research on it but not finding any useful help. I need some one to help me on this. I will be thankful.

Mueed [import]uid: 126619 topic_id: 33326 reply_id: 333326[/import]

You can use JSON. If you play around with metatables a little bit, you can have the json lazy-load on first use, and act like a standard LUA table.

local file = {}  
local variables = nil  
  
--Change these variables to point to your downloaded file  
local fileName = "variables.json"  
local dir = System.resourceDirectory  
  
local function load()  
 local path = system.pathForFile( filename, dir )  
 local file = io.open( fileName, "r" )  
 if file then  
 local contents = file:read( "\*a" )  
 io.close( file )  
 variables = json.decode(contents)  
 end   
 return contents  
end  
  
local function get(tbl, idx)  
 if not variables then  
 load()  
 end  
 return variables and variables[idx]  
end  
  
function file.reload()  
 variables = nil  
end  
  
setmetatable(file, {\_\_index = get }) --If you want it to be read-only, you can also add '\_\_newindex = function()end to the second parameter  
return file  

This file then acts like the a LUA table, even though it is originally a JSON file. i.e. if you had a JSON file like:

{  
 key1: "Value1",  
 tableKey:   
 [  
 tableKey1:"tableValue1",  
 tableInt: 5  
 ]  
}  
  
--LUA (and assuming the above LUA code was saved to a file called variables.lua  
local var = require("variables")  
print(var.tableKey.tableKey1) --Prints tableValue1  

Now the only remaining element is to use network.download to overwrite the existing variables.json file whenever changes are required.

Note: This isn’t so well tested, so there may be typos, but the overall premise works. [import]uid: 134101 topic_id: 33326 reply_id: 132340[/import]

We wrote a simple lib a while ago for this purpose - https://github.com/GlitchGames/GGTweak [import]uid: 119420 topic_id: 33326 reply_id: 132341[/import]

Hi Glitch Games,

I download the lib and it looks very useful. However I am bit confused with it. I am trying to read a json file using the method given but I get nil value.

Here is my json file

{
“employees”: [
{ “firstName”:“John” , “lastName”:“Doe” },
{ “firstName”:“Anna” , “lastName”:“Smith” },
{ “firstName”:“Peter” , “lastName”:“Jones” }
]
}

Now I am trying to read it this way

local GGTweak = require( “GGTweak” )

local onRefresh = function()
print( tweak:get( “firstName” ) )
end
local tweak = GGTweak:new( “http://localhost/emp.json”, onRefresh, 1000 )

print( tweak:get( “firstName” ) )

I have installed the wamp server and put my json file named emp.json in the www directory. But I get a nil value in return. May be I am doing it wrong. Please guide me further about this.

Thanks

Mueed [import]uid: 126619 topic_id: 33326 reply_id: 132434[/import]

You can use JSON. If you play around with metatables a little bit, you can have the json lazy-load on first use, and act like a standard LUA table.

local file = {}  
local variables = nil  
  
--Change these variables to point to your downloaded file  
local fileName = "variables.json"  
local dir = System.resourceDirectory  
  
local function load()  
 local path = system.pathForFile( filename, dir )  
 local file = io.open( fileName, "r" )  
 if file then  
 local contents = file:read( "\*a" )  
 io.close( file )  
 variables = json.decode(contents)  
 end   
 return contents  
end  
  
local function get(tbl, idx)  
 if not variables then  
 load()  
 end  
 return variables and variables[idx]  
end  
  
function file.reload()  
 variables = nil  
end  
  
setmetatable(file, {\_\_index = get }) --If you want it to be read-only, you can also add '\_\_newindex = function()end to the second parameter  
return file  

This file then acts like the a LUA table, even though it is originally a JSON file. i.e. if you had a JSON file like:

{  
 key1: "Value1",  
 tableKey:   
 [  
 tableKey1:"tableValue1",  
 tableInt: 5  
 ]  
}  
  
--LUA (and assuming the above LUA code was saved to a file called variables.lua  
local var = require("variables")  
print(var.tableKey.tableKey1) --Prints tableValue1  

Now the only remaining element is to use network.download to overwrite the existing variables.json file whenever changes are required.

Note: This isn’t so well tested, so there may be typos, but the overall premise works. [import]uid: 134101 topic_id: 33326 reply_id: 132340[/import]

We wrote a simple lib a while ago for this purpose - https://github.com/GlitchGames/GGTweak [import]uid: 119420 topic_id: 33326 reply_id: 132341[/import]

Hi Glitch Games,

I download the lib and it looks very useful. However I am bit confused with it. I am trying to read a json file using the method given but I get nil value.

Here is my json file

{
“employees”: [
{ “firstName”:“John” , “lastName”:“Doe” },
{ “firstName”:“Anna” , “lastName”:“Smith” },
{ “firstName”:“Peter” , “lastName”:“Jones” }
]
}

Now I am trying to read it this way

local GGTweak = require( “GGTweak” )

local onRefresh = function()
print( tweak:get( “firstName” ) )
end
local tweak = GGTweak:new( “http://localhost/emp.json”, onRefresh, 1000 )

print( tweak:get( “firstName” ) )

I have installed the wamp server and put my json file named emp.json in the www directory. But I get a nil value in return. May be I am doing it wrong. Please guide me further about this.

Thanks

Mueed [import]uid: 126619 topic_id: 33326 reply_id: 132434[/import]

I never tested on a local server however it should work just fine, are you able to make a simple demo app to show the issue off? [import]uid: 119420 topic_id: 33326 reply_id: 132695[/import]

I never tested on a local server however it should work just fine, are you able to make a simple demo app to show the issue off? [import]uid: 119420 topic_id: 33326 reply_id: 132695[/import]