Jump to content


Photo

New To Lua, But Not To Programming?


  • Please log in to reply
13 replies to this topic

#1 taktik

taktik

    Renoise Developer

  • Admins
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 15040 posts
  • Gender:Male
  • Location:Berlin, Germany
  • Interests:füße waschen

Posted 20 July 2010 - 03:21

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:
[lua]
some_object.some_function(some_object)
--- is the same as:
some_object:some_function()
-- or
renoise.app():show_error("Ouch!")
[/lua]

basically just syntactic sugar.

but properties need no colon. They are no functions:
[lua]
some_object.some_property -- "member"
-- or
renoise.song().transport.bpm
[/lua]

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
  • Conner_Bw, Djeroek, dr4g0n76 and 5 others like this

#2 MarvelousMarvin

MarvelousMarvin

    Super Advanced Member

  • Normal Members
  • PipPipPipPip
  • 158 posts
  • Location:Teh Interwebs

Posted 20 July 2010 - 10:55

Lua counts from 1

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.
  • wahrk and bitbear like this

#3 epc

epc

    Member

  • Normal Members
  • PipPip
  • 49 posts

Posted 20 July 2010 - 15:59

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.
os: OS X 10.6.4

#4 Conner_Bw

Conner_Bw

    Probably More God or Borg Than Human Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 7043 posts
  • Gender:Male
  • Location:Montreal, Quebec, Canada

Posted 20 July 2010 - 19:43

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

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

Edited by Conner_Bw, 20 July 2010 - 21:39.

cpu Lenovo X220, Intel i7-2640M @ 2.80GHz ×4 os Windows 10 / Ubuntu 16.04 LTS
My Homepage » : My Renoise Tools » : Normalize Your Sig » : One million clicks! »


#5 Guest_Bantai_*

Guest_Bantai_*
  • Guests

Posted 20 July 2010 - 20:18

Syntactic sugar. You may be freaked out to see something like this

local colors = table.create{'red', 'green', 'blue'}

colors:insert('purple')

for _,color in ipairs(colors) do
print(color)
end


- table.create{} is a shortcut for table.create({}). Combined with closures, makes for interesting looking code:

    function digitButton (digit)
return Button{ label = digit,
action = function ()
add_to_display(digit)
end
}
end



- A table created like

local t = table.create()

accepts functions with the colon operator to refer to itself.


- A table created like local t = {} is more like a primitive, so you need to feed it to a function to modify it, eg.

table.insert(t, 'yellow')


- An interesting variation on the above is the string literal. Perhaps a bit contradictory.

print( ("Hello World"):lower() )


- The underscore (_) is conventionally used as a temporary variable in loops. If you're not interested in the key of the key/value pair, the underscore is a way to say "I don't care about you, key".


- Lua also supports VarArgs, allowing you to create functions that single AND multiple arguments.

function cli_options(...)
for _,v in ipairs{...} do
print(v)
end
end

cli_options('--help')
cli_options('-v', '-a')
cli_options({'-format','-verbose'})

results in

--help
-v
-a
table: 09BCC0B0

In fact, print() itself is a VarArg function.

function cli_options(...)
print(...)
end

--help
-v  -a
table: 09BB8660


#6 Johann

Johann

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2774 posts
  • Gender:Not Telling

Posted 20 July 2010 - 20:58

*stares*

A table created like local t = {}


lolcat! THAT I understand. everything else, *whoooosh*

it will take a while to sink in.

music / photos / renoise scripts


hay guises! action/reaction. I'm not into leaving or avoiding places, I ban 'em by making sure I get banned.. loadsa noise, little signal.. basically anything else makes more sense to spend energy on. thanks for the app, but from here on out we simply don't have any use for each other period. oh, and of course big shoutouts to bit-arts and kaneel and anyone else I forgot :D :D

#7 Guest_Bantai_*

Guest_Bantai_*
  • Guests

Posted 20 July 2010 - 21:09

*stares*


Hm, apparently I'm not a very good teacher. I recommend trying the examples above in TestPad.lua.

#8 s-n-s

s-n-s

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2029 posts
  • Gender:Male

Posted 20 July 2010 - 21:26

local foo = { 1. 2. 3}
local bar = foo
bar:insert(4)
rprint(foo) -- What do you think happens here?



10 second idle notification

Subbase - Danish Dark DnB


#9 Guest_Bantai_*

Guest_Bantai_*
  • Guests

Posted 20 July 2010 - 21:32

10 second idle notification


LOL!

There were some typos in that example, try this

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

When you add a new value to b, the original table will be changed. It's similar to a cloned pattern in Renoise.

#10 Johann

Johann

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2774 posts
  • Gender:Not Telling

Posted 20 July 2010 - 21:36

Hm, apparently I'm not a very good teacher.


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.

Edited by Johann, 20 July 2010 - 21:37.

music / photos / renoise scripts


hay guises! action/reaction. I'm not into leaving or avoiding places, I ban 'em by making sure I get banned.. loadsa noise, little signal.. basically anything else makes more sense to spend energy on. thanks for the app, but from here on out we simply don't have any use for each other period. oh, and of course big shoutouts to bit-arts and kaneel and anyone else I forgot :D :D

#11 Conner_Bw

Conner_Bw

    Probably More God or Borg Than Human Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 7043 posts
  • Gender:Male
  • Location:Montreal, Quebec, Canada

Posted 20 July 2010 - 21:40

LOL!

There were some typos in that example, try this


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

cpu Lenovo X220, Intel i7-2640M @ 2.80GHz ×4 os Windows 10 / Ubuntu 16.04 LTS
My Homepage » : My Renoise Tools » : Normalize Your Sig » : One million clicks! »


#12 Guest_Bantai_*

Guest_Bantai_*
  • Guests

Posted 20 July 2010 - 21:42

Of course, we Renoise Team Members have worked for months with Lua. While for most of you it will be maybe the second day.

All the more important I jot it down now, because in another x months it may be too trivial to notice at all :D

#13 Conner_Bw

Conner_Bw

    Probably More God or Borg Than Human Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 7043 posts
  • Gender:Male
  • Location:Montreal, Quebec, Canada

Posted 20 July 2010 - 22:08

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:
https://code.google....k/Documentation

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

cpu Lenovo X220, Intel i7-2640M @ 2.80GHz ×4 os Windows 10 / Ubuntu 16.04 LTS
My Homepage » : My Renoise Tools » : Normalize Your Sig » : One million clicks! »


#14 geoffroy

geoffroy

    Advanced Member

  • Normal Members
  • PipPipPip
  • 80 posts

Posted 25 June 2013 - 09:45

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
  • Ledger, catchphrase and radian like this