New To Lua, But Not To Programming?

We all (the whole Renoise team) are new to Lua as well, have not used Lua before it got part of Renoise, so here are some general Tips & Tricks from my past months experience. Maye this helps someone to dig into all the Lua stuff.

Completely new to Lua?
Then start right here: Programming in Lua (introduction) or Lua Reference Manual (reference)

Looking for general Lua code snippets (how to do what in Lua)?
Then this is a good source: Lua user Wiki

Lua counts from 1:
But Renoise counts from 0 in many areas in its GUI. Instruments for example start at 0 in Renoise. To access Renoise instrument no. 0 in Lua, you’ll have to use renoise.song().instruments[1].

Tables:
Lua basically “just” is one of many imperative programming languages. If you have done things with C or Python or JavaScript, then you should quickly find your way. One special thing in Lua is the use of “tables” though. They are used for nearly every complex data types in Lua and are damn powerful, but also need some time to get used to. The meta table concept can be quite irritating as well first, but will make sense at some time. Don’t poke your head too much with meta table in the beginning, but just see/use tables as associative arrays, which can be used as maps, or number based arrays - just like you look at them and need them. This at least helped me dealing with them…

Numbers:
Lua only has one type of numbers, a double precision floating point value. Most of the time this just works. Sometimes, when doing arithmetic stuff with indices, remind taking the floor of the numbers -> my_octave_table[math.floor(note / 12)]

OO programming:
Lua has no classes or real built in OO support, but Renoises Lua impl has. See XRNX docs (classes.lua) for a small rundown

Mind the colon:
Especially when dealing with classes, the use of a dot vs. a colon can be quite confusing at first. Don’t worry, this will make sense at some point. A colon just means: pass whats’s left to it to the called function -> call a method:

  
some_object.some_function(some_object)  
--- is the same as:  
some_object:some_function()  
-- or  
renoise.app():show_error("Ouch!")  
  

basically just syntactic sugar.

but properties need no colon. They are no functions:

  
some_object.some_property -- "member"  
-- or  
renoise.song().transport.bpm  
  

Out of the box, Lua is extremely lightweight
… so we’ve added the rest to make it comfortable for us. Probably you have to add some more stuff to make it comfortable for you. A lot of also very low level stuff has been extended in Renoise to make do so. And all this is Renoise specific. You won’t find info on the web or user wiki about this, but only here: Renoise Lua docs

Somewhere, a little puppy just died because of this.

Thanks for the summary. It’s awesome to have something like this at hand, when starting to learn a new programming language.

For iterating variables, Lua doesn’t have ++ or +=. You use i = i + 1.

  
i = 0  
  
for pos, line in renoise.song().pattern_iterator:lines_in_pattern_track(1, 1) do  
 i = i + 1  
 print(i)  
end  
  

Remember to set the initial variable outside of the for loop.

  
local a = table.create{1, 2, 3}  
local b = a  
b:insert(4)  
rprint(a) -- What do you think happens here?  
  

In lua, strings and numbers are copied when using the = symbol, everything else (table) is a “shortcut”.

When you pass a table to a function, you are passing something like a pointer. If you manipulate the table within the function you are manipulating the original object, not a copy.

Helpful additions to deal with this in the Renoise API include:

rprint, oprint, table.copy, table.rcopy

stares

lolcat! THAT I understand. everything else, whoooosh

it will take a while to sink in.

10 second idle notification

no, that’s not your fault, my brain is mush at this point in the day.

but sure, it could be a bit more verbose ^^ but as long as the stuff you say is true (and I guess it is), it’s still useful, and stuff will begin to make sense over time… I mean if you haven’t even come in touch with most of these issues you can just read it an go “mmmmkaaayyy…”, but that’s not the fault of the tutorial, it’s just all so new and strange hehe.

Doh, not in front of Renoise. Typed it out sloppy with no testing. Fixed now, thanks.

Don’t use Google and surf World of Warcraft forums. Lua implementations differ from platform to platform.

Look here:
http://www.lua.org/manual/5.1/

Then look here:

If it’s not in either, then it’s not in Renoise.

Hello

I’ve just found this link on Hacker News
Learn Lua in 15 minutes

And I found this article to be very concise and helpful.

Cheers

Geoffroy