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
Trying to make an 'or' entry, please help me out?

Hi guys,

For a personal mod in Assetto Corsa, I'm trying to let a LUA file - that switches on the app when it sees to right car - look for not one, but two cars: the 2025 car and the 2024 car.
Nothing I do seems to work:
" , "
" or "
making the 5 (or 4) into %s

These are the lines; can someone please point me in the right direction?



if string.startsWith(ac.getCarID(0), "formula_2_2025") then appActive = true

local function optimizeCarModel(car)
if ac.getCarID(car.index) ~= "formula22025” then
return
end

https://redd.it/1ivh7sg
@r_lua
Dluau: luau runtime focused on extensibility and modularity

Project link

A personal project i've been working on. Luau is a great variant of lua in my opinion, but its support for dynamically extending the environment has always been limited. This project tries to bridge that gap a little on top of some other extra quality-of-life features.

The project is subdivided into a minimal core, which exports all the Luau C API symbols as well as some extra API for registering userdata tags, namecall atoms and types in a dynamic library friendly way. And a standard library (which is kinda all over the place at the moment) which you can optionally use.

Statically resolves noscript, DLL and standard library dependencies at startup of the runtime only loading the dependencies you are actually using. Then sandboxes these resources.

Any tips, feedback, suggestions or critique would be greatly appreciated.

https://redd.it/1ivtmij
@r_lua
Lua Game Engine export multi platform application only bytecode

Hello

I'am looking for a recommendation of a (2D) Lua game engine which can be used to develop games.

I want a multi platform export of the application but with only bytecode in the bundle/package (I don't want to ship the Lua source code in the bundle/package).

Any recommendation?

Thank you

https://redd.it/1ivtw09
@r_lua
I've made a very small programming language in Lua in 2 days

So I've created IBScript because I was bored at a math lesson and this idea came to me.

The name was selected because It is badly written :/

here is the GitHub page: https://github.com/illersaver/IBScript

https://redd.it/1ivx93n
@r_lua
Require

I still haven’t been able to find how to do this in lua:

How do i require luas i don’t know the full name of?

For example Require all luas with Addon or Plugin at the end

I’ve seen a lua that requires all luas that have Addon at the end from a certain directory like SomethingAddon.lua and it works requires all luas from a directory

I would look at the code of it but it’s obfuscated with a custom obfuscator so i thought my only option was asking here how i do that

https://redd.it/1iwn5qw
@r_lua
Is this possible to deobfuscate?

\\149\\163\\221\\137\\1469\\148!\\198\\212,qz\\136


If not tell me an moonsec V3 Deobfuscator that works :) ty

https://redd.it/1ix57m5
@r_lua
Variations in the hash function used in Lua tables

Running Lua 5.4.7 here. I've found that the output of the following program can change from one run to another.

t = { "a"=1, "b"=1 }

for k, v in pairs(t) do
io.write(k)
end
io.write("\n")

Using the standard Lua interpreter, sometimes it outputs ab and sometimes ba.

Now, I understand that the order in which pairs iterates through the keys in a table is unspecified -- as with most hash tables. But I find it interesting that the order can actually vary between runs on the same machine with the same version of Lua.

Some details: if I start up a Lua interactive environment and run the above code with dofile, then the order is consistent until I quit the interactive environment. But if I start up a new interactive environment, then the order may be different.

So, I take it that, when the standard Lua interpreter is initialized, there is some pseudorandomness or time dependence in the choice of the hash function used for tables.

I thought I had a question, but I guess I really don't. Although if someone has more info on this issue -- e.g., exactly what is done, and why, and whether this happens in all versions of Lua -- I'd love to hear about it.

https://redd.it/1ix9hhl
@r_lua
better Lua fail value?

In the Lua docs it mentions fail which is currently just nil.

I don't personally like Lua's standard error handling of returning nil, errormsg -- the main reason being it leads to awkward code, i.e. local val1, val2 = thing(); if not val1 then return nil, val2 end

