Standard value's file

In the past I have tried out a bunch of things regarding to app development and programming.

I have made an app for iOS and tried out Xamarin a cross-platform app development engine but I didn’t find it suit my needs so i stepped over to corona sdk ( which I totally love btw :))

But I never had lessons in programming or any education in it nor I am an expert in any sense. 

So i don’t know how you would do the following in corona development or in other app development.

In my app i have a lot of variables some of them are read-only values (value aren’t changed) and some are changed, I access most of the variable very frequently.

For organisation purposes I thought I make a file where all/some of the value are saved. I don’t need the same values in other scene(composer)/files but what if I did.

a. Is this a thing one does in app development and in particular Corona?

b. If so how would you setup the file?

My english isn’t so good, so if you don’t understand anything please let me know.

I’m a little unclear on what you’re asking.

Do you want to have an options / persistent-data file in you game/app where users can select values options and those options will be re-loaded the next time you start the app/game?

Ex: 

  • Music On/Of
  • Difficulty Level
  • Last High Score

If the answer is yes, then you can learn how to do that here:

https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

Also, SSK2 has this functionality as:

Note: If you’re very new, this may all still be a little confusing.  My answer above only applies to the how of saving and loading persistent data.

In computer sciencepersistence  refers to the characteristic of state that outlives the process that created it. This is achieved in practice by storing the state as data in computer data storage. Programs have to transfer data to and from storage devices and have to provide mappings from the native programming-language data structures to the storage device data structures

You still have to write code to save it at the right time and to load it at the right then, then to take those ‘loaded’ values and apply them in your game/app.

No it is not for the user it are variables use by the program. A little bit like the stat of a game. 

I already know how to save my highscore I just use a library from someone.

local shapes = { "square", "triangle", "circle", "star", "diamond" } local colors = { "blue", "green", "red", "gold", "black" } local colorsStat = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} local strokeColors = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} local vertices = { star = { 0, -40, 9, -13, 37, -13, 15, 5, 23, 32, 0, 16, -24, 32, -16, 5, -38, -13, -10, -13 }, square = { -30, -30, 30, -30, 30, 30, -30, 30 }, triangle = { 0,-37, 32,18.5, -32,18.5 }, diamond = { 0,-40, 28,0, 0,40, -28,0 }} local bgColor = {1,1,1} -- background color local strokeColor = 0.6 local strokeThick = 4 --strokeWidth

I just though it would by nicer to have all these values and more in one file. And then the can also be used in multiple files/scenes.

Ah I see.

Yes. You can make a module for storing all ‘common’ data like this:

  1. Create a file called common.lua (can be anything, but called common for this discussion):

    – common.lua – I use the same name for the table, but can be anything you want in the file. local common = {} common.shapes = { “square”, “triangle”, “circle”, “star”, “diamond” } common.colors = { “blue”, “green”, “red”, “gold”, “black” } common.colorsStat = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} common.strokeColors = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} common.vertices = { star = { 0, -40, 9, -13, 37, -13, 15, 5, 23, 32, 0, 16, -24, 32, -16, 5, -38, -13, -10, -13 }, square = { -30, -30, 30, -30, 30, 30, -30, 30 }, triangle = { 0,-37, 32,18.5, -32,18.5 }, diamond = { 0,-40, 28,0, 0,40, -28,0 }} common.bgColor = {1,1,1} – background color common.strokeColor = 0.6 common.strokeThick = 4 --strokeWidth return common

  2. In any other file use it like this all files will see the contents and changes to the module will also be visible in any file using it.

    – main.lua local common = require “common” print( common.shapes[1], common.shapes[2], common.shapes[3] ) local tmp = display.newPolygon( 100, 100, unpack(common.verticies.star) ) tmp:setFillColor( unpack( common.colorsStat.blue ) ) – … etc.

To be clear, the following is exactly equivalent to my prior example
 

-- settings.lua -- I use the same name for the table, but can be anything you want in the file. local public = {} public.shapes = { "square", "triangle", "circle", "star", "diamond" } public.colors = { "blue", "green", "red", "gold", "black" } public.colorsStat = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} public.strokeColors = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} public.vertices = { star = { 0, -40, 9, -13, 37, -13, 15, 5, 23, 32, 0, 16, -24, 32, -16, 5, -38, -13, -10, -13 }, square = { -30, -30, 30, -30, 30, 30, -30, 30 }, triangle = { 0,-37, 32,18.5, -32,18.5 }, diamond = { 0,-40, 28,0, 0,40, -28,0 }} public.bgColor = {1,1,1} -- background color public.strokeColor = 0.6 public.strokeThick = 4 --strokeWidth return public 

-- main.lua local common = require "settings" print( common.shapes[1], common.shapes[2], common.shapes[3] ) local tmp = display.newPolygon( 100, 100, unpack(common.verticies.star) ) tmp:setFillColor( unpack( common.colorsStat.blue ) ) -- .. etc.

All code I typed above is UNTESTED so it may have syntax errors, but it is close enough to correct if not 100% correct that you should get the gist of what I’m trying to express.

