Lua - Reddit – Telegram
Lua - Reddit
31 subscribers
281 photos
31 videos
4.27K links
News and discussion for the Lua programming language.

Subreddit: https://www.reddit.com/r/lua

Powered by : @r_channels & @reddit2telegram
Download Telegram
Can anyone explain why this code doesnt work ? i wrote the code in the newest lua version.

local userpassword = {}
local generated
password = {}

function randpasswordgenerate()
    repeat
        table.insert(generatedpassword,table.concat(string.char(math.random(60,116))))
    until user
passwordmath.random(8, 16) ~= nil    

end

userpassword = generatedpassword

https://redd.it/1icywvh
@r_lua
I am Interested in Learning lua

i am interested in learning lua but don't know where to start. what should i learn first and in what order should i learn first in lua and for context i know java java noscript python and html. if that make's it easier for choosing how i should learn thank you for considering in advance

https://redd.it/1id3nex
@r_lua
Ban posts asking for help to learn Lua

There's been too many posts like "help learn lua" "best way to learn lua" "where do I learn"

God damn. 50% of the posts are asking the exact same. All you have to do is search this subreddit before posting a question.

You will find plenty of online resources for learning. Don't flood this subreddit with those questions. Ask questions that haven't been asked before, that way you add value to this subreddit.

Mods, do something about it.

https://redd.it/1idin6j
@r_lua
Book

Are there any books for lua? (Price 10 - 20$) I love readinng books and most of the time i remember alot from them so reading them could help me learn the language.

https://redd.it/1iduhhu
@r_lua
Mesh modification?

I want to make a little noscript that takes in an FBX mesh (or other 3d model type) and assuming it is a cube, each side would represent (what i call) 1 "unit." It would then generate and save all size variations of this mesh going from 1x1x1 units (the starting size) to 8x8x8. In the end, the folder that had the 1x1x1 mesh now has all 512 different size variations of it. If the input were a wedge/slope with the same bounding box, it would also output all size variations of it, so it isnt limited to cubes.

But what can I do to modify the mesh in the first place? I cant find any luarocks modules to use, (or they exist and im not looking closely) nor do I know how to do it manually.

https://redd.it/1idw71u
@r_lua
Why people laugh of lua so much?

Recently i see more and more people making fun out of lua and saying it is not proper language. I fell like as if lua was even more laughed of than Python.

https://redd.it/1idx0yq
@r_lua
Hello world final boss

local program = [[

\++++++++[>++++[>++>+++>+++>+<<<<-\]>+>+>->>+[<\]<-\]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.

\]\]



local tape = {}

local tapePointer = 1

local output = ""



for i = 1, 30000 do

tape[i\] = 0

end



local function getTapeValue()

return tape[tapePointer\] or 0

end



local function setTapeValue(value)

tape[tapePointer\] = value % 256

end



local function interpretBF(code)

local codePointer = 1

local codeLength = #code

local jumpTable = {}

local openBrackets = {}



for i = 1, codeLength do

local instruction = code:sub(i, i)

if instruction == "[" then

table.insert(openBrackets, i)

elseif instruction == "\]" then

local startPos = table.remove(openBrackets)

if startPos then

jumpTable[startPos\] = i

jumpTable[i\] = startPos

end

end

end



while codePointer <= codeLength do

local instruction = code:sub(codePointer, codePointer)



if instruction == ">" then

tapePointer = tapePointer + 1

elseif instruction == "<" then

tapePointer = math.max(tapePointer - 1, 1)

elseif instruction == "+" then

setTapeValue(getTapeValue() + 1)

elseif instruction == "-" then

setTapeValue(getTapeValue() - 1)

elseif instruction == "." then

output = output .. string.char(getTapeValue())

elseif instruction == "[" and getTapeValue() == 0 then

codePointer = jumpTable[codePointer\]

elseif instruction == "\]" and getTapeValue() \~= 0 then

codePointer = jumpTable[codePointer\]

end



codePointer = codePointer + 1

end

end



pcall(function() interpretBF(program) end)



print(output)



for i = 1, (200 - 55) do

print("")

end



https://redd.it/1iedrtj
@r_lua
How to launch 2 functions with one luadrawhookpost command.

Hello everyone,

I have been working for about an hour with chatgpt and it simple cannot do this.

I have a .lua file that contains two functions named main and main2

I have a config file for the linux app Conky that launches this .lua via the code:

`-- Lua Load`

`lua
load = '/home/logansfury/.conky2/horiz.lua',

luadrawhookpost = 'main',`


I tried editing to "lua\
draw_hook_post = 'main main2'," but this didnt work. I then tried:

-- Lua Load

lua_load = 'horiz2.lua wht_blue_cal.lua',

lua_draw_hook_post = 'main',

lua_draw_hook_post = 'main2',


but this only launched main2.

The chatbot suggested:

lua_draw_hook_post = {'main', 'main2'},


then it instructed to put this in .lua

function combined_hook()

main()

main2()

end

and this in the config file:

lua_draw_hook_post = 'combined_hook',

but this didnt work either. Then it had me edit the .lua bit to:

function combined_hook()

if main then main() end

if main2 then main2() end

end

and this also failed to work, displaying NOTHING, not even the 2nd .lua

I am out of ideas for how to question the bot to get a correct answer. Can anyone here please show me how to launch two .lua functions with a single separate config file?


Thank you for reading,

Logan





https://redd.it/1ieocw8
@r_lua
Good Resources to Learn Lua. (This is for Beginners)

I was reading this post: https://www.reddit.com/r/lua/comments/1idin6j/ban\_posts\_asking\_for\_help\_to\_learn\_lua/

It seems that there are a lot of people asking the same "How to learn Lua?" question. I feel that there should just be one link we can just copy and paste for people that ask that question that way it is like an FAQ.


List resources that helped you or any good one that you know and upvote the ones that you like.

https://redd.it/1ieu5gq
@r_lua
Lossless 60% compression in 1000 lines of C (with Lua bindings)

Hey everyone, I wrote a compression and binary diff algorithm that gets to qbout 40% lossless compression (zip is about 33% compression for comparison).

It's primary purpose is to implement patching for my own version control, the compression ability is gravy on top with minimal code.

It uses a variation of xdiff compression (I call rdiff) and then Huffman encoded. Check it out!

https://github.com/civboot/civlua/blob/main/lib/smol

https://lua.civboot.org#Package_smol

https://redd.it/1iex7bg
@r_lua
I've made MDN style Lua Documentation (LuaDocs)

Hi there!

Back in 2022, I was doing some game modding and found it very hard and slow to reference Lua for programming.


So I decided to create proper documentation following MDN sytle: The ones created from the manual itself, but in a modern format: https://www.luadocs.com/docs/introduction


I come from JavaScript, and I never had to learn Lua - I just knew it, but I always kept forgetting how to use stuff or what functions even existed.


The documentation isn't finished, but if anyone wants to help, or if there's interest I'll be up for finishing it off.


Got plenty of ideas, but want to see if anyones interested in this, if yes I'll continue it for the community and get it into a solid state but I want to see if it'll be of interest to anyone, as I no longer use Lua.

https://redd.it/1if0www
@r_lua
typosafe and performant enum in 60 lines of code

