then unzipped that file and opened the file “! Introduction.txt.html” in my web browser (it’s in XrnxStarterPack270\Documentation\html\ )
so…
since this is Da Sandbox, i’ll just plunge into that Introduction chapter and report in this thread what i don’t quite understand (what needs further investigation)
i can see that there is a reference list of “all available Lua functions and classes that control the Renoise application” in the chapter titled “Renoise Application API Reference”
Would it be fair to say that typically an API assumes some familiarity with the programming language it is based around. IE you would have to study LUA itself, not Renoise’s API, to get the syntax and relationship before Functions, Classes, Observables, Functions etc. It is not the API’s job to teach you how to correctly write an If/Else loop (if they exist for example) but to tell you what of the existing LUA library has been implemented and what functions there are to interact with Renoise.
however, as i’m reading the docs for the first time, i’ll just try out things as i read, to get a first impression and basic frame of reference before i plunge into the Lua specifics
often it’s fruitful for me to simply dive into something and experiment, while investigating things on the go (looking up concepts, reading about Lua syntax, etc)
@.xrns: i appreciate your efforts, and i think it is a good way to start this exploration, as it would probably be the way most would start out doing this stuff. funny thing is how fast it branches out into different questions. for example, i started out the same way you did, but never really tried the oprint() thingy to see the other options. so essentially you’re documenting your own road through the documentation, but it does not matter, it is still very useful.
@kazakore: if you had not said it, i would have. completely true. but it might be a good idea to mention this somewhere on the http://scripting.renoise.com site as well. the way it is stated there, it seems you never need to bother with the lua docs, which turns out not to be true (in most cases).
-- Class
class "Person"
function Person:__init(name)
self.name = name
end
function Person:eat()
print(self.name .. " is eating cake.")
end
-- Objects
p1 = Person("Jane")
p1:eat()
p2 = Person("Joe")
p2:eat()
--[[ Output
Jane is eating cake.
Joe is eating cake.
]]--
The Class is the blueprint, the Object is the real thing. (there are exceptions, eg static methods, singletons, …, but that’s not beginner stuff)
The API is split into Classes. You the programer make Objects out of Renoise Classes and do something with them.
-- Class
class "Person"
function Person:__init(name,sex)
self.name = name
self.sex = sex
end
function Person:what()
print(self.name .. " is " .. self.sex)
end
-- Objects
p1 = Person("Jane","female")
p1:what()
p2 = Person("Joe","male")
p2:what()
--[[ Output
Jane is female.
Joe is male.
]]--
@.xrns: functions need to be placed ON objects, attached to them, so to speak… that is why you get the OBJECT:FUNCTION() construction. as Conner explained earlier, a Class is the blueprint for an object. so, he uses the ‘class’ command to make a new object called ‘person’, and attaches a function to it.
‘self’ refers to the object. so instead of ‘self.name’ you could also write ‘Person.name’ i suppose. again, the property is attached to the object, so OBJECT.PROPERTY … but ‘self’ is a shortcut by which the object refers to, well, itself.
take a look at my example with 2 arguments. i think you can do ‘self.x = name’ no problem. unless i missed something and you can only do self.SOMETHING if .SOMETHING exists as a property on the Blueprint Class?
i also noticed by testing that the class name doesn’t need to be the same as the functions in that class (just to answer myself earlier in this thread)
so it’s perfectly fine to have:
class "Fruitcake"
function Fruit:__init(name,color)
Fruit.name = name
Fruit.color = color
end
function Fruit:what_color()
print(Fruit.name .. " is " .. Fruit.color)
end
f1 = Fruit("Apple","green")
f1:what_color()
f2 = Fruit("Banana","yellow")
f2:what_color()
and of course now i can use for example
oprint(f2)
to get this information about that specific object:
class: Fruit
methods:
__init
what
what_color
nice!
EDIT: actually, it’s not as nice as i first thought because why does it return “class: Fruit” instead of “class: Fruitcake” ? i need to investigate this a bit further…
EDIT2: ok, seems like previous editing is retained in memory when testing things out in the terminal editor
actually i think i’m turning into fruitcake over this issue:
i shut down renoise just to get rid of everything global
then opened renoise again and started my TestPad.lua which consists of the following:
class "Fruitcake"
function Fruit:__init(name,color)
Fruit.name = name
Fruit.color = color
end
function Fruit:what_color()
print(Fruit.name .. " is " .. Fruit.color)
end
f1 = Fruit("Apple","green")
f1:what_color()
f2 = Fruit("Banana","yellow")
f2:what_color()
now, running this results in the following error:
*** TestPad.lua:2: variable 'Fruit' is not declared
*** stack traceback:
*** [C]: in function '_error'
*** [string "local mt = getmetatable(_G)..."]:29: in function
*** TestPad.lua:2: in main chunk
so i guess that i have to take back what i wrote above that class name and class functions don’t need to have the same name
conclusion: they must have the same name, it’s not enough that the functions are stacked up under the “class” marker
Sorry guys, i take it back. I wasn’t really looking attentively.
Incorrect:
class "Fruit"
function Fruit:__init(name,color)
Fruit.name = name
Fruit.color = color
end
function Fruit:what_color()
print(Fruit.name .. " is " .. Fruit.color)
end
local f1 = Fruit('Apple', 'Red')
local f2 = Fruit('Pear', 'Green')
print(f1:what_color())
-- Output is: Pear is Green
Output is obviously wrong.
What we want is:
class "Fruit"
function Fruit:__init(name,color)
self.name = name
self.color = color
end
function Fruit:what_color()
print(self.name .. " is " .. self.color)
end
local f1 = Fruit('Apple', 'Red')
local f2 = Fruit('Pear', 'Green')
print(f1:what_color())
-- Output is: Apple is Red
There’s difference between Fruit and self.
Self means assign the value to myself, where as fruit (sort of) means “assign the value to the blueprint, affect everyone”
It was a typo, but it didn’t barf on me. I fixed it. As explained in another thread, colon is a shortcut.
I’m not sure. I would never write Lua like that. The behaviour falls under “unexpected” to me. In something like PHP it might be “static” but I’m not sure what it is in Lua.
This should also emphasize that you don’t have to know everything about a language to get work done. I don’t know everything, doesn’t stop me in the least.