Skip to content

Instantly share code, notes, and snippets.

@eeropic
Last active September 13, 2021 12:04
Show Gist options
  • Save eeropic/d415c61979cbf4f5d596d8a55cd71a14 to your computer and use it in GitHub Desktop.
Save eeropic/d415c61979cbf4f5d596d8a55cd71a14 to your computer and use it in GitHub Desktop.
Lua tips for TouchOSC scripting (Lua 5.1)
  • You can omit parenthesis in function calls, if your argument is a string or a table
myFunc"hello"
myFunc{1,2,3}
  • Variable number of function arguments
function myFunc(...)
  local args = {...}
end

Example: Join n number of tables

function joinTables(...)
  local tables = {...}; local result = {};
  for _,t in pairs(tables) do
    for k,v in pairs(t) do table.insert(result,v) end
  end
  return result
end

--usage:
local joinedTables = joinTables({1,2,3},{4,5,6})
--output: {1,2,3,4,5,6}
  • Extract part of a table into a new table
local partialTable = {table.unpack(myTable, from_index, to_index)}
  • Assign multiple variables in one line
local x,y,z = 1,2,3
  • Return multiple values from a function
function multipleReturns()
  local a = "foo"
  local b = "bar"
  return a,b
end

local r1,r2 = multipleReturns()
-- r1 = "foo", r2 = "bar"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment