Jump to content


Photo

Global variables within the "whole script"?


  • Please log in to reply
13 replies to this topic

#1 esaruoho

esaruoho

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2679 posts
  • Gender:Male
  • Location:Kontula, Helsinki, Finland
  • Interests://Varying Interests//

Posted 17 January 2018 - 10:55

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?

 


http://lackluster.org | on Bandcamp | on YouTube | on Facebook | on Spotify | on iTunes | on TiDAL | on Soundcloud | 

http://twitter.com/esaruoho | http://esaruoho.tumblr.com | GitHubesaruoho@gmail.com | 

| macOS 10.13 High Sierra | MacBook Pro 2.8 GHz Intel Core i7 / 16 Gb 1600 MHz DDR 3 |

| http://renoise.com/tools/paketti | BTC: 1BoZ9rvb8wT7dFNyVPwSX7DLj3stNo7844 | http://paypal.me/esaruoho |

| Latent Impulse Tracker/Scream Tracker 3/Schism Tracker <-> Renoise fanatic |

More API functions, please! ;) http://forum.renoise.com/index.php/topic/26329-the-api-wishlist-thread/page-3#entry240266

#2 danoise

danoise

    Probably More God or Borg Than Human Member

  • Renoise Team
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 6989 posts
  • Gender:Male
  • Location:Berlin
  • Interests:wildlife + urban trekking

Posted 17 January 2018 - 11:09

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 as local 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

Tracking with Stuff. API wishlist | Soundcloud


#3 joule

joule

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

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

Posted 17 January 2018 - 11:11

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.



#4 Ledger

Ledger

    Guruh Motha Fakka Knows More About Renoise Than Taktik

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3648 posts
  • Gender:Male

Posted 17 January 2018 - 14:03

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/...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'

Edited by Ledger, 17 January 2018 - 14:25.

  • esaruoho likes this

--> Lua for newbies <--

My Scripts On Forum

Top Tip!

 

cpu : Xeon 1231 v3, os : Win 7 64bit, audio: Audient iD4
posts as 4tune @ KvR and some other music related sites


#5 esaruoho

esaruoho

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2679 posts
  • Gender:Male
  • Location:Kontula, Helsinki, Finland
  • Interests://Varying Interests//

Posted 17 January 2018 - 14:37

 

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/...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.


http://lackluster.org | on Bandcamp | on YouTube | on Facebook | on Spotify | on iTunes | on TiDAL | on Soundcloud | 

http://twitter.com/esaruoho | http://esaruoho.tumblr.com | GitHubesaruoho@gmail.com | 

| macOS 10.13 High Sierra | MacBook Pro 2.8 GHz Intel Core i7 / 16 Gb 1600 MHz DDR 3 |

| http://renoise.com/tools/paketti | BTC: 1BoZ9rvb8wT7dFNyVPwSX7DLj3stNo7844 | http://paypal.me/esaruoho |

| Latent Impulse Tracker/Scream Tracker 3/Schism Tracker <-> Renoise fanatic |

More API functions, please! ;) http://forum.renoise.com/index.php/topic/26329-the-api-wishlist-thread/page-3#entry240266

#6 Ledger

Ledger

    Guruh Motha Fakka Knows More About Renoise Than Taktik

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3648 posts
  • Gender:Male

Posted 17 January 2018 - 14:56

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..


Edited by Ledger, 17 January 2018 - 14:56.

--> Lua for newbies <--

My Scripts On Forum

Top Tip!

 

cpu : Xeon 1231 v3, os : Win 7 64bit, audio: Audient iD4
posts as 4tune @ KvR and some other music related sites


#7 esaruoho

esaruoho

    Guruh Motha Fakka is Levitating and Knows Everything About Renoise Member

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPip
  • 2679 posts
  • Gender:Male
  • Location:Kontula, Helsinki, Finland
  • Interests://Varying Interests//

Posted 17 January 2018 - 14:59

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.


http://lackluster.org | on Bandcamp | on YouTube | on Facebook | on Spotify | on iTunes | on TiDAL | on Soundcloud | 

http://twitter.com/esaruoho | http://esaruoho.tumblr.com | GitHubesaruoho@gmail.com | 

| macOS 10.13 High Sierra | MacBook Pro 2.8 GHz Intel Core i7 / 16 Gb 1600 MHz DDR 3 |

| http://renoise.com/tools/paketti | BTC: 1BoZ9rvb8wT7dFNyVPwSX7DLj3stNo7844 | http://paypal.me/esaruoho |

| Latent Impulse Tracker/Scream Tracker 3/Schism Tracker <-> Renoise fanatic |

More API functions, please! ;) http://forum.renoise.com/index.php/topic/26329-the-api-wishlist-thread/page-3#entry240266

#8 triple zero

triple zero

    Advanced Member

  • Normal Members
  • PipPipPip
  • 70 posts

Posted 24 February 2018 - 18:15


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.


  • Ledger likes this

#9 Ledger

Ledger

    Guruh Motha Fakka Knows More About Renoise Than Taktik

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3648 posts
  • Gender:Male

Posted 24 February 2018 - 19:47

 

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..


--> Lua for newbies <--

My Scripts On Forum

Top Tip!

 

cpu : Xeon 1231 v3, os : Win 7 64bit, audio: Audient iD4
posts as 4tune @ KvR and some other music related sites


#10 triple zero

triple zero

    Advanced Member

  • Normal Members
  • PipPipPip
  • 70 posts

Posted 27 February 2018 - 20:45



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.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!)


Edited by triple zero, 27 February 2018 - 20:45.

  • Ledger likes this

#11 Ledger

Ledger

    Guruh Motha Fakka Knows More About Renoise Than Taktik

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3648 posts
  • Gender:Male

Posted 27 February 2018 - 21:22

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

 

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.


  • triple zero likes this

--> Lua for newbies <--

My Scripts On Forum

Top Tip!

 

cpu : Xeon 1231 v3, os : Win 7 64bit, audio: Audient iD4
posts as 4tune @ KvR and some other music related sites


#12 danoise

danoise

    Probably More God or Borg Than Human Member

  • Renoise Team
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 6989 posts
  • Gender:Male
  • Location:Berlin
  • Interests:wildlife + urban trekking

Posted 27 February 2018 - 23:08

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. 


  • Ledger, Conner_Bw and triple zero like this

Tracking with Stuff. API wishlist | Soundcloud


#13 Ledger

Ledger

    Guruh Motha Fakka Knows More About Renoise Than Taktik

  • Normal Members
  • PipPipPipPipPipPipPipPipPipPipPipPipPipPip
  • 3648 posts
  • Gender:Male

Posted 28 February 2018 - 00:09

No Shoggoths? Boooo!

 

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


  • triple zero likes this

--> Lua for newbies <--

My Scripts On Forum

Top Tip!

 

cpu : Xeon 1231 v3, os : Win 7 64bit, audio: Audient iD4
posts as 4tune @ KvR and some other music related sites


#14 triple zero

triple zero

    Advanced Member

  • Normal Members
  • PipPipPip
  • 70 posts

Posted 03 April 2018 - 18:11

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:


  • Ledger likes this