Handy Code Snippets

This is a post that lists some handy code snippets. They will generally be small examples and the list will be added to in time and also if you guys post some snippets I will add them to this list.
 

Print/Display a boolean value preceded by a string

Problem :

If you try to simply print a boolean value preceded by a string like so :

local myBool = true

print("Value of my bool = " … myBool)[/lua]

You will get the following error :

“attempt to concatenate field ‘myBool’ (a boolean value)”

To get around this you instead use a comma.

–Usage
local myBool = true

print("Value of my bool = ", myBool)[/lua]

This will output :

Value of my bool = true
 

Iterating through a key/pair table in order

As you know if you iterate through a key/pair table, it does not do so in the order it was defined.

Eg:

local myTable = { ["key1"] = 1, ["key2"] = 2, ["key3"] = 3, } for k, v in pairs(myTable) do print(myTable[k]) end

will print :

1, 3, 2

Sometimes that is fine, but it is not always what you want.

To iterate in order you can use the following function (taken from the lua wiki)

local myTable = { ["key1"] = 1, ["key2"] = 2, ["key3"] = 3, } local function \_\_genOrderedIndex( t ) local orderedIndex = {} for key in pairs(t) do table.insert( orderedIndex, key ) end table.sort( orderedIndex ) return orderedIndex end local function orderedNext(t, state) -- Equivalent of the next function, but returns the keys in the alphabetic -- order. We use a temporary ordered key table that is stored in the -- table being iterated. --print("orderedNext: state = "..tostring(state) ) if state == nil then -- the first time, generate the index t.\_\_orderedIndex = \_\_genOrderedIndex( t ) key = t.\_\_orderedIndex[1] return key, t[key] end -- fetch the next value key = nil for i = 1,table.getn(t.\_\_orderedIndex) do if t.\_\_orderedIndex[i] == state then key = t.\_\_orderedIndex[i+1] end end if key then return key, t[key] end -- no more value to return, cleanup t.\_\_orderedIndex = nil return end --This is the function you will use to replace "pairs" local function orderedPairs(t) -- Equivalent of the pairs() function on tables. Allows to iterate -- in order return orderedNext, t, nil end for k, v in orderedPairs(myTable) do print(myTable[k]) end

That will print:

1, 2, 3
  Shuffling a table

Say you are making a card game and want to shuffle your table containing your cards. Here is one function that can do it.

local function shuffle(t) local rand = math.random assert(t, "table.shuffle() expected a table, got nil") local iterations = #t local j for i = iterations, 2, -1 do j = rand(i) t[i], t[j] = t[j], t[i] end end --Usage shuffle(myTable)

Checking if a file exists

This speaks for itself, to check if a file exists you can use a handy function like this:

local function doesFileExist(theFile, path) local thePath = path or system.DocumentsDirectory local filePath = system.pathForFile(theFile, thePath) local results = false local file = io.open(filePath, "r") --If the file exists, return true if file then io.close(file) results = true end return results end --Usage doesFileExist("levels.data") --Returns true/false --Or doesFileExist("levels.data", system.ResourceDirectory) --Returns true/false

  Get distance between two objects

This is a commonly needed function, it simply returns the distance between two objects

local function getDistanceBetween(obj1, obj2) return math.ceil(math.sqrt( ((obj2.y - obj1.y) \* (obj2.y - obj1.y)) + ((obj2.x - obj1.x) \* (obj2.x - obj1.x)) )) end --Usage local plane = display.newRect(100, 100, 40, 40) local bird = display.newRect(200, 100, 40, 40) getDistanceBetween(plane, bird) --In this case it will return 100

  Printing memory usage to the terminal

A lot of people ask how do i monitor my memory usage? Here is a handy function that will print it neatly to the terminal and format it so it is easy to understand.

local function printMemUsage() local memUsed = (collectGarbage("count")) / 1000 local texUsed = system.getInfo( "textureMemoryUsed" ) / 1000000 print("\n---------MEMORY USAGE INFORMATION---------") print("System Memory Used:", string.format("%.03f", memUsed), "Mb") print("Texture Memory Used:", string.format("%.03f", texUsed), "Mb") print("------------------------------------------\n") return true end --Usage printMemUsage() --Best to call when you are finished changing scenes once

[import]uid: 84637 topic_id: 19231 reply_id: 319231[/import]

Here’s a lua implementation of C’s ternary operator:

for those who do not know, in C

if a\>10   
 b=a+1;  
else  
 b=a-1;  
end  

can be written as

b = (a\>10)?(a+1):(a-1)  

Corresponding lua implementation
[lua]b = (a>10) and (a+1) or (a-1)[/lua]

[import]uid: 64174 topic_id: 19231 reply_id: 74191[/import]

How to make a Health Bar

