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
What is the return function?

I'm learning how to code, but I've reached a roadblock on what the return function is, as in I don't understand the explanation on what a return function does. I believe it's where you set a variable to the end of a sum? I'm pretty sure I'm wrong, so could you lovely people please help me?

https://redd.it/1jgntc2
@r_lua
Need help with URI-encoded link pattern

So I wanted to create a URI encode/decode library and I am stuck on my function "IsUri"

I can't figure out how to return true/false correctly, because:
A URI encoded link will have %HEX for special characters like " " (space)

A non URI-encoded link can also contain "%" which messes up my pattern.

I tried to do these 2 steps but failed:
find if there are any special characters without "%" in a string (return false early)
find if "%" has a valid syntax (return false/true)


I have also searched google and your subreddit for it. No answers....

https://redd.it/1jgsmxf
@r_lua
How do I create a website using HTML, CSS, & Lua?

I have tried EVERYTHING, Lapis, OpenResty, Luasocket, LuaJit, Luarocks, EVERYTHING, but NOTHING will work, I just want an index.lua file, with embedded HTML & CSS, & then I can use Lua as the back-end stuff such as button clicking.

I don't get it, why does it work for EVERYBODY but me.

I am on Windows-11, & I use Visual Studio Code.

https://redd.it/1jgtoye
@r_lua
Run other lua noscripts without knowing the name

Hi, so i’m making a lua noscript and it has a gui, and i want to make it so people can make addons for that gui, people keep saying it’s risky or they wouldn’t do it, they don’t even give a tip on how to do it, can anyone help?

https://redd.it/1jgvzl9
@r_lua
Should I learn Lua over Python as a non-dev ? (For macro / Scripting in Davinci Resolve)

Hello !

So I'm working with Davinci Resolve on a daily basis and I want to learn how to make my own noscript and macro. Resolve support both Lua and Python, but I don't know which language I should invest my time into. I don't really need to code outside this usecase, so I want to keep things simple and efficient.

I know that both are (relatively) easy to learn and from what I've heard the main advantage of Lua is its speed and simplicity while Python have a bigger community / ecosystem. I might be wrong or miss some elements tho, so I would like to know your opinion or advice !

https://redd.it/1jielaz
@r_lua
Fastest way to execute Lua?

Is there any method to execute Lua at it's highest speed?

Right now I'm using Zerobrane studio to execute Lua noscripts. It's very handy.

But it's probably not the fastest way to run it. I wonder if there are any faster methods for running Lua?

https://redd.it/1jif3h5
@r_lua
How do I learn Lua???

Hello I recently had the idea to learn how to code I heard that lua is better than python while still being as easy or easier than python the only problem is where do I start? What resources should I use to learn Lua? Can somebody help me. Thank You.

https://redd.it/1jibrgx
@r_lua
Error : <eof> expected near 'function'

Anyone know how to get this noscript good ?

ELEMENTS_TO_GATHER = { }
MAX_PODS = 90
MIN_MONSTERS = 1
MAX_MONSTERS = 8

-- Script OnlyBot pour Paysan 1-200 avec retour banque

local trajet = {}

function trajet:run()
    return {
        -- Récolte du blé (niveau 1-20)
        { map = "2,-25", action = "harvest" },
        { map = "3,-25", action = "harvest" },
        { map = "4,-25", action = "harvest" },

        -- Vérification de l'inventaire et retour banque
        { condition = "inventoryFull", map = "5,-18", action = "bankDeposit" },

        -- Récolte de l’orge (niveau 20-40)
        { map = "5,-25", action = "harvest" },
        { map = "6,-25", action = "harvest" },

        -- Retour banque si plein
        { condition = "inventoryFull", map = "5,-18", action = "bankDeposit" },

        -- Récolte de l’avoine (niveau 40-60)
        { map = "7,-25", action = "harvest" },
        { map = "8,-25", action = "harvest" },

        -- Retour banque
        { condition = "inventoryFull", map = "5,-18", action = "bankDeposit" },

        -- Récolte du houblon (niveau 60-100)
        { map = "9,-25", action = "harvest" },
        { map = "10,-25", action = "harvest" },

        -- Retour banque
        { condition = "inventoryFull", map = "5,-18", action = "bankDeposit" },

        -- Récolte du seigle (niveau 100-140)
        { map = "11,-25", action = "harvest" },
        { map = "12,-25", action = "harvest" },

        -- Retour banque
        { condition = "inventoryFull", map = "5,-18", action = "bankDeposit" },

        -- Récolte du malt et riz (niveau 140-200)
        { map = "13,-25", action = "harvest" },
        { map = "14,-25", action = "harvest" },

        -- Retour en banque final
        { condition = "inventoryFull", map = "5,-18", action = "bankDeposit" }
    }
end

return trajet


function bank()
    if map_CurrentMapId() ~= 162791424 and map_CurrentMapId() ~= 191105026 and map_CurrentMapId() ~= 191104002 and map_CurrentMapId() ~= 192415750 then
        return {{map = map_CurrentPos(), changeMap = "havenbag"},}
    end
    return {
        --HavreSac--
        {map = "162791424", changeMap = "usezaap:191105026"}, -- Astrub
        --Astrub--
        {map = "191105026", changeMap = "left"},
        {map = "191104002", changeMap = "cell:289"},
        {map = "192415750", custom = banquier},
    }
end

function banquier()
    global_Delay(1000)
    npc_Speak(-20000) -- [-20000] = id du npc banque Astrub
    global_Delay(1000)
    npc_Reply(64347) -- [64347] = id de la reponse
    global_Delay(1000)
    storage_DropAll() -- On vide TOUT les items
    global_Delay(1000)
    npc_Close() -- On ferme la banque
    global_Delay(1000)
    map_ChangeMap("cell:409") -- Sortir de la banque
    global_Delay(5000)
end

https://redd.it/1jj2hiy
@r_lua
lua feels high and low level at the same time

thinking about it for the past hour. no pointers to screw you up, yet nearly zero standard library as well. every time i need a very basic function (like checking if a table contains a value, iirc even C++ has that in the standard library!) i try and look it up, only to realise that i am on my own. it's nothing difficult most of the time, annoying at worst. yet... it feels like zen. the purity i've felt while writing stuff in 6502 assembly in an online emulator, except in lua... everything actually works. it all makes sense. every piece of code makes sense. there are frustrating points as well, like not having the continue keyword (what a shame!)... but it's a small price for such soulful feeling.


>!oh GODDAMMIT i have to write deepcopy again!<

https://redd.it/1jld1rx
@r_lua
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