I know my first post what not clear. But thanks for the help. :slight_smile:

Btw does this effect performances much?
And can you change the values and is it persistent?

Doesn’t affect performance at all, and yes the values are persistent. If you change a value in one scene, it will be changed for all scenes that also called require “common”.

I’m a little unclear on what you’re asking.

Do you want to have an options / persistent-data file in you game/app where users can select values options and those options will be re-loaded the next time you start the app/game?

Ex: 

  • Music On/Of
  • Difficulty Level
  • Last High Score

If the answer is yes, then you can learn how to do that here:

https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

Also, SSK2 has this functionality as:

Note: If you’re very new, this may all still be a little confusing.  My answer above only applies to the how of saving and loading persistent data.

In computer sciencepersistence  refers to the characteristic of state that outlives the process that created it. This is achieved in practice by storing the state as data in computer data storage. Programs have to transfer data to and from storage devices and have to provide mappings from the native programming-language data structures to the storage device data structures

You still have to write code to save it at the right time and to load it at the right then, then to take those ‘loaded’ values and apply them in your game/app.

No it is not for the user it are variables use by the program. A little bit like the stat of a game. 

I already know how to save my highscore I just use a library from someone.

local shapes = { "square", "triangle", "circle", "star", "diamond" } local colors = { "blue", "green", "red", "gold", "black" } local colorsStat = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} local strokeColors = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} local vertices = { star = { 0, -40, 9, -13, 37, -13, 15, 5, 23, 32, 0, 16, -24, 32, -16, 5, -38, -13, -10, -13 }, square = { -30, -30, 30, -30, 30, 30, -30, 30 }, triangle = { 0,-37, 32,18.5, -32,18.5 }, diamond = { 0,-40, 28,0, 0,40, -28,0 }} local bgColor = {1,1,1} -- background color local strokeColor = 0.6 local strokeThick = 4 --strokeWidth

I just though it would by nicer to have all these values and more in one file. And then the can also be used in multiple files/scenes.

Ah I see.

Yes. You can make a module for storing all ‘common’ data like this:

  1. Create a file called common.lua (can be anything, but called common for this discussion):

    – common.lua – I use the same name for the table, but can be anything you want in the file. local common = {} common.shapes = { “square”, “triangle”, “circle”, “star”, “diamond” } common.colors = { “blue”, “green”, “red”, “gold”, “black” } common.colorsStat = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} common.strokeColors = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} common.vertices = { star = { 0, -40, 9, -13, 37, -13, 15, 5, 23, 32, 0, 16, -24, 32, -16, 5, -38, -13, -10, -13 }, square = { -30, -30, 30, -30, 30, 30, -30, 30 }, triangle = { 0,-37, 32,18.5, -32,18.5 }, diamond = { 0,-40, 28,0, 0,40, -28,0 }} common.bgColor = {1,1,1} – background color common.strokeColor = 0.6 common.strokeThick = 4 --strokeWidth return common

  2. In any other file use it like this all files will see the contents and changes to the module will also be visible in any file using it.

    – main.lua local common = require “common” print( common.shapes[1], common.shapes[2], common.shapes[3] ) local tmp = display.newPolygon( 100, 100, unpack(common.verticies.star) ) tmp:setFillColor( unpack( common.colorsStat.blue ) ) – … etc.

To be clear, the following is exactly equivalent to my prior example
 

-- settings.lua -- I use the same name for the table, but can be anything you want in the file. local public = {} public.shapes = { "square", "triangle", "circle", "star", "diamond" } public.colors = { "blue", "green", "red", "gold", "black" } public.colorsStat = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} public.strokeColors = { blue = {0,0,1,1}, red = {1,0,0,1}, green = {0,1,0,1}, gold = {1, 215/255, 0, 1}, black = {0,0,0,1}} public.vertices = { star = { 0, -40, 9, -13, 37, -13, 15, 5, 23, 32, 0, 16, -24, 32, -16, 5, -38, -13, -10, -13 }, square = { -30, -30, 30, -30, 30, 30, -30, 30 }, triangle = { 0,-37, 32,18.5, -32,18.5 }, diamond = { 0,-40, 28,0, 0,40, -28,0 }} public.bgColor = {1,1,1} -- background color public.strokeColor = 0.6 public.strokeThick = 4 --strokeWidth return public 

-- main.lua local common = require "settings" print( common.shapes[1], common.shapes[2], common.shapes[3] ) local tmp = display.newPolygon( 100, 100, unpack(common.verticies.star) ) tmp:setFillColor( unpack( common.colorsStat.blue ) ) -- .. etc.

All code I typed above is UNTESTED so it may have syntax errors, but it is close enough to correct if not 100% correct that you should get the gist of what I’m trying to express.

I know my first post what not clear. But thanks for the help. :slight_smile:

Btw does this effect performances much?
And can you change the values and is it persistent?

Doesn’t affect performance at all, and yes the values are persistent. If you change a value in one scene, it will be changed for all scenes that also called require “common”.