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
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
A little Morse translator I made for fun

local morseCode = {
["1"] = "*----",
["2"] = "**---",
["3"] = "***--",
["4"] = "****-",
["5"] = "*****",
["6"] = "-****",
["7"] = "--***",
["8"] = "---**",
["9"] = "----*",
["0"] = "-----",
[" "] = "/",
["."] = "*-*-*-",
[","] = "--**--",
["!"] = "-*-*--",
["?"] = "**--**",
["`"] = "*----*",
["/"] = "-**-*",
[":"] = "---***",
[";"] = "-*-*-*",
["+"] = "*-*-*",
["-"] = "-****-",
["="] = "-***-",
["("] = "-*--*",
[")"] = "-*--*-",
a = "*-",
b = "-***",
c = "-*-*",
d = "-**",
e = "*",
f = "**-*",
g = "--*",
h = "****",
i = "**",
j = "*---",
k = "-*-",
l = "*-**",
m = "--",
n = "-*",
o = "---",
p = "*--*",
q = "--*-",
r = "*-*",
s = "***",
t = "-",
u = "**-",
v = "***-",
w = "*--",
x = "-**-",
y = "-*--",
z = "--**"
}

local function FindInTable(t, v)
for k, cv in pairs(t) do
if v == cv then
return k
end
end
end

local function Help()
print("\n")
print("Entering morse: Enter \"*\" for a dot, \"-\" for a dash. Between each letter put a space. Between each word put a \"/\".")
print("Entering text: Enter any vaild character(s) that can be converted to morse")
print("Enter \"quit\" to exit the program")
print("Enter \"help\" to see this again")
print("\n")
end

while true do
print("\n")
print("Translate morse to text: enter \"1\"")
print("Translate text to morse: enter \"2\"")
print("Exit program: enter \"quit\"")
print("For Help: enter \"help\"")
print("\n")

local input = string.lower(io.read("l"))

if input == "quit" then print("Goodbye!") break end
if input == "help" then Help() goto continue end

local mode = input == "1" and "m->t" or input == "2" and "t->m" or nil

if not mode then print("invalid input; try again"); goto continue end

print("enter your message in "..(mode == "m->t" and "morse" or "text"))
print("\n")

local input = io.read("l")
local message = ""

if mode == "m->t" then
local morseCharacter = ""

input = input.." "

for character in input:gmatch"." do
if character == "*" or character == "-" then
morseCharacter = morseCharacter..character
elseif character == "/" then
message = message.." "
elseif character == " " then
local letterOfMorse = FindInTable(morseCode, morseCharacter)
if morseCharacter and morseCharacter ~= "" then
if letterOfMorse then
message = message..letterOfMorse
morseCharacter = ""
else
print("Invalid morse found in message: \""..morseCharacter.."\". Please retry")
goto continue
end
end
else
print("Invalid character found in message: \""..character.."\". Please retry")
goto continue
end
end

elseif mode == "t->m" then

for character in input:gmatch"." do
if not morseCode[character] then
print("Invalid character found in message: \""..character.."\". Please retry")
goto continue
end
message = message..morseCode[character].." "
end
end

print("\n", message, "\n")

::continue::
end


https://redd.it/1ihrw4q
@r_lua
Where learn lua

Hello I just want to know where I start learning lua and simple project I can made

https://redd.it/1iia6pl
@r_lua
TAL - a pseudo-runtime for lua, written in lua

[https://git.topcheto.eu/topchetoeu/tal](https://git.topcheto.eu/topchetoeu/tal)

This is a small runtime made to build on top of the, quite honestly, insufficient and incomplete lua stdlibs, as well as the annoying non-relative module system.

It is written for my personal usage and so it's pretty shitty and undocumented, some pretty questionable decisions have been made to make this working. The code base follows no sane standards and will probably break a lot of existing lua code - TAL is definitely far from prod-ready.

Some of the major features it has are:

* symmetric coroutines (by utilizing a modified version of "coro")
* a better module system
* extensions of the standard libraries, most notably the new "array" class
* some useful modules
* an event loop (still useless because there are no async functions yet)

Feel free to check it out and give me your feedback - should I clean this up for usage by sane people?

Also, the name means "TopchetoEU's Atrocious Lua"

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