Make a common utility file in Lua — Corona SDK

When we building an app or a game, we use a function very frequently and we need to make a common functions and constants. One thing is you can declare the variables globally and use anywhere or to make the code more efficient and more readable, you can make a common class to access all the functions and variable.

Create a new file utility.lua or any other name which you prefer and add the code like below

local myVariable1 = "SOME_CONSTANT_VALUE"
local myVariable2 = "SOME_CONSTANT_VALUE"

local M = {}

function M.myAwesomeFunction1()
  -- Do something here and return the value

function M.getMyVariable1()
  return myVariable1

return M

Then on any other lua file, where you want to use the function call like this

local utility = require "utility"

local anyVariable = utility.myAwesomeFunction1()
local myVariable1 = utility.getMyVairable1()

Using a utility file will be so convenient and easy to modify the code.

Happy Coding !

No Comments Yet