Tag Archive for Corona SDK

Corona SDK Utilities: NewVariable function

Hello Corona Devs,

Second function I want to share with you is UTL.NewVariable. It is committed into GitHub repository and is located in file utl/settings.lua

Please feel free to use/share them. If you find any bugs or have any comments please don’t hesitate to write me.



function UTL.NewVariable(filename, default)
	local tbl = UTL.LoadTable(filename);
	if not (tbl) then
		tbl = default;
		UTL.SaveTable(tbl, filename);

	return setmetatable({
			Get = function()
				return tbl;
		}, {
		__index = function(t, key)
			return tbl[key];

		__newindex = function(t, key, value)
			tbl[key] = value;
			UTL.SaveTable(tbl, filename);

This function uses UTL.LoadTable and UTL.SaveTable function you can find below:

function UTL.SaveTable(t, filename)
	local path = system.pathForFile( filename, system.DocumentsDirectory)
	local file = io.open(path, "w")
	if file then
		local contents = JSON.encode(t)
		file:write( contents )
		io.close( file )
		return true
		return false

function UTL.LoadTable(filename, dir)
	if (dir == nil) then
		dir = system.DocumentsDirectory;

	local path = system.pathForFile( filename, dir)
	local contents = ""
	local myTable = {}
	local file = io.open( path, "r" )
	if file then
		 -- read all contents of file into a string
		 local contents = file:read( "*a" )
		 myTable = JSON.decode(contents);
		 io.close( file )
		 return myTable 
	return nil


What it does

Function NewVariable creates table to which you can set/get values and every time value in that table is changed it will be saved in file specified. Initial value of variable will be one that is saved in file.


Here is a small example:

local var = UTL.NewVariable("test.json", {
    counter = 0
var.value1 = "something";
var.value2 = 12;
var.counter = var.counter + 1;

print("value1 = ", var.value1);
print("value2 = ", var.value2);
print("counter = ", var.counter);

Above example will output following:

First run:
value1 = 	something
value2 = 	12
counter = 	1

Second run:
value1 = 	something
value2 = 	12
counter = 	2

Third run:
value1 = 	something
value2 = 	12
counter = 	3


When to use

This object is useful when you want to save apps state and restore on next run easily.

For example:

local Settings = UTL.NewVariable("settings.json", {
    soundEnabled = true,
    musicEnabled = true,
    coins = 0


store:addEventListener("purchase", function(amount)
    Settings.coins = Settings.coins + amount;


Corona SDK Utilities: Chain function

Hello Corona Devs,

I am starting series of tutorial like posts where I will share with all of you some small utility functions for Corona SDK which I use in my apps and games. I have created GitHub repository where I committed all my current utility functions. Please feel free to use/share them. If you find any bugs or have any comments please don’t hesitate to write me.

For my first post I have this small function I called Chain.



function UTL.Chain(...)
	local callbacks = {...};

	return function(...)
		local res = {...};
		for i = 1, #callbacks do
			res = { callbacks[i](unpack(res)) };
		return unpack(res);


What it does

Function Chain take other functions as argument and returns single function: chain function. When chain function called it will start calling first function with arguments it got, then second function with return value of first function, then third function with return value of second and so on. Chain function will return return value of last function.


Here is a small example:

local function Func1(a, b)
	print("Func1 called with ", a, b);
	return a + 1, b + 2;

local function Func2(a, b)
	print("Func2 called with ", a, b);
	return a + b;

local function Func3(a)
	print("Func3 called with ", a);
	return a * 2;

local chain = UTL.Chain(Func1, Func2, Func3);
print("Result is ", chain(1, 1));

Above example will output following:

Func1 called with 	1	1
Func2 called with 	2	3
Func3 called with 	5
Result is 	10


When to use

Chain function is very useful in places where you want to call two functions and don’t want to create a new  anonymous function in place.

For example:

obj:addEventListener("tap", UTL.Chain(PlayTapSound, HideBottomBar, DoSomethingElse));

If you want to return some value from chain: for example you want to return true to stop event layering you can just add function that returns true at the end: in this library there is a function UTL.TrueFn which does that.



Icon Generator for Corona SDK

Hello fellow Corona SDK developers

I know how painful is to generate all icons for your game. There are scripts and everything for that, but I decided to create small web based program for icon generation Icon Generator.

Just select your highest resolution PNG file and press Generate. It will generate following files for you.

  • Icon-40.png – iOS
  • Icon-40@2x.png – iOS
  • Icon-60.png – iOS
  • Icon-60@2x.png – iOS
  • Icon-72.png – iOS
  • Icon-72@2x.png – iOS
  • Icon-76.png – iOS
  • Icon-76@2x.png – iOS
  • Icon-Amazon.png – For Amazon Game submission
  • Icon-Small-50.png – iOS
  • Icon-Small-50@2x.png – iOS
  • Icon-Small.png – iOS
  • Icon-Small@2x.png – iOS
  • Icon-hdpi.png – Android
  • Icon-ldpi.png – Android
  • Icon-mdpi.png – Android
  • Icon-xhdpi.png – Android
  • Icon-xxhdpi.png – Android
  • Icon.png – iOS
  • Icon@2x.png – iOS
  • iTunesArtwork.png – iOS
  • iTunesArtwork@2x.png – iOS
  • upload.png – Your original file

Added new icons based on feedback

  • Icon-16.png – iOS
  • Icon-24.png iOS
  • Icon-32.png iOS
  • Icon-64.png iOS
  • Icon-120.png iOS
  • Icon-152.png iOS
  • Icon-Small-40.png iOS
  • Icon-Small-40@2x.png – iOS

Please feel free to contact me if you have any questions or comments.

Thank you