Variable For "renoise.song()"

For a bit of shorthand a typing I have been starting my scripts with a few little variables that shorted what you have to write to access many of the functions and attributes of the API.

“local rns = renoise.song()” being one of the main ones.

This works perfectly while Renoise is open and I’m writing/patching a script. Can load New songs, edit, etc etc and no worries, all seems happy.

Now I export the script and load into Renoise. Seems to go OK. The every time you start Renoise you get a message that: “std::logic_error: ‘trying to access a nil object of type ‘class RenoiseSong’. the object is not or no longer available.’”

It appears this is likely because the Scripts are being loaded before the Template Song is opened, thus there is no renoise.song() to refer to (would that be a fair description?) Clicking Reload All Tools and again everything works perfectly for the whole session.

So do you need to declare this type of variable within each and every Function? Seems a little much for what is just a little shorthand saving work. I have seen others go even further with it, taking whole attributes down to a couple of letters. Surely you should be able to describe you API shorthand within the first few lines of code with no conflicts and problems.

Ahhh OK. I think that makes sense. Found I had to do the same with math.random() {IE use rnd = math.random and then use rnd() when I wanted to call it.}

As long as it doesn’t end in parenthesis “()” are you OK to declare it as a variable anywhere then?

EG:
local inst = renoise.song().selected_instrument
even though it contains renoise.songb[/b] within the string?

How come it is renoise.song().selected_instrument and not renoise.song.selected_instrument?

Been slowly reading through the PIL but have to admit not done overly well and keep in skipping to needed parts/Google what I want before it truly sinks in properly. Not done Object Based programming before and used to having to define what a variable is (string, integer, float) etc. Object. Classes. Classes within classes. All Variables being a multi-formate Table. Plus getting my head around a new syntax after not touching any kind of programming (except the most basic html and css) for a long time so excuse me if it takes a little to sink in at time. I think I’m getting there slowly though…

P.S.

local t = renoise.tool()
t.XXXX.XXXX

works fine at all times as far as I can tell. Correctly I guess I should really use:

local t = renoise.tool
t().XXXX.XXXX

that’s why i set the local s=renoise.song() inside a function, so it is set when called. seems to save me the grief of restart-renoise-renoise.song()-issues.