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
cool little 3d terminal ascii renderer i made for fun

```

local width = 50
local height = 50
local cameraZ = 100
local FPS = 1/60
local origin = {x = 25, y = 25}

local grid = {}

local properties = {
    size = 13,
    position = {x = 0, y = 0, z = 0},
    rotation = {x = 0, y = 0, z = 0},
}

local vertices = {
    {x = -1, y = -1, z = -1},  -- Vertex 1: Bottom-left-front
    {x = 1, y = -1, z = -1},   -- Vertex 2: Bottom-right-front
    {x = 1, y = 1, z = -1},    -- Vertex 3: Top-right-front
    {x = -1, y = 1, z = -1},   -- Vertex 4: Top-left-front
    {x = -1, y = -1, z = 1},   -- Vertex 5: Bottom-left-back
    {x = 1, y = -1, z = 1},    -- Vertex 6: Bottom-right-back
    {x = 1, y = 1, z = 1},     -- Vertex 7: Top-right-back
    {x = -1, y = 1, z = 1}     -- Vertex 8: Top-left-back
}

local edges = {
    {1, 2},  -- Bottom-left-front to Bottom-right-front
    {2, 3},  -- Bottom-right-front to Top-right-front
    {3, 4},  -- Top-right-front to Top-left-front
    {4, 1},  -- Top-left-front to Bottom-left-front
    {5, 6},  -- Bottom-left-back to Bottom-right-back
    {6, 7},  -- Bottom-right-back to Top-right-back
    {7, 8},  -- Top-right-back to Top-left-back
    {8, 5},  -- Top-left-back to Bottom-left-back
    {1, 5},  -- Bottom-left-front to Bottom-left-back
    {2, 6},  -- Bottom-right-front to Bottom-right-back
    {3, 7},  -- Top-right-front to Top-right-back
    {4, 8}   -- Top-left-front to Top-left-back
}

local function sleep(t)
    local n = os.clock()
    while os.clock() - n < t do end
end

local function plot(x, y)
    if x >= 1 and x <= width and y >= 1 and y <= height then
        gridxy = "##"
    end
end

-- Bresenham's line algorithm
local function DrawLine(x0, y0, x1, y1)
    local dx = math.abs(x1 - x0)
    local dy = math.abs(y1 - y0)
    local sx = x0 < x1 and 1 or -1
    local sy = y0 < y1 and 1 or -1
    local err = dx - dy
   
    while true do
        plot(x0, y0)
        if x0 == x1 and y0 == y1 then break end
        local e2 = 2 err
        if e2 > -dy then
            err = err - dy
            x0 = x0 + sx
        end
        if e2 < dx then
            err = err + dx
            y0 = y0 + sy
        end
    end
end

local function project(x3d, y3d, z3d)    
    local depth = cameraZ - z3d
    if depth <= 0 then return nil, nil end  -- Clip behind camera
   
    local projX = (x3d - origin.x)
cameraZ / depth + origin.x
    local projY = (y3d - origin.y) cameraZ / depth + origin.y
    local screenX = math.floor(projX + 0.5)
    local screenY = height - math.floor(projY + 0.5) + 1

    if screenX < 1 or screenX > width or screenY < 1 or screenY > height then
        return nil, nil
    end
   
    return screenX, screenY
end

local function SetupGrid()
    for x = 1, width do
        grid[x] = {}
        for y = 1, height do
            grid[x][y] = "  "
        end
    end
end

local function TransformVertices()
    local transformed = {}
    local cosX, sinX = math.cos(properties.rotation.x), math.sin(properties.rotation.x)
    local cosY, sinY = math.cos(properties.rotation.y), math.sin(properties.rotation.y)
    local cosZ, sinZ = math.cos(properties.rotation.z), math.sin(properties.rotation.z)

    for _, v in ipairs(vertices) do
        -- Scale
        local x = v.x
properties.size / 2
        local y = v.y properties.size / 2
        local z = v.z
properties.size / 2

        -- Rotate around X axis
        local y1 = y cosX - z sinX
        local z1 = y sinX + z cosX
        y, z = y1, z1

        -- Rotate around Y axis
        local x1 = x cosY + z sinY
        local z2 =
cool little 3d terminal ascii renderer i made for fun

\`\`\`

local width = 50
local height = 50
local cameraZ = 100
local FPS = 1/60
local origin = {x = 25, y = 25}

local grid = {}

local properties = {
    size = 13,
    position = {x = 0, y = 0, z = 0},
    rotation = {x = 0, y = 0, z = 0},
}

local vertices = {
    {x = -1, y = -1, z = -1},  -- Vertex 1: Bottom-left-front
    {x = 1, y = -1, z = -1},   -- Vertex 2: Bottom-right-front
    {x = 1, y = 1, z = -1},    -- Vertex 3: Top-right-front
    {x = -1, y = 1, z = -1},   -- Vertex 4: Top-left-front
    {x = -1, y = -1, z = 1},   -- Vertex 5: Bottom-left-back
    {x = 1, y = -1, z = 1},    -- Vertex 6: Bottom-right-back
    {x = 1, y = 1, z = 1},     -- Vertex 7: Top-right-back
    {x = -1, y = 1, z = 1}     -- Vertex 8: Top-left-back
}

local edges = {
    {1, 2},  -- Bottom-left-front to Bottom-right-front
    {2, 3},  -- Bottom-right-front to Top-right-front
    {3, 4},  -- Top-right-front to Top-left-front
    {4, 1},  -- Top-left-front to Bottom-left-front
    {5, 6},  -- Bottom-left-back to Bottom-right-back
    {6, 7},  -- Bottom-right-back to Top-right-back
    {7, 8},  -- Top-right-back to Top-left-back
    {8, 5},  -- Top-left-back to Bottom-left-back
    {1, 5},  -- Bottom-left-front to Bottom-left-back
    {2, 6},  -- Bottom-right-front to Bottom-right-back
    {3, 7},  -- Top-right-front to Top-right-back
    {4, 8}   -- Top-left-front to Top-left-back
}

local function sleep(t)
    local n = os.clock()
    while os.clock() - n < t do end
end

local function plot(x, y)
    if x >= 1 and x <= width and y >= 1 and y <= height then
        grid[x][y] = "##"
    end
end

-- Bresenham's line algorithm
local function DrawLine(x0, y0, x1, y1)
    local dx = math.abs(x1 - x0)
    local dy = math.abs(y1 - y0)
    local sx = x0 < x1 and 1 or -1
    local sy = y0 < y1 and 1 or -1
    local err = dx - dy
   
    while true do
        plot(x0, y0)
        if x0 == x1 and y0 == y1 then break end
        local e2 = 2 * err
        if e2 > -dy then
            err = err - dy
            x0 = x0 + sx
        end
        if e2 < dx then
            err = err + dx
            y0 = y0 + sy
        end
    end
end

local function project(x3d, y3d, z3d)    
    local depth = cameraZ - z3d
    if depth <= 0 then return nil, nil end  -- Clip behind camera
   
    local projX = (x3d - origin.x) * cameraZ / depth + origin.x
    local projY = (y3d - origin.y) * cameraZ / depth + origin.y
    local screenX = math.floor(projX + 0.5)
    local screenY = height - math.floor(projY + 0.5) + 1

    if screenX < 1 or screenX > width or screenY < 1 or screenY > height then
        return nil, nil
    end
   
    return screenX, screenY
end

local function SetupGrid()
    for x = 1, width do
        grid[x] = {}
        for y = 1, height do
            grid[x][y] = "  "
        end
    end
end

local function TransformVertices()
    local transformed = {}
    local cosX, sinX = math.cos(properties.rotation.x), math.sin(properties.rotation.x)
    local cosY, sinY = math.cos(properties.rotation.y), math.sin(properties.rotation.y)
    local cosZ, sinZ = math.cos(properties.rotation.z), math.sin(properties.rotation.z)

    for _, v in ipairs(vertices) do
        -- Scale
        local x = v.x * properties.size / 2
        local y = v.y * properties.size / 2
        local z = v.z * properties.size / 2

        -- Rotate around X axis
        local y1 = y * cosX - z * sinX
        local z1 = y * sinX + z * cosX
        y, z = y1, z1

        -- Rotate around Y axis
        local x1 = x * cosY + z * sinY
        local z2 =
-x * sinY + z * cosY
        x, z = x1, z2

        -- Rotate around Z axis
        local x2 = x * cosZ - y * sinZ
        local y2 = x * sinZ + y * cosZ
        x, y = x2, y2

        -- Translate
        x = x + properties.position.x + origin.x
        y = y + properties.position.y + origin.y
        z = z + properties.position.z

        table.insert(transformed, {x = x, y = y, z = z, close = v.close})
    end

    return transformed
end

local function DrawLines()
    local transformed = TransformVertices()
    for _, edge in ipairs(edges) do
        local v1 = transformed[edge[1]]
        local v2 = transformed[edge[2]]
        local x0, y0 = project(v1.x, v1.y, v1.z)
        local x1, y1 = project(v2.x, v2.y, v2.z)
        if x0 and y0 and x1 and y1 then
            DrawLine(x0, y0, x1, y1)
        end
    end
end

local function DrawGrid()
    for y = 1, height do
        for x = 1, width do
            io.write(grid[x][y])
        end
        io.write("\n")
    end
end

-- Main loop
while true do
    os.execute("cls") -- Clear output on Windows (if you're on mac you shouldn't be here, it is the inferior os)

    properties.rotation.x = properties.rotation.x + 0.05
    properties.rotation.y = properties.rotation.y + 0.03
    properties.position.x = math.sin(os.clock()) * 3 -- Oscillate position

    SetupGrid()
    DrawLines()
    DrawGrid()

    sleep(FPS)
end



\`\`\`


that renders a cube, but i have some other shapes too


pyramid:



\`\`\`

local properties = {

size = 7,

position = {x = 0, y = 0, z = 0},

rotation = {x = 0, y = 0, z = 0},

}



local vertices = {

{x = -2, y = -2, z = 0}, -- Vertex 1: Bottom-left

{x = 2, y = -2, z = 0}, -- Vertex 2: Bottom-right

{x = 2, y = 2, z = 0}, -- Vertex 3: Top-right

{x = -2, y = 2, z = 0}, -- Vertex 4: Top-left

{x = 0, y = 0, z = 6} -- Vertex 5: Apex

}



local edges = {

{1, 2}, -- Bottom-left to Bottom-right

{2, 3}, -- Bottom-right to Top-right

{3, 4}, -- Top-right to Top-left

{4, 1}, -- Top-left to Bottom-left

{1, 5}, -- Bottom-left to Apex

{2, 5}, -- Bottom-right to Apex

{3, 5}, -- Top-right to Apex

{4, 5} -- Top-left to Apex

}
\`\`\`


and a dodecahedron


\`\`\`

local properties = {

size = 20,

position = {x = 0, y = 0, z = 0},

rotation = {x = 0, y = 0, z = 0},

}



local vertices = {

{x = 1, y = 1, z = 1}, -- Vertex 1

{x = 1, y = 1, z = -1}, -- Vertex 2

{x = 1, y = -1, z = 1}, -- Vertex 3

{x = 1, y = -1, z = -1}, -- Vertex 4

{x = -1, y = 1, z = 1}, -- Vertex 5

{x = -1, y = 1, z = -1}, -- Vertex 6

{x = -1, y = -1, z = 1}, -- Vertex 7

{x = -1, y = -1, z = -1}, -- Vertex 8

{x = 0, y = 1/1.618, z = 1.618}, -- Vertex 9

{x = 0, y = 1/1.618, z = -1.618},-- Vertex 10

{x = 0, y = -1/1.618, z = 1.618},-- Vertex 11

{x = 0, y = -1/1.618, z = -1.618},-- Vertex 12

{x = 1/1.618, y = 1.618, z = 0}, -- Vertex 13

{x = 1/1.618, y = -1.618, z = 0},-- Vertex 14

{x = -1/1.618, y = 1.618, z = 0},-- Vertex 15

{x = -1/1.618, y = -1.618, z = 0},-- Vertex 16

{x = 1.618, y = 0, z = 1/1.618}, -- Vertex 17

{x = 1.618, y = 0, z = -1/1.618},-- Vertex 18

{x = -1.618, y = 0, z = 1/1.618},-- Vertex 19

{x = -1.618, y = 0, z = -1/1.618},-- Vertex 20

}



local edges = {

{1, 2}, {2, 4}, {4, 3}, {3, 1}, -- Top face

{5, 6}, {6, 8}, {8, 7}, {7, 5}, -- Bottom face

{1, 9}, {9, 5}, -- Connect top to middle

{2, 10}, {10, 6},

{3, 11}, {11, 7},

{4, 12}, {12, 8},

{9, 13}, {13, 15}, {15, 10}, {10, 14}, {14, 9}, -- Top-middle pentagon

{11, 16}, {16, 14}, {14, 12}, {12, 17}, {17, 11},-- Bottom-middle pentagon

{13, 17}, {17, 18}, {18, 15},

{16, 19}, {19, 20}, {20, 14},

{19, 5}, {20, 6}

}
\`\`\`

https://redd.it/1ijnx6n
@r_lua
Lua modding

Does anyone know or have experience modding Sims 4 in lua? I have been learning lua for a bit and wonder if its possible to mod sims 4 with this programming language.

https://redd.it/1ijxjl2
@r_lua
I've only been learning for a short time, but I love physics so I tried this

Is it good for a beginner?



--define the root function
function root(x)
return math.sqrt(x)
end


--- light constant
c = 310^8

--get the values
print("how many percent of the speed of light")
v = (tonumber(
io.read())/100)c
print("what is the mass of the object(kg)")
M = tonumber(io.read())


--calculate lorentz value
LorentzFactor = (1/root(1-(v^2)/(c^2)))


--simplify the variable
F= Lorentz
Factor


--see if the value is less than the speed of light
if v >= c+1 then
print("               $ERROR.0".. math.random(1,10).. "$;enter a valid value (<= c)")
-- here checks if the value is equal
elseif v == c then
print(" $ERROR.0"..math.random(11,15)..";value approaches infinity")
--aki checks if the value is negative
elseif v <= -1 then
print("ERROR.016;identified negative value")

else
--energy calculation
E = (F - 1)Mc^2
--print the energy
print("the energy of the object is "..E)
end





https://redd.it/1ik0pz6
@r_lua
Thoughts on roblox as a free way to learn?

What are your thought son roblox studio and the platform itself, from a developer's perspective?


I'm leaning towards roblox but ive also considered love2d or some fantasy console.

I like lua and i want to learn the basic of it. ive messed with roblox but im not sure if i should stick with it. ive also never finished anything in studio.

https://redd.it/1ike280
@r_lua
Why did you guys decide to learn Lua?

Basically noscript.
I’m just curious to learn why other people decided to learn Lua. For me it’s for my work (I work as a robot programmer and I need to learn how to write some noscripts). I’m just starting out and I think it’s kinda fun.
Do you have any tips and or insights for a newb?

https://redd.it/1ilpbvz
@r_lua
fivem noscript support

ayone here with fivem noscripting that can help me out please?

https://redd.it/1ilssyh
@r_lua
How do you learn programming in general when you have mathematical struggles?

Hello, I have a learning disability and it affects my ability to comprehend math.

https://redd.it/1imcqo1
@r_lua
Learning Lua

Hello all!

I am very interested in Lua language because the game Final Fantasy XI has some add-ons that use it. Is there an online course I could take?? I've heard about Love2d

https://redd.it/1in7awd
@r_lua
It's lua a good choice to Desktop Apps?

My first language was lua, like 10 years ago, but then i start use JavaScript, and i forgot how to use lua btw, i want to know if i can use lua to make Desktop apps, not games, i want to build apps such as a POS or a Dashboard.

https://redd.it/1ini0e3
@r_lua
My two cents about my favorite language

Decided to write this up as a love letter to the best language - Lua

https://topcheto.eu/posts/0002

https://redd.it/1io7r01
@r_lua
Rationale for the numeric "for" loop syntax in Lua?

I've always been mystified by the obfuscated syntax of numeric "for" loops in Lua.

for i=10,1,-1 do

They seem so out of place compared to all of the other syntactical conventions of Lua which have more English-like semantics. I have to wonder was this a design decision by some completely separate group than the original creators of Lua? Or how did it come about?

https://redd.it/1io7ke6
@r_lua
Sub chat

Am looking for some lua coders here ,I have a problem with a roblox code, it's subbost to comment commands I selected and it's not working ,I tryed with Claude and gpt and still not working ,the code suppost to work in the noscript emulater (if you know what I mean) and not on roblox studio itself, can anyone help ;( ?

https://redd.it/1iodxer
@r_lua
Arrays start at 1 executive order
https://redd.it/1iosmv3
@r_lua
Why you should know Lua

Okay, maybe you don’t have to, but it’s definitely useful. People who know me well might be surprised here. They’re aware that my first programming language was PHP, but they don’t know that my second wasn’t JavaScript, it was Lua. I bet even my friends are confused now, and you probably are too. We’ll circle back to this.

## Why Should I Care?

That’s a fair question, especially if you already know other noscripting languages. Sure, you can live without Lua. But it’s lurking in places you’d never expect.

It’s popular, even if you rarely hear about it. Not as hyped as those Python devs who import every single letter, or Java folks in suits at banks, but Lua has an active community and plenty of great resources.

## It’s Simple. Too simple

Lua is a minimalist language with clean, straightforward syntax. It avoids unnecessary complexity, so you can learn it in a couple of hours. And here’s the classic example:

print('Hello, everyone! Except Java folks.')

if you.crazy() then
learnJava()
end


Lua isn’t usually used solo. It’s often embedded in game engines, other software, or paired with C/C++. I could list endless examples of where it’s used, but check this out if you’re curious.

## It’s fast

Lua is one of the fastest interpreted languages. It compiles to bytecode and runs on a virtual machine, giving it a significant speed advantage over other noscripting languages. That’s why it’s used in game engines and paired with C/C++ it’s easy to integrate and lightweight. Lua itself is tiny.

## Gaming

Ever played World of Warcraft? Or maybe you still do? (RIP your social life.) If so, you’ve heard of addons like browser extensions, but for games. Those addons are written in Lua. Back in school, I wrote and tweaked them myself. Magical times. As you might have guessed, yes, I did this before diving into client side web technologies. Oddly, I already knew PHP by then. If anyone’s interested, I could write a separate post about that.

Lua isn’t limited to WoW, though. It’s in game engines like:

- Godot (Lua support via GDScript wrappers)
- Love2D (a Lua-first engine)
- Defold (Lua as the main language)
- Gideros (mobile game engine)
- Cocos2d-x (supports Lua alongside C++ and JavaScript)
- Solar2D (formerly Corona SDK, Lua-centric for 2D games)
- OpenMW (Morrowind engine with Lua modding)

Honestly, I have no idea what most of these engines are, except Love2D it’s awesome. I’m no game engine expert, but clearly, Lua is everywhere.

According to Google, here are more games using Lua:

- Garry’s Mod—Lua drives most modding and gameplay.
- Roblox—uses a Lua dialect called Luau.
- Angry Birds—core logic is Lua.
- Terraria (tModLoader)—modding via Lua.
- Don't Starve—Lua for modding and game logic.
- Dark Souls series—Lua noscripts some AI.
- The Sims 4—powers game mechanics.
- Far Cry series (pre-Far Cry 3)—heavily noscripted with Lua.
- RimWorld (partially via Harmony modding).
- Payday 2—mods are Lua-based.
- Teardown—Lua for modding.

## DevOps & Systems programming

Lua isn’t just for games. It’s also in sysadmin tools:

- Nginx—uses Lua for dynamic configurations.
- Redis—supports Lua server-side noscripting.
- Tarantool—in-memory database.
- AwesomeWM—a Lua-based window manager.
- Syslog-ng—logging with Lua.
- HAProxy—Lua noscripting.
- OpenWrt—router firmware.

Lua doesn’t replace Python, Bash, or Go in DevOps, but it fits where speed, embeddability, and a tiny footprint matter.

## C’s Best Friend

Lua was built to be embedded. If you’re writing BlAzInG FaSt apps in C/C++, Lua adds flexibility without sacrificing BlAzInG FaSt.

## Should You Learn It?
Don’t forget, even though Lua is super small, it’s still a different language. For broadening your horizons it’s an interesting, if you have time, why not? There’s also Nelua, an interesting project, but it’s not nearly as widespread as Lua.

If you’re a default web monkey shuffling JSONs on the server and happy with that, Lua won’t make you better or worse. But exploring it might just