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

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

Powered by : @r_channels & @reddit2telegram
Download Telegram
Guidance on Improving Function Efficiency

Hi all, I'm working on a vehicle model in Lua/Aumlet, but have been running into performance issues. One function that gets called *a lot* is the function that returns an iterator to iterate over all the degrees of freedom (DoF) of the car (body x, y, z direction, etc.). The vehicle is modelled as a body, axles, and powertrain parts. The way I've done it feels pretty sloppy. Any pointers?

function car:iterateOverDoF()
local a = 0 -- Initialise body DoF counter to 0
local aMax = 3 -- Number of body DoF
local b = 0 -- Initialise axle DoF counter to 0
local bMax = self.body.numAxles*3 -- Number of axle DoF
local c = 0 -- Initialise powertrain DoF counter to 0
local cMax = #self.powertrain -- Number of powertrain DoF
local i=0 -- Overall counter
return function ()
i=i+1 -- Increment counter
if a<aMax then -- Check that we have not iterated over all body DoF
a=a+1 -- Increment body DoF counter
return i, self.body, self.body.dimensions[a] -- Return information about the DoF being inteorgated
elseif b<bMax then -- Repeat same process for axles and powertrain
b=b+1
return i, self.axles[math.ceil(b/3)], self.axles[math.ceil(b/3)].dimensions[(b-1)%3+1]
elseif c<cMax then
c=c+1
return i, self.powertrain[c], self.powertrain[c].dimensions[1]
else return nil end -- Return nil once all DoF have been iterated over
end
end

https://redd.it/1jmo0ti
@r_lua
Anyone help me?

Hello everyone! Im New in noscript lua, i using game guardian... i need help, what are the commands to copy the address of an item and place it over the saved values?

https://redd.it/1jmpmcy
@r_lua
4.x

I have a random question who here has actually used pre 5 lua and how was it I'm more just curious also wondering if there is anywhere you can get a copy of it once again curious I see it mentioned in documentation and of course it existed but seems like its been frozen for a minute.

https://redd.it/1jmro88
@r_lua
Question on creating a "Read Only" table ...

**Version: LuaJIT**

## Abstract

Lets consider we come across the following pattern for implementing a read only table. Lets also establish our environment and say we're using LuaJIT. There's a few questions that popped up in my head when I was playing around with this and I need some help confirming my understanding.

local function readOnly(t)
local proxy = {}
setmetatable(proxy, {
__index = t,
__newindex = function(_, k, v)
error("error read only", 2)
end
})
return proxy
end

## QUESTION 1 (Extending pattern with ipairs)

If I wanted to use `ipairs` to loop over the table and print the values of `t`, protected by proxy, would the following be a valid solution? Maybe it would be better to just implement `__tostring`?

local function readOnly(t)
local proxy = {}
function proxy:ipairs() return ipairs(t) end
setmetatable(proxy, {
__index = t,
__newindex = function(_, k, v)
error("error read only", 2)
end
})
return proxy
end
local days = readOnly({ "mon", "tue", "wed" })
for k, v in days:ipairs() do print(k, v) end

## QUESTION 2 (Is it read only?)

Nothing is stopping me from just accessing the metatable and getting access to `t` or just simply deleting the metatable. For example I could easily just do ...

getmetatable(days).__index[1] = "foo"

I have come across a metafield called `__metatable`. My understanding is that this would protect against this situation? Is this a situation that `__metatable` aims to be of use?

local function readOnly(t)
local proxy = {}
function proxy:ipairs() return ipairs(t) end
setmetatable(proxy, {
__index = t,
__newindex = function(_, k, v)
error("error read only", 2)
end,
__metatable = false
})
return proxy
end


https://redd.it/1jmt4r9
@r_lua
Regarding metatable definitions

Hey might be a stupid question but why does:

local v = {}
v.add = function(left, right)
return setmetatable({
left1 + right1,
left2 + right2,
left3 + right3
}, v)
end

local v1 = setmetatable({3, 1, 5}, v)
local v2 = setmetatable({-3, 2, 2}, v)
local v3 = v1 + v2
print(v31, v32, v33)
v3 = v3 + v3
print(v31, v32, v33)

work fine and returns value as expected:

0 3 7
0 6 14

but this does not:

local v = {
add = function(left, right)
return setmetatable({
left1 + right1,
left2 + right2,
left3 + right3
}, v)
end
}


local v1 = setmetatable({3, 1, 5}, v)
local v2 = setmetatable({-3, 2, 2}, v)
local v3 = v1 + v2
print(v31, v32, v33)
v3 = v3 + v3
print(v31, v32, v33)

Got error in output:

0 3 7
lua: hello.lua:16: attempt to perform arithmetic on a table value (local 'v3')
stack traceback:
hello.lua:16: in main chunk
C: in ?

I did ask both chatgpt and grok but couldn't understand either of their reasonings. Was trying to learn lua through: https://www.youtube.com/watch?v=CuWfgiwI73Q/

https://redd.it/1jn7blm
@r_lua
Sony Inzone interfering with Lua noscript

