_G and _ENV
Hello. I am reading the chapter about environments from *Programming in Lua, 4th edition*, and also did online searching regarding `_G` and `_ENV`. Here are my observations and experimenting for record. Feel free to comment if you have something to add or correct.
* The `_ENV` is a local variable for the compiled chunk but can simulate a *global* environment by the compiler's mechanism of prepending all free names with `_ENV.` . Creating and accessing a global variable e.g. `a = 5 print(a)` is equivalent to `_ENV.a = 5 print(_ENV.a)`.
* The `_ENV` is initialized to be the same as `_G`, because `_G == _ENV -- true` (having the same memory address).
* `_ENV` contains a field of `_G`, and `_G` contains a field of `_G` as well.
* A global variable assignment such as `a = 5` puts a key of "a" and value of 5 in `_ENV` and `_G` simultaneously because they are still the same table. Evidenced as below:
​
print(_ENV) -- table: 0x6000017d8040
print(_G) -- table: 0x6000017d8040
a = 5
print(_ENV.a) -- 5
print(_G.a) -- 5
print(_ENV) -- table: 0x6000017d8040
print(_G) -- table: 0x6000017d8040
The result is the same if `a = 5` is replaced by `_ENV = 5` or `_G = 5`.
* However, if `_ENV` is assigned a new table, the memory addresses of `_G` and `_ENV` will be different, but the rule that the *global* environment is `_ENV` still applies.
* Example of overriding the \_ENV:
​
print(_ENV) -- table: 0x600001844200
print(_G) -- table: 0x600001844200
_ENV = {_G = _G, print = _ENV.print} -- can also be print = print or _G.print
print(_ENV) -- table: 0x600001840140 -- different from before
print(_G) -- table: 0x600001844200
a = 5
print(a) -- 5
print(_ENV.a) -- 5
print(_G.a) -- nil
In this case, `_G` will be prepended as `_ENV._G`, but since it was never added the field of "a", `_G.a` is `nil`.
https://redd.it/1m5766o
@r_lua
Hello. I am reading the chapter about environments from *Programming in Lua, 4th edition*, and also did online searching regarding `_G` and `_ENV`. Here are my observations and experimenting for record. Feel free to comment if you have something to add or correct.
* The `_ENV` is a local variable for the compiled chunk but can simulate a *global* environment by the compiler's mechanism of prepending all free names with `_ENV.` . Creating and accessing a global variable e.g. `a = 5 print(a)` is equivalent to `_ENV.a = 5 print(_ENV.a)`.
* The `_ENV` is initialized to be the same as `_G`, because `_G == _ENV -- true` (having the same memory address).
* `_ENV` contains a field of `_G`, and `_G` contains a field of `_G` as well.
* A global variable assignment such as `a = 5` puts a key of "a" and value of 5 in `_ENV` and `_G` simultaneously because they are still the same table. Evidenced as below:
​
print(_ENV) -- table: 0x6000017d8040
print(_G) -- table: 0x6000017d8040
a = 5
print(_ENV.a) -- 5
print(_G.a) -- 5
print(_ENV) -- table: 0x6000017d8040
print(_G) -- table: 0x6000017d8040
The result is the same if `a = 5` is replaced by `_ENV = 5` or `_G = 5`.
* However, if `_ENV` is assigned a new table, the memory addresses of `_G` and `_ENV` will be different, but the rule that the *global* environment is `_ENV` still applies.
* Example of overriding the \_ENV:
​
print(_ENV) -- table: 0x600001844200
print(_G) -- table: 0x600001844200
_ENV = {_G = _G, print = _ENV.print} -- can also be print = print or _G.print
print(_ENV) -- table: 0x600001840140 -- different from before
print(_G) -- table: 0x600001844200
a = 5
print(a) -- 5
print(_ENV.a) -- 5
print(_G.a) -- nil
In this case, `_G` will be prepended as `_ENV._G`, but since it was never added the field of "a", `_G.a` is `nil`.
https://redd.it/1m5766o
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
Need beta testers for HRAM (hand-rolled assembly machine)
Hi everyone. I'm making an app called HRAM (hand-rolled assembly machine), and I plan to release it this week. But I need some beta testers first. Please send me an email at admin@90s.dev if you're interested. Your feedback will be helpful enough that I'll give you a free license. The app is only for Windows (10 or 11).
The app is programmable via Lua and has an assembly library built in, so you can create and run assembly functions at runtime. It has a 320x180 pixel screen that you can manipulate to help you practice assembly. The point of the app is to help learn low level concepts, in the fun environment of making a retro style game. I'm also in the process of adding threading/mutexes/etc also, but that may have to wait post release.
Current manual is at https://hram.dev/docs.txt
https://redd.it/1m5jfw0
@r_lua
Hi everyone. I'm making an app called HRAM (hand-rolled assembly machine), and I plan to release it this week. But I need some beta testers first. Please send me an email at admin@90s.dev if you're interested. Your feedback will be helpful enough that I'll give you a free license. The app is only for Windows (10 or 11).
The app is programmable via Lua and has an assembly library built in, so you can create and run assembly functions at runtime. It has a 320x180 pixel screen that you can manipulate to help you practice assembly. The point of the app is to help learn low level concepts, in the fun environment of making a retro style game. I'm also in the process of adding threading/mutexes/etc also, but that may have to wait post release.
Current manual is at https://hram.dev/docs.txt
https://redd.it/1m5jfw0
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
Can someone make a code that makes my gun recoil in the opposite direction to where I'm pointing please?
Excuse me for asking this here, but I'm trying to make a Terraria weapon in Gmod, and as a secondary attack, it makes a noise like the Tau-Cannon and sends me far back. If someone can do it or give me instructions on how to do it, I would appreciate it. Thanks in advance.
I'm not uploading the code because it won't let me for some reason
https://redd.it/1m5qjo9
@r_lua
Excuse me for asking this here, but I'm trying to make a Terraria weapon in Gmod, and as a secondary attack, it makes a noise like the Tau-Cannon and sends me far back. If someone can do it or give me instructions on how to do it, I would appreciate it. Thanks in advance.
I'm not uploading the code because it won't let me for some reason
https://redd.it/1m5qjo9
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
FiveM Lua Dev
Hi there!
I'm currently looking to take on some Fiverr gigs at a very affordable rate. I have over 2 years of experience in Lua development and a year of experience as a full-stack developer.
Whether you need a simple noscript or a large-scale project, I can deliver clean, documented, reliable code for just a few bucks. I'm building up my Fiverr portfolio and would love to help bring your ideas to life!
Feel free to reach out - let’s work together!
https://redd.it/1m5ud8e
@r_lua
Hi there!
I'm currently looking to take on some Fiverr gigs at a very affordable rate. I have over 2 years of experience in Lua development and a year of experience as a full-stack developer.
Whether you need a simple noscript or a large-scale project, I can deliver clean, documented, reliable code for just a few bucks. I'm building up my Fiverr portfolio and would love to help bring your ideas to life!
Feel free to reach out - let’s work together!
https://redd.it/1m5ud8e
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
Beginner code question on nested tables
I am trying to construct and print a nested table In this simple example:
#!/usr/bin/env lua
company = {director="boss",
{address="home",
{zipcode ="12345"}
}
}
print(company.director) --> boss
print(company.director.address) --> nil - was expecting 'home'
print(company.director.address.zipcode)
print(company"director") --> boss
print(company"director""address") --> nil - was expecting 'home'
print(company"director""address""zipcode")
It prints nil where I was expecting it to print 'home'.
What am I doing wrong?
https://redd.it/1m61yq3
@r_lua
I am trying to construct and print a nested table In this simple example:
#!/usr/bin/env lua
company = {director="boss",
{address="home",
{zipcode ="12345"}
}
}
print(company.director) --> boss
print(company.director.address) --> nil - was expecting 'home'
print(company.director.address.zipcode)
print(company"director") --> boss
print(company"director""address") --> nil - was expecting 'home'
print(company"director""address""zipcode")
It prints nil where I was expecting it to print 'home'.
What am I doing wrong?
https://redd.it/1m61yq3
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
Sol2 and Modules
What are your thoughts on using cpp and sol2 library to create Modules to use in lua noscripts?
Do you prefer a Module written in c/cpp with the pure lua api, or Is It ok to use sol2?
https://redd.it/1m677cb
@r_lua
What are your thoughts on using cpp and sol2 library to create Modules to use in lua noscripts?
Do you prefer a Module written in c/cpp with the pure lua api, or Is It ok to use sol2?
https://redd.it/1m677cb
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
Luans - a Lua Learning App inspired by Koans (beta)
Hey everyone,
We’ve created a small web app to help people get started with Lua: [**play.tenum.app**](https://play.tenum.app)
It’s inspired by Kotlin Koans — we’re calling it **Luans**.
Each exercise is structured as a failing unit test, and your goal is to make it pass. The idea is to learn Lua through hands-on practice, one small step at a time.
Right now, the app is super basic and only contains a few lessons. We're testing right now and would love to hear from this community:
* Is this format helpful for learning Lua?
* What kinds of exercises or topics would you want to see?
* Are there other Lua learning platforms/tools you’ve used or recommend?
We’re considering investing more time into it, so your feedback would mean a lot.
Thanks in advance
https://redd.it/1m6bykq
@r_lua
Hey everyone,
We’ve created a small web app to help people get started with Lua: [**play.tenum.app**](https://play.tenum.app)
It’s inspired by Kotlin Koans — we’re calling it **Luans**.
Each exercise is structured as a failing unit test, and your goal is to make it pass. The idea is to learn Lua through hands-on practice, one small step at a time.
Right now, the app is super basic and only contains a few lessons. We're testing right now and would love to hear from this community:
* Is this format helpful for learning Lua?
* What kinds of exercises or topics would you want to see?
* Are there other Lua learning platforms/tools you’ve used or recommend?
We’re considering investing more time into it, so your feedback would mean a lot.
Thanks in advance
https://redd.it/1m6bykq
@r_lua
My awesome PWA app
Best PWA app in the world!
Working on a Wireshark parser, why does TreeItem:add_le() not reverse strings too?
My first instinct would be to post to r/wireshark, but last time I had a similar question I was directed here. Apologies if that’s incorrect.
[Link to line in docs](https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Tree.html)
Trying to fetch a little endian string, but it’s reversed because apparently the little endian add function only works on numbers? This feels really wrong, I can’t imagine why it works like this. Let me know if a more elegant way to display this is known.
https://redd.it/1m6m516
@r_lua
My first instinct would be to post to r/wireshark, but last time I had a similar question I was directed here. Apologies if that’s incorrect.
[Link to line in docs](https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Tree.html)
Trying to fetch a little endian string, but it’s reversed because apparently the little endian add function only works on numbers? This feels really wrong, I can’t imagine why it works like this. Let me know if a more elegant way to display this is known.
https://redd.it/1m6m516
@r_lua
I Make Steam Capsule Art That Pops! DM Me If You Want It For Your Lua Game
https://redd.it/1m6z3z9
@r_lua
https://redd.it/1m6z3z9
@r_lua
Reddit
From the lua community on Reddit: I Make Steam Capsule Art That Pops! DM Me If You Want It For Your Lua Game
Explore this post and more from the lua community
PLS HELP ME
I have no experience coding but i just wanted to start coding with lua, do yall know what platform is the best to learn coding with lua tysm! BTW ITS FOR GAME DEVELOPMENT
https://redd.it/1m797ho
@r_lua
I have no experience coding but i just wanted to start coding with lua, do yall know what platform is the best to learn coding with lua tysm! BTW ITS FOR GAME DEVELOPMENT
https://redd.it/1m797ho
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
How do I get better in Algorithms/Problem solving in Lua?
I started solving ''LeetCodes'' ( just LeetCode style coding puzzles, generated by ChatGPT since there is no Lua support for LeetCode) about just over a week ago. While I thought I finally got mastered for loops and tables, I now realize that my problem solving skills just suck and I would just keep failing most of the easy difficulty questions... For context, the average easy question would be something like finding the longest substring with no recurring/repeating letters or finding if a string is an Anagram, some other stuff with numbers like finding two pairs in a table that equal to target using just one for loop and other stuff that like that all of which require Hash tables which I really suck at... I have no idea what I can do to improve, other than just keep asking ChatGPT to explain stuff in more detail which it either can't do properly or I'm just too stupid for this... Sadly, unlike other languages, apart from having no LeetCode support, there are also minimal Lua tutorials when it comes to this kind of stuff, 0 on YouTube as far as I'm concerned so I just don't know how to progress with it. Maybe I am just stupid lol, I found a 7th grader the other day cranking c++ LeetCodes like the fucking legend he is, meanwhile I'm out here struggling to solve a LeetCode in what is possibly the most high-end programming language known to man... Any tips on getting better with algorithms and stuff like that in general and mainly mastering it inside Lua?(any good places to find tutorials maybe??) Also how do I get more used to hash tables and known how to use them properly? (additionally, I would ask whatever ''time complexity'' is but maybe that's a lesson for another a time) Basically how do I adapt to all of those situations I'm just so lost...
Any help will be appreciated! Seriously... D:
https://redd.it/1m7pyfb
@r_lua
I started solving ''LeetCodes'' ( just LeetCode style coding puzzles, generated by ChatGPT since there is no Lua support for LeetCode) about just over a week ago. While I thought I finally got mastered for loops and tables, I now realize that my problem solving skills just suck and I would just keep failing most of the easy difficulty questions... For context, the average easy question would be something like finding the longest substring with no recurring/repeating letters or finding if a string is an Anagram, some other stuff with numbers like finding two pairs in a table that equal to target using just one for loop and other stuff that like that all of which require Hash tables which I really suck at... I have no idea what I can do to improve, other than just keep asking ChatGPT to explain stuff in more detail which it either can't do properly or I'm just too stupid for this... Sadly, unlike other languages, apart from having no LeetCode support, there are also minimal Lua tutorials when it comes to this kind of stuff, 0 on YouTube as far as I'm concerned so I just don't know how to progress with it. Maybe I am just stupid lol, I found a 7th grader the other day cranking c++ LeetCodes like the fucking legend he is, meanwhile I'm out here struggling to solve a LeetCode in what is possibly the most high-end programming language known to man... Any tips on getting better with algorithms and stuff like that in general and mainly mastering it inside Lua?(any good places to find tutorials maybe??) Also how do I get more used to hash tables and known how to use them properly? (additionally, I would ask whatever ''time complexity'' is but maybe that's a lesson for another a time) Basically how do I adapt to all of those situations I'm just so lost...
Any help will be appreciated! Seriously... D:
https://redd.it/1m7pyfb
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
why isint eof a vaild statement?
i am very very new to lua (about 3 days into learning) and an error in the console is saying that it is excpecting <eof> but recived end and when i try to fix the error i figure out that eof isint a vaild statement (not sure if that is the right term) having the valid statements being colored to match the statement
eof being not colored
https://redd.it/1m85wkd
@r_lua
i am very very new to lua (about 3 days into learning) and an error in the console is saying that it is excpecting <eof> but recived end and when i try to fix the error i figure out that eof isint a vaild statement (not sure if that is the right term) having the valid statements being colored to match the statement
eof being not colored
https://redd.it/1m85wkd
@r_lua
Does a Logitech G915 X allow LUA noscripting?
I'm trying to get a new keyboard to replace my seven year old G910, only two problems...there seems to be no Mkey state for this keyboard, meaning nine macro keys is all you can get, and worse yet my old noscripts don't work at all. This is a major issue because, for some noscripts, it's just too long, and so having to write a noscript is just easier (especially when it comes to adding a comment on what said code is supposed to accomplish), or if you have to move the mouse to a specific place on screen for various actions. Point is noscripting is a real time saver, and I'd like to get it to work, which brings me to my main question: does the Logitech G915 X even allow noscripting in the first place?
I've tried modifying my code from
to
and even
and none of them will cause the noscript to run, which in this case, is just to press and release a random key on the keyboard, and nothing else...it's a test noscript after all. Am I doing something wrong, or is the newest keyboard just two giant summersaults backwards when it comes to the Mkey states and noscripting in general?
https://redd.it/1m8i46r
@r_lua
I'm trying to get a new keyboard to replace my seven year old G910, only two problems...there seems to be no Mkey state for this keyboard, meaning nine macro keys is all you can get, and worse yet my old noscripts don't work at all. This is a major issue because, for some noscripts, it's just too long, and so having to write a noscript is just easier (especially when it comes to adding a comment on what said code is supposed to accomplish), or if you have to move the mouse to a specific place on screen for various actions. Point is noscripting is a real time saver, and I'd like to get it to work, which brings me to my main question: does the Logitech G915 X even allow noscripting in the first place?
I've tried modifying my code from
if (event == "G_PRESSED" and mkey == 1 and arg == 1) thento
if (event == "G_PRESSED" and arg == 1) thenand even
if (event == "G_PRESSED" and arg == G1) thenand none of them will cause the noscript to run, which in this case, is just to press and release a random key on the keyboard, and nothing else...it's a test noscript after all. Am I doing something wrong, or is the newest keyboard just two giant summersaults backwards when it comes to the Mkey states and noscripting in general?
https://redd.it/1m8i46r
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community
luau Development (roblox)
Hey! I am very new to luau and i am wondering where i can find some steady information on luau and roblox API. Should i start just learning luau then move onto the roblox api or should i learn the API and luau together?
Thanks.
https://redd.it/1m8pwre
@r_lua
Hey! I am very new to luau and i am wondering where i can find some steady information on luau and roblox API. Should i start just learning luau then move onto the roblox api or should i learn the API and luau together?
Thanks.
https://redd.it/1m8pwre
@r_lua
Reddit
From the lua community on Reddit
Explore this post and more from the lua community