I'm thinking of designing a fail metatable, basically just a table with __tostring that does string.format(table.unpack(self)) and __call that does setmetatable so you can make it with fail{"bad %i", i}. The module would also export a isfail(v) function that just compares the getmetatable to the fail table as well as assert that handles a fail object (or nil,msg).

So the code would now be local val1, val2 = thing(); if isfail(val1) then return val1 end

Has anyone else worked in this space? What are your thoughts?



https://redd.it/1ixndr0
@r_lua
local varients optimization

I heard that using "local" keywords are very important in Lua. But I knew that "local" keyword in algorithm coding are not good at optimization. No difference, sometimes take long time to be executed.

Why "local" keyword is nesessary? Is it really efficient? I can't figure out why these things are happening.

(But if you don't use local keyword in function, This can make error)

https://redd.it/1ixrt06
@r_lua
zero dependency fail type and module in ~30 lines of code.

Thanks for the discussion, I've published the fail module to luarocks. You can read documentation at http://lua.civboot.org/#Package\_fail or view the \~30 lines of code or look below

local push, concat = table.insert, table.concat
local unpack = table.unpack
local sfmt = string.format
local setmt, getmt = setmetatable, getmetatable
local raweq = rawequal

--- The fail type and module. See README.cxt for usage.
local fail = setmetatable({name = 'fail'}, {
name = 'failtype',
call = function(F, f) return setmt(f, F) end,
})

--- The fail type. See module documentation.
fail.tostring = function(f) return sfmt(unpack(f)) end

--- return true if rawequal(getmt(v), fail).
---
--- Typical use is $if failed(v) then return v end to propogate
--- a failure to the caller.
fail.failed = function(v) return raweq(getmt(v), fail) end

--- throw error if $v is falsy or fail.
fail.assert = function(v, ...) --> v, ...
if not v then error((...) or 'assert failed') end
if raweq(getmt(v), fail) then error(tostring(v)) end
return v, ...
end

--- Convert lua's standard $ok, errmsg to fail when not ok.
---
--- This is useful to return the result of "standard" lua (ok, errmsg) APIs.
fail.check = function(ok, errmsg) return ok or fail{errmsg} end

return fail









https://redd.it/1iy2r3n
@r_lua
Lua have another null type than nil

Hello everyone,

I've come across some curious behavior:

function foo() end
function bar() return nil end

table.insert(t, a) --> ok, even if a is nil
table.insert(t, nil) --> ok
table.insert(t, foo()) --> error “wrong number of arguments to ‘insert’”
table.insert(t, (foo())) --> but this is ok
table.insert(t, bar()) --> and this too

I guess it's because if a, nil, and (foo()) are all values, foo() in this context is actually a kind of tuple: it can return 1, 2, x values... or none. And table.insert needs to know how many arguments foo() returns. This is confirmed by bar(), which poses no problem because it returns one value.

Is my interpretation correct? So in Lua, is there a difference between returning the value "nothing" and...returning nothing?

https://redd.it/1iy6xcp
@r_lua
Anyone know of why Lua is so satisfying?

For context, I'm someone who has been self-taught since 8th grade (currently a senior) and learned only python until last year, when I started to branch out. I've looked into languages like C, C++, C#, and while I did enjoy C and C#, (I even wrote a crude but functional dynamic array in C, which was fun) but no other language feels the same way Lua does. While I do come from python, I actually do prefer types, I make sure to type annotate in python, so I don't think (at least in my case) it's because it's dynamically typed. While I'm still learning the below surface level stuff in Lua, I'm just finding Lua to be extremely enjoyable, anyone have any ideas why I and other people I know find Lua naturally enjoyable?

https://redd.it/1iyndkl
@r_lua
Global and local variables with Fengari

I know that local variables allow the code to run faster than globals but does the same apply when the code is converted to javanoscript by the fengari virtual machine? The question earlier this week made me curious ;-0

https://redd.it/1iyplwe
@r_lua
Code has and error when run locally, but runs fine when submitted to Exercism.

I'm losing my mind, hopefully one of you can help me understand what is going on.

I'm using Exercism to learn the basics of Lua. In my code, I am trying to divide a number by 2, but floor it if it's not an integer value. With Exercism, it provides a test noscript, and you run and test the code with Busted. When I run this locally, I get an error:

./eliuds-eggs\_spec.lua:1: error loading module 'eliuds-eggs' from file './eliuds-eggs.lua':

./eliuds-eggs.lua:8: unexpected symbol near '/'

I submitted it to Exercism anyway so that I could look at other people's submissions and see if anyone else did what I was trying to do, and my code passed with no issues.

What could be causing my local environment to not recognize integer division?

Lua: 5.4.7

busted: 2.2.0

The function in question (the error message is pointing to the line that says "number = (number // 2)")

function EliudsEggs.egg_count(number)
local count = 0
while (number > 0) do
count = count + (number % 2)
number = (number // 2)
end
return count
end

https://redd.it/1iyxgxx
@r_lua
What data types can math.randomseed take?

I'm reading through some code that has uses a pseudorandom process to generate seeds for math.randomseed, but the seeds are generated as some float between 0 and 1.

Here's what the code is doing:

-- LCG algorithm, generates float between 0 and 1
pseudoseed1 = math.abs(tonumber(string.format("%.13f", (2.134453429141+pseudoseed*1.72431234)%1)))
-- hashed
seed is also a float between 0 and 1
pseudoseed = (pseudoseed1 + hashedseed)/2
math.randomseed(pseudoseed)
print(math.random())

Is there a way to make sense of this? Running this in lua 5.4 will generate an error (since math.randomseed only takes integers), and running it in lua 5.1 will result in pseudoseed being truncated to 0 when passed to math.random, giving the same exact result from math.random every time. The software definitely works at generating different numbers each time, so I feel like I'm missing something here.

https://redd.it/1iz63i1
@r_lua
LUA binding for Dart

Has someone a good tutorial, how to create a LUA binding for Dart via ffi? I want to create one for an old version, because we want to transfer old compiled lua code into a Flutter app.

Can someone help?

https://redd.it/1izjo4j
@r_lua
Generating UUIDs and getting current time with just a standard Lua library

Hello, I am trying to create an API Gateway with Krakend which would take an incoming HTTP request, transform it's body and then post it onto a RabbitMQ queue. To transform it, I need to write a Lua noscript, which among other things, would have to add the current time and generate a unique UUID. Problem is though, there is a certain, huge limitation for Lua on Krakend (info from it's documentation):

>Because Lua runs sandboxed in a virtual machine, there is certain stuff you cannot do. When you include files, you should limit your logic to creating functions and referencing them in different places.

>In addition to that, the usage of modules would require KrakenD to push them in advance to the virtual machine, so user-created modules are not possible. Package managers like LuaRocks are not supported either, because they inject modules globally in the system, not in the virtual machine.


As soon as I try to use methods such a math, or os.time(), which are needed for generating UUIDs or setting the current time, the noscript fails. I haven't written any Lua code before, so my question is, does anyone have any idea on how to solve this problem? Or maybe someone has an experience with Krakend and solved a similiar issue?

https://redd.it/1izkg6f
@r_lua
any apps i can use to code

I really want to get into programming I have a book to learn lua since i head its an easier ish one but I'm finding it hard to find an app to use to run and write lua if anyone has a good let me know thanks.

https://redd.it/1izsyb2
@r_lua
How to start noscripting Fivem

How to learn lua for Fivem. I don't have any knowledge about lua or any other languages. Is there any tutorials online, but from beginning to end. Like how to start Fivem server, what to do after and so on. Thanks

https://redd.it/1izubuk
@r_lua
Lua Linters/static analysis

Hi! I have recently inherited a medium sized Lua project in work (I have never worked with Lua before) and I am wanting to add linting and maybe some static analysis if that is possible (why this was never done before I do not know). When looking around I noticed that the likes of luacheck and luainspect do not seem to be actively maintained looking at the githubs.

What are people generally using for this? Are these tools feature complete, or is there something modern I am missing?

Thanks in advance!

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