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
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
pod: plain old data de/serialization similar to protobuf

I just finished the initial implementation of pod: my plain-old-data library and serialization protocol that I intend to use for my minimalist CRUD database implemented in (nearly) pure lua.

Basically you can define types using my metaty and "implement" them by calling pod (on the type itself) -- then you can convert the type to/from extremely compact bytes. This is very similar to other serialization formats like msgpack but supports Lua types (mainly table types) more natively -- since I think Lua does it right for this kind of thing.

See the module documentation for more info: http://lua.civboot.org/#Package\_pod

https://redd.it/1igt08l
@r_lua
Why am I struggling to learn Lua?

I'm trying to become a game dev and want to start on Roblox, but I for some reason can't comprehend the programming. I'm interested in programming yet I'm struggling to understand it. I'm feeling stuck.

https://redd.it/1igze2t
@r_lua
Installing Packages with Lua Rocks

I'm trying to install packages with Lua Rocks, but for some reason when I use require in code it doesn't find my install. I'm in a Windows environment. When I installed Lua Rocks itself, it started off really flakey for some reason, giving an error when I called it saying that BIN_PATH was not correctly set/called. I installed using MinGW.

I somehow got around that, and tried to install Socket, but got errors relating to GetFileSizeEx not being correctly defined, so I had to extract the package manually, add lines to the code to define the Windows version (because according to some stack exchange thread that fixes it), and then it installed, but to an obscure file path. When I call require("socket") it tells me it cannot find socket, and the listed directories do not include C:/Program Files (x86)/LuaRocks/luasocket-3.1.0-1/lua where socket.lua is actually located.

Am I just being dense? What am I doing wrong that is making this so convoluted and hard? I spend 3 hours on this yesterday :(.

https://redd.it/1ihcw78
@r_lua
Lua Debugger Project

[Edit: I'm dumb, here's the link to the project https://github.com/NewbySlime/Lua-Debugger-GUI\]

Hey, I wanted to share something. This is a GUI debugger tool for Lua 5.3 using Godot 4.3, I've been working on this debugger for a while. Since I need people to test the project out, I wonder if I can get some traction going by posting it here.

NOTE: for now, only works for Windows.

https://preview.redd.it/kzdu3zi195he1.png?width=1177&format=png&auto=webp&s=86658cf4e9141feee0316f6a709b587790ea1737

Also bear with me when it comes to Github stuff, this is "kind of" my first time creating a kinda big project.

Note: my time here is night, any issues will be processed the next morning (my morning). Thanks!

Oh and, I'll be thankful to anyone contributing to the project <3

https://redd.it/1ihl7on
@r_lua
Error on VSC when trying to install an addon for a lua extension.

Heyo guys, fresh to lua and got this error when trying to install Garry's Mod Lua API Definitions for the lua extension. Does anyone know how to fix this?

#

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