[http://lua.civboot.org/#metaty.enum](http://lua.civboot.org/#metaty.enum)

[https://github.com/civboot/civlua/blob/main/lib/metaty/metaty.lua#L385](https://github.com/civboot/civlua/blob/main/lib/metaty/metaty.lua#L385)


Hey everyone, I created typosafe enums as part of my metatype module. The main advantages are:

* allows using either strings or ids (integers) as your enum "value" -- as long as your outer API converts to the one you expect using `myEnumName = MyEnum.name(myEnumNameOrId)`
* checks that your "matcher" table-of-functions handles all possible variants, throwing an error at module instantiation time if not (you should only define these tables in your module (i.e. not inside a function call) since creating the table is a bit expensive)
* Can be used in the proto-like serialization format I am writing (still WIP)

Note: I haven't pushed it to luarocks yet, but will soonish.

https://redd.it/1ifax1k
@r_lua
How to display error messages nicely?

When using error(msg) the output seems too verbose if I simply want to provide the user with an error message:

local err_msg = string.format("%d %s: %s", status_code, response.error.type, response.error.message) error(err_msg)local err_msg = string.format("%d %s: %s", status_code, response.error.type, response.error.message)

error(err_msg)

However while using print(msg) followed by os.exit() achieves the desired effect it seems to hacky:

print(err_msg)

os.exit()

What is a common approach?

https://redd.it/1ifcm0t
@r_lua
Connecting to UNIX socket with luaposix

Hi all, I'm trying to figure out how to connect to a UNIX socket with luaposix. I've been looking through their API documentation and they have an example on how to connect to web sockets, but not this. It might be similar but I'm severely lacking on socket programming.

The reason I'm doing this is the Astal framework supports Lua, but it also has no native libraries for Sway as far as I know. So to get my workspaces and query other info about Sway, obviously I'd need to connect to the IPC.

local posix = require("posix.unistd")

local M = require("posix.sys.socket")



local sock_path = os.getenv("SWAYSOCK")



local r, err = M.getaddrinfo(sock_path, "unix", { family = M.AF_UNIX, socktype = M.SOCK_STREAM })

local sock, err = M.socket(M.AF_UNIX, M.SOCK_STREAM, 0)

if not sock then

print("Error creating socket: " .. err)

return

end



local result, err = M.connect(sock, r[1\])

if not result then

print("Error connecting to the socket: " .. err)

return

end



local command = '{"jsonrpc": "2.0", "id": 1, "method": "get_version"}'

local result, err = posix.write(sock, command)

if not result then

print("Error sending data to socket: " .. err)

return

end



local response = posix.read(sock, 1024)

if response then

print("Response from Sway IPC: " .. response)

else

print("Error reading from socket: " .. err)

end

posix.close(sock)


I don't have this in a code block because everytime I tried, reddit would mash it together onto one line.

https://redd.it/1ifq4lo
@r_lua
What do you think of my desktop background?
https://redd.it/1ifskct
@r_lua
how can i choose a specific line of a txt file and give it to the variable named word



https://redd.it/1ig23gx
@r_lua
Question about copying tables

This is not about how to do it, but rather if I am right about the cause of a bug.

I have a default table that I use, it is then copied over to new instances to allow them to handle the variables themselves.

local a = {b=1, c=2,d={e=1,f=10}}

When I copy them, I use the standard code,

function table.copy(t)
local t2 = {}
for k,v in pairs(t) do
t2k = v
end
for k,v in pairs(t.d)
t2.dk = v
end
return t2
end

However, when I made a change a t2.de, it would change it for all instances. When I fixed this, I basically reset d by doing d={} before it is copied on the guess that I am creating a reference to td when I was copying it?

Things are working, I just want to be certain I am not going to have a bug related to this down the road.

https://redd.it/1ig9pzb
@r_lua
Any good game engines that use lua? (besides roblox)

I like lua and its concept but i cant really find many tools or engines that use lua, the only one i could find was roblox or some overprices junk but i havent look that hard.

What are some notable game engines that use lua that have a interface to work with?


or are there better ways to try out lua?

https://redd.it/1ig8e6g
@r_lua
thread pausing in MAME's Lua?

I'm wanting to replicate something similar to packer's keyboard stuffing in MAME to automate some things (ok, to play the Infocom HHTTG game) ... the "keyboard stuffing" bit is working fine, I just can't work out how to get my thread that's doing this to pause in any other way than a busy loop ...

I've done some reading on Lua in other contexts which suggest I need to set up a coprocess and have one yield to the other which I've tried, however I've not managed to get that to Do The Right Thing. The other often-mentioned "solution" is to call out to the OS and run sleep() there, however I don't think that's offered in MAME (and I don't think it should be either)

Are there any worked examples for doing pausing execution of your LUA thread for an arbitrary (sub-second) time in MAME?

Thanks

https://redd.it/1igc3yx
@r_lua
Getting into Lua again with Pico-8, what are some good small projects to try?
https://redd.it/1igoe19
@r_lua
How to regex match the pipe char in Lua?

I need to substitute the '|' char with 'sometext'. Tried all the listed variants for now, still no result.

new_text = string.gsub(text, '%|', 'sometext')

new_text = string.gsub(text, '|', 'sometext')

new_text = string.gsub(text, '\\|', 'sometext')

None of this AI-suggested approaches work, so asking some help from the community.



https://redd.it/1igqaf4
@r_lua