Global variables within the "whole script"?

Hi, is there some way of defining a “global” variable within main.lua?

Like

s=renoise.song()

for instance, so that every single function would have it within main.lua?

Scope depends on where & how the variable is declared.

What you just did would indeed be accessible from anywhere in main.lua - as well as any other file you imported in your tool.

If you had declared it as local s =renoise.song(), then it would be accessible_only_within main.lua, but not other files.

If you had declared it aslocal s =renoise.song()within the scope of some function, it would only be accessible within that scope/function.

So, as you might understand it’s actually bad practice not to prefix things with local, unless you want to risk variables clashing with each other

(it gets tricky to keep the overview once you split code into multiple files…)

PS: If you’re planning to keep a single reference to renoise.song(), you definitely want to renew this variable once the song gets replaced

by attaching a notifier to this

renoise.tool().app_new_document_observable

A “global” variable would be a variable that is available directly under the tools environment table _G

What you suggested is achieving exactly that. Just make sure that it’s declared before you’re trying to access it.

s = renoise.song() will be available in all “files”.

local s = renoise.song() will only be available from the same “file”.

PS. I think that avoiding the use of global variables will generally lead to better coding practice.

On the subject I noticed that global shows up as a blue keyword in the editor/terminal

Not part of the standard, so what does it actually do? or is it just to visually mark the declaration?

https://www.lua.org/manual/5.1/manual.html

The following keywords are reserved and cannot be used as names:

     and break do else elseif
     end false for function if
     in local nil not or
     repeat return then true until while

----EDIT;

actually it fires an error if you try and declare a variable:… so still none the wiser…

7 global my_global = 10
8 print(my_global)
*** main.lua:7: '=' expected near 'my_global'

On the subject I noticed that global shows up as a blue keyword in the editor/terminal

Not part of the standard, so what does it actually do? or is it just to visually mark the declaration?

https://www.lua.org/manual/5.1/manual.html

The following keywords are reserved and cannot be used as names:

and break do else elseif
end false for function if
in local nil not or
repeat return then true until while

----EDIT;

actually it fires an error if you try and declare a variable:… so still none the wiser…

7 global my_global = 10
8 print(my_global)
*** main.lua:7: '=' expected near 'my_global'

That global thing being “ok” yet “not ok” does seem quite weird to me, isn’t it?
I mean, I wouldn’t expect s=renoise.song() to just work globally without me defining “something” somewhere. Not just dumping it into main.lua willynilly. I’ll try it to see what happens, but feels odd.

That global thing being “ok” yet “not ok” does seem quite weird to me, isn’t it?
I mean, I wouldn’t expect s=renoise.song() to just work globally without me defining “something” somewhere. Not just dumping it into main.lua willynilly. I’ll try it to see what happens, but feels odd.

Yes just typing a variable without local does make it by default a global. I agree it would seem neater with a keyword, just to flag it visually as a declaration. I tend to declare all globals at the same point in the main.lua file and just comment them for clarity.

On the other hand the global keyword does exist in the editor/terminal, but my question is what is it for? as it doesn`t seem to work in a variable declaration…

Yes just typing a variable without local does make it by default a global. I agree it would seem neater with a keyword, just to flag it visually as a declaration. I tend to declare all globals at the same point in the main.lua file and just comment them for clarity.

On the other hand the global keyword does exist in the editor/terminal, but my question is what is it for? as it doesn`t seem to work in a variable declaration…

from my point of view, it would be cool if there was some way of declaring the same global variable for the terminal, too. the main reason why i wasn’t using shortenings for brevity, was because then i’d suddenly not be able to paste them into the terminal as they wouldn’t work.

On the other hand the global keyword does exist in the editor/terminal, but my question is what is it for? as it doesn`t seem to work in a variable declaration…

I’m not sure about Lua so this is just a guess, but maybe it’s similar to the “mistakes were made” in Javascript? Javascript has a couple of keywords that were initially reserved for future use, but some of them were never used and I don’t really expect them to be.

I’m not sure about Lua so this is just a guess, but maybe it’s similar to the “mistakes were made” in Javascript? Javascript has a couple of keywords that were initially reserved for future use, but some of them were never used and I don’t really expect them to be.

Could be, I couldn`t find anything definitive with a quick google unfortunately,