I’m not quite sure if this is the right place for this but I use lua noscripts on my Logitech mouse for video games, and today I bought some Sony Inzone earbuds. It seems to make the values for the recoil higher out of nowhere but it’s only while the usb-c dongle is plugged in. It doesn’t change the actual values in the noscript but it responds about 2-3x stronger. It seems unrelated to the control center app from Sony but is affected by the dongle. Does anyone have a fix for this or know why this is happening?

https://redd.it/1jnstfp
@r_lua
A good learning resource for lua and programming in general?

What are your recommendations?

https://redd.it/1jntn67
@r_lua
How to interpret bit.lshift 2 and bit.lshift 2ULL?

Here is my snippet code as follows:d

#!/usr/bin/env resty

local ffi = require("ffi")
local bit = require "bit"

local lshift = bit.lshift

local function printx(x)
print("0x"..bit.tohex(x))
end

local lshiftuint64
do
local ffi
uint = ffi.new("uint64t")

lshift
uint64 = function(v, offset)
ffiuint = v
return lshift(ffi
uint, offset)
end
end

print("--- ffiuint = v ---")
printx(lshift
uint64(2, 61))
printx(lshiftuint64(2ULL, 61))
printx(lshift
uint64(0x2ULL, 61))

local lshiftuint64new
do
lshiftuint64new = function(v, offset)
local ffiuint = ffi.new("uint64t", v)
return lshift(ffiuint, offset)
end
end

print("--- ffi
uint = ffi.new ---")
printx(lshiftuint64new(2, 61))
printx(lshiftuint64new(2ULL, 61))
printx(lshiftuint64new(0x2ULL, 61))


The output on my macOS is:

--- ffiuint = v ---
0x40000000
0x4000000000000000
0x4000000000000000
--- ffi
uint = ffi.new ---
0x4000000000000000
0x4000000000000000
0x4000000000000000

The output of printx(lshift_uint64(2, 61)) seems just 32-bit long?

https://redd.it/1jntw7g
@r_lua
ide's or libraries for young learners?

i have a pre-teen sibling who wants to learn coding, he has tried scratch so he has somewhat of an experience to coding, he has created some games with it and he did it well! he told me he wanted to try 'real' code, so this is where this reddit post steps in.

he loves to try new things, so he doesn't mind doing pure code!

https://redd.it/1jo0p5s
@r_lua
Using continuation functions as normal functions possible?

I have often the case where I want to loop and within that loop call a lua function or have to yield, but yieldable with continuation.
For that I have to provide a continuation function which only functions as trampoline to call the normal function again.

int foo(luaState*);
int foo
continue(luaState* L, int, luaKContext) {
foo(L);
}
int foo(luaState* L) {
while (true) {
/* do things */
lua
yield(L, 0, NULL, &foocontinue);
}
}
int main() {
// ...
lua
pushcfunction(L, &foo);
// ...
}

Because I have to persist the runtime, I'm using Eris, I now also have to add the continuation function to the persistency table.

I would love to remove that boilerplate by simply doing something like this:

int foo(luaState* L, int, luaKContext) {
while (true) {
/ do things /
luayield(L, 0, NULL, &foo);
}
}
int main() {
// ...
lua
pushcfunction(L, &foo);
// ...
}

Using reinterpret cast that seems to work just fine but idk if that is really stable and doesnt cause undefined behaviour.
So, is this allowed or not?

https://redd.it/1jo284c
@r_lua
Good resources on Lua that also teach programming fundamentals?

I'd like to believe I know Lua well enough, having used it a lot for game modding. However there's a lot of people who come to our community trying to make mods with zero knowledge of programming, and trying to help those people gets frustrating for everyone involved.

What resources are there that teach Lua while also explaining basic concepts (variables, conditionals, loops, etc.)? First few tutorials I could find seem to be made for people who already know programming and just need a crash course on specifics of the language...

https://redd.it/1jo4ihc
@r_lua
Termfx make fails with error lua.h: No such file or directory

What it says on the tincan. Attempting to build Termfx without Luarocks (because luarocks has caused nothing but pain and suffering for me) and I'm not able to make it successfully as it doesn't seem to be able to find the C headers.

I think this boils down to me not specifying where the lua.h file is, but I don't know how I can do that. Thanks for any help

https://redd.it/1jo7trj
@r_lua
Accidently deleted Ghub lua noscript command and now cant remember.

There was a lua file located on my computer on D: , a noscript was running it without copying everything to GHUB,it was someting like D: ..... Test.lua, i accidently deleted it and cant remember now would you guys help me?

https://redd.it/1jp2z0m
@r_lua
Obfuscated lua file

Hey. I have a lua file that is in my fivem server, I need to edit a few things in the file
Is there a way to deobfuscate the file through my server key?
I bought the file legally

https://redd.it/1jp8yq8
@r_lua
What special Lua tutorial would be useful?

Hello I make YouTube tutorials. I made a general “Learn Lua” tutorial but I would like to make specific tutorials. I was thinking of teach concepts like strings (and its functions) and metatables. Are there any tutorials that would be useful please let me know?

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