If you are new to corona and wandered how to implement health bar to your game( for certain object or enemy), here’s my little code:

[lua]health_bar = display.newRect(0,0,200,10)
health_bar:setReferencePoint(display.TopLeftReferencePoint)
health_bar.x = 50; health_bar.y = 10
health_bar:setFillColor(0,255,0)
gameGroup:insert(health_bar)[/lua]

Point is to change .xScale property of health_bar upon change of enemy’s health or something like that, to achieve this we can use next function, presumable that enemy has .health = 100 property

[lua]local function onTouch(self,event) – this can be anything, touch, collision, enterFrame, whatever you want to make your enemy depleting health

self.health = self.health - 10 – or any amount of damage you need

if self.health > 0 then
health_bar.xScale = self.health *0.01
–this is simple math. equation that will help you reduce code
end
end

enemy.touch = onTouch
enemy:addEventListener(“touch”, enemy)[/lua]
[import]uid: 16142 topic_id: 19231 reply_id: 74194[/import]

May also be worth including the memory usage function here;

[lua]local function monitorMem(event)
collectgarbage(“collect”)

print( “\nMemUsage: " … (collectgarbage(“count”)/1000) … " MB”)
print("Texture Usage " … system.getInfo( “textureMemoryUsed” ) / 1000000)

return true
end

Runtime:addEventListener(“enterFrame”, monitorMem)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 19231 reply_id: 74498[/import]

Everybody should know that the print function accepts comma separated values so you should never have to use the … operator in a print function. This is much more efficient too because temporary strings don’t have to be created and collected.

print("Value of my bool = ", myBool)  

[import]uid: 7563 topic_id: 19231 reply_id: 74506[/import]

Wow Eric, I did not know that ever! Thanks for it!

“love the commas instead that crazy dots”

:slight_smile:

[import]uid: 89165 topic_id: 19231 reply_id: 74507[/import]

Print a LUA table, really useful during development / debug:

print\_r = function (t, name, indent) local tableList = {} function table\_r (t, name, indent, full) local serial=string.len(full) == 0 and name or type(name)~="number" and '["'..tostring(name)..'"]' or '['..name..']' io.write(indent,serial,' = ') if type(t) == "table" then if tableList[t] ~= nil then io.write('{}; -- ',tableList[t],' (self reference)\n') else tableList[t]=full..serial if next(t) then -- Table not empty io.write('{\n') for key,value in pairs(t) do table\_r(value,key,indent..'\t',full..serial) end io.write(indent,'};\n') else io.write('{};\n') end end else io.write(type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"' or tostring(t),';\n') end end table\_r(t,name or '\_\_unnamed\_\_',indent or '','') end [import]uid: 21692 topic_id: 19231 reply_id: 76275[/import]

winning lottery numbers

10, 15, 32, 41, 44, 3

sorry couldn’t help myself [import]uid: 7911 topic_id: 19231 reply_id: 76278[/import]

Love this one too, I hate those crazy dot!! [import]uid: 4187 topic_id: 19231 reply_id: 93991[/import]

This can get noisy and hard to navigate pretty soon. How about a wiki? [import]uid: 7026 topic_id: 19231 reply_id: 98986[/import]

Added the following snippets:

Iterating through a key/pair table in order
Shuffling a table
Checking if a file exists
Get distance between two objects
Printing memory usage to the terminal [import]uid: 84637 topic_id: 19231 reply_id: 108079[/import]

Stop ‘print()’ output when running on a device (improves performance) without removing the print statements in your code:

[lua]if (system.getInfo(“environment”) == “device”) then
print = function() end
end[/lua]

This may have been in a blog post somewhere, but I can’t find it now. [import]uid: 8271 topic_id: 19231 reply_id: 113737[/import]

using transition.to on a variable

myVal = { myCustomVal = 0 }

transition.to( myVal, { time = 1000, myCustomVal = 10000 }) [import]uid: 7911 topic_id: 19231 reply_id: 124806[/import]

using transition.to on a variable

myVal = { myCustomVal = 0 }

transition.to( myVal, { time = 1000, myCustomVal = 10000 }) [import]uid: 7911 topic_id: 19231 reply_id: 124806[/import]

Here’s two that take advantage of Lua’s Closures:

local function generateCommand(funcPtr, ...)  
 local temp = {n=select('#', ...), ...}  
 return function()  
 return funcPtr(unpack(temp, 1, temp.n))  
 end  
end  

This first one allows you to take a function, and a variable list of parameters (passed in as generateCommand(function, param1, param2, param3…) and creates a parameterless function that when called will call your original function with all params attached. It’s very useful for creating all sorts of variables and linkages on initialization, and then just having behaviours attached to simple callbacks, without having to manage access to all the required parameters. Mostly I use this for button actions, to hookup the entire behaviour on creation but not have a dozen slightly different functions.

local function wrapParam(funcPtr, param)  
 return function(...)  
 return funcPtr(param, ...)  
 end  
end  

This one is similar to generateCommand, but it only attaches 1 parameter, and allows you to call the function with additional arguments. A good use case is if you want to have a Corona callback with a ‘self’ parameters, you can call onPress = wrapParam(self.myFunction, self), and now the Corona callback will be onPress(self, event), or any other parameters you need to attach, rather than just being stuck with the onPress(event) signature. this function can be chained together (pass the result of the function into another wrapParam), to attach multiple parameters. [import]uid: 134101 topic_id: 19231 reply_id: 126284[/import]

Here’s a great code snippet that can really help out in spotting unwanted Global variables creeping into your code (courtesy of Ntero ):

local function globalWatch(g, key, value)  
 print("GlobalWatch --- ", tostring(key) .. " has been added to \_G\n" .. debug.traceback())  
 rawset(g, key, value)  
end  
setmetatable(\_G, { \_\_index = globalWatch })  

Originally posted by Ntero on http://developer.coronalabs.com/forum/2012/09/19/type-being-redefined#comment-124608

Naomi
[import]uid: 67217 topic_id: 19231 reply_id: 126283[/import]

Here’s two that take advantage of Lua’s Closures:

local function generateCommand(funcPtr, ...)  
 local temp = {n=select('#', ...), ...}  
 return function()  
 return funcPtr(unpack(temp, 1, temp.n))  
 end  
end  

This first one allows you to take a function, and a variable list of parameters (passed in as generateCommand(function, param1, param2, param3…) and creates a parameterless function that when called will call your original function with all params attached. It’s very useful for creating all sorts of variables and linkages on initialization, and then just having behaviours attached to simple callbacks, without having to manage access to all the required parameters. Mostly I use this for button actions, to hookup the entire behaviour on creation but not have a dozen slightly different functions.

local function wrapParam(funcPtr, param)  
 return function(...)  
 return funcPtr(param, ...)  
 end  
end  

This one is similar to generateCommand, but it only attaches 1 parameter, and allows you to call the function with additional arguments. A good use case is if you want to have a Corona callback with a ‘self’ parameters, you can call onPress = wrapParam(self.myFunction, self), and now the Corona callback will be onPress(self, event), or any other parameters you need to attach, rather than just being stuck with the onPress(event) signature. this function can be chained together (pass the result of the function into another wrapParam), to attach multiple parameters. [import]uid: 134101 topic_id: 19231 reply_id: 126284[/import]

Here’s a great code snippet that can really help out in spotting unwanted Global variables creeping into your code (courtesy of Ntero ):

local function globalWatch(g, key, value)  
 print("GlobalWatch --- ", tostring(key) .. " has been added to \_G\n" .. debug.traceback())  
 rawset(g, key, value)  
end  
setmetatable(\_G, { \_\_index = globalWatch })  

Originally posted by Ntero on http://developer.coronalabs.com/forum/2012/09/19/type-being-redefined#comment-124608

Naomi
[import]uid: 67217 topic_id: 19231 reply_id: 126283[/import]

Copies a table completely. Pass in a table, it returns a new, unique copy you can modify independently (instead of a typical reference, when assigning in lua).

-- Creates a complete/deep copy of the data function deepCopy(object) local lookup\_table = {} local function \_copy(object) if type(object) ~= "table" then return object elseif lookup\_table[object] then return lookup\_table[object] end local new\_table = {} lookup\_table[object] = new\_table for index, value in pairs(object) do new\_table[\_copy(index)] = \_copy(value) end return setmetatable(new\_table, getmetatable(object)) end return \_copy(object) end

Snagged this somewhere on the boards, it’s fast, it’s tight — pure genius, it’s a keeper. That table print up there looks pretty good too… Hmmm… [import]uid: 79933 topic_id: 19231 reply_id: 131357[/import]

Copies a table completely. Pass in a table, it returns a new, unique copy you can modify independently (instead of a typical reference, when assigning in lua).

-- Creates a complete/deep copy of the data function deepCopy(object) local lookup\_table = {} local function \_copy(object) if type(object) ~= "table" then return object elseif lookup\_table[object] then return lookup\_table[object] end local new\_table = {} lookup\_table[object] = new\_table for index, value in pairs(object) do new\_table[\_copy(index)] = \_copy(value) end return setmetatable(new\_table, getmetatable(object)) end return \_copy(object) end

Snagged this somewhere on the boards, it’s fast, it’s tight — pure genius, it’s a keeper. That table print up there looks pretty good too… Hmmm… [import]uid: 79933 topic_id: 19231 reply_id: 131357[/import]