maybe one for Danoise or taktik to answer?, not a big issue, it`s just a curiosity…

Could be, I couldn`t find anything definitive with a quick google unfortunately,

maybe one for Danoise or taktik to answer?, not a big issue, it`s just a curiosity…

If my guess was correct, I’d expect to find a definitive answer in an official Lua formal language specification or other. Now, Lua only has a reference manual (the difference being the former specifices what a Lua parser+compiler+runtime should conform to, while the latter is a detailed reference on how to use it), which you already alluded to earlier ITT.

I must not have quite been paying attention the other day–being distracted making sure my comment about Javascript was still correct because something seems to have changed about the keywords since the ES2015 standard but let’s not get distracted again–because you already linked the Lua reference manual that lists all the keywords and “global” isn’t in there:

https://www.lua.org/manual/5.3/manual.html#3.1 (that’s 5.3, I’m not actually sure which version of Lua is running in Renoise but the keyword “global” is neither in the 5.1 you linked nor in 5.3)

Lacking a formal specification, let’s assume the reference manual doubles as the spec. Meaning there is no “global” keyword in your vanilla garden-variety Lua runtime.

What we know is that the Renoise code editor syntax-highlights the word “global” as if it was a keyword.

What we don’t know is whether Renoise’s Lua runtime is either vanilla or of the garden-variety. Meaning the developers could have taken a Lua runtime to include in Renoise (which is what you do, you don’t go implement your own Lua parser from scratch if you’re meant to be developing DAW software) and modified it with additional funky stuff and possibly an extra keyword or so. Except that I kind of doubt that.

One, because the “global” keyword doesn’t seem to do anything (but maybe we’re just holding it wrong) and two, because I read in another thread that they use Lua/C bindings that were specially designed for programs like Renoise to expose their API to Lua, in the way that the Lua was meant to be used as an included runtime. This involves something object-oriented like (except the objects are tables because Lua), but surely does not involve adding extra keywords, because those are defined at the parser stage, which comes way before the moment when bindings and stuff come into play.

That doesn’t mean they didn’t also do that or that they did not hide a super secret Easter egg involving the “global” keyword and five XY-pads on which you are to draw the sigils of the LBRP in order to attempt to unlock the Ultimate Everything Soundgooder device (but if you do it wrong, a Shoggoth will eat your sample collection).

… but, it could also be that they accidentally included an extra keyword to syntax highlight when writing the Renoise Lua code editor.

(if I never post again after this, you’ll know what to believe except you will also know what NOT to believe if you want to keep your sanity … ia ia! fhtaghn!)

So far Ive not encountered a Shoggoth, but if I was going to add a rotten easter egg that ate sample collections,Shoggoth` would be the keyword! :slight_smile:

My guess is that it was something like global was added early in rns 2.6 development and then not needed later. As an example renoise-Lua, does include the class keyword for it`s basic OO system. This also hilights in the editor but is not part of the basic Lua spec.

As an example renoise-Lua, does include the class keyword for it`s basic OO system. This also hilights in the editor but is not part of the basic Lua spec.

I can of course figure out why exactly this keyword is being highlighted, but don’t expect any Shoggoths to appear … I think smilie_schreck.gif

What I do know is that, yes, there’s a little extra syntactic sugar added to the vanilla lua - not just because of luabind (the framework used for C++ bindings and basic class/OO) but also because the Renoise API defines extra methods - such as rprint, ripairs, etc.

No Shoggoths? Boooo!

But the global mystery remains…(and no, I don`t mean that thread! …)

It’s great to be on a forum where I can use the word “Shoggoth” in casual conversation and people just go with it and know what I’m talking about :badteeth: