Lua - Reddit – Telegram
Lua - Reddit
30 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
I Want to Learn

My son loves computers.

I've made a couple of super basic Scratch projects with him showing me the ropes and received more praise than I probably deserved.

That is the extent of my knowledge...but not the extent of my ambition. I really want to learn how to code. Like proper coding.

The kid and I love Roblox so I feel like choosing a first language connected to a shared passion might be best. So Lua....

I tried watching YouTube to get my feet wet only to find that nothing made sense.

Please help me. I need an introduction that starts at level zero. Where do I look/go/watch?

https://redd.it/1l3spff
@r_lua
A Cross-Platform “Batteries-Included” Lua Networking Toolkit

This is my first time posting here—please forgive any mistakes or inappropriate formatting.

silly is a cross-platform “super wrapper” (Windows/Linux/macOS) that bundles TCP/UDP, HTTP, WebSocket, RPC, timers, and more into one easy-to-use framework.

Built-in network primitives (sockets, HTTP client/server, WebSocket, RPC)
Event loop & timers, all exposed as idiomatic Lua functions
Daemonization, logging, process management out of the box
Self-contained deployment (no C modules needed, aside from optional libreadline)

Check out the examples/ folder (socket, HTTP, RPC, WebSocket, timer) to see how fast you can go from zero to a fully event-driven service. Everything is MIT-licensed—fork it, tweak it, or just learn from it.

▶️ Repo & docs: https://github.com/findstr/silly

Feel free to share feedback or ask questions!

https://redd.it/1l3ts1v
@r_lua
Catch output from function called via load(string)

Hey there,

I'm coming back to lua after some time away... I'm trying to call a constructor based on a string from some Tiled output. I've had success doing this using the `load` function - the constructor is being called - but I can't seem to get a reference to the object created. Here's some example code:

function AddElement(_type, _x, _y)
if (_type == null) then return end
local s = _type .. "(" .. tostring(_x) .. "," .. tostring(_y) .. ")"
local makeElement = load(s)
local e = makeElement()
table.insert(elements, 1, e)
print(#elements)
end

I am seeing output from print statements inside the elements' constructors, but the `elements` table is not increasing in size, and `e` seems to be nil. Any ideas?

https://redd.it/1l432f2
@r_lua
I'm new to programming trying to build a timetable generator for my school

when placing the subjects monday will be filled up completely but the other days will have a lot of free slots


function TimeTable_Placement()
local days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}
local daily_limit = 11
local doubleCountperday = {}

for class_name, subject_groups in pairs(Grouped_subjects) do
TimeTable[class_name\] = {}
for _, day in ipairs(days) do
TimeTable[class_name\][day\] = {}
doubleCountperday[day\] = 0
for p = 1, daily_limit do
TimeTable[class_name\][day\][p\] = nil
end

local reserved = Reserved_Periods[class_name\][day\] or {}
for period, reserved_name in pairs(reserved) do
TimeTable[class_name\][day\][period\] = reserved_name
end

end

for _, subject_group in ipairs(subject_groups) do
local placed = false
local needs_double = false
local subjects_done
local weekly_periods

for _, day in ipairs(days) do
for p = 1, daily_limit do

local try_double = false

local required_periods = 1
for _, subject in ipairs(subject_group) do

if DoublePeriodSubjects[subject\] then -- looks at the subjects to check if the user set this subject to need double
needs_double = true
end


if needs_double and doubleCountperday[day\] < 3 and ((Classes[class_name\][subject\].subject_periods_per_week - Classes[class_name\][subject\].Subjects_done_in_week ) >= 2 ) then -- checks if the number of double periods scheduled in the day are less than two then checks if the subjects remaining in the week are 2 or more so that it can set double periods
required_periods = 2
try_double = true
else
required_periods = 1
try_double = false
end

end

for _, subject in ipairs(subject_group) do
if subject == "math" then
print(Classes[class_name\][subject\].subject_periods_per_week)
end
end




if try_double then
if TimeTable[class_name\][day\][p + 1\] == nil and TimeTable[class_name\][day\][p\] == nil then
if p + required_periods - 1 <= daily_limit then
local conflict = false

for _, subject in ipairs(subject_group) do
if Classes[class_name\][subject\].Subjects_done_in_week >= Classes[class_name\][subject\].subject_periods_per_week then print("break") placed = false end
end
for offset = 0, required_periods - 1 do
for _, subject in ipairs(subject_group) do
local teacher = Get_teacher((Classes[class_name\][subject\]))
if teacher_schedule[teacher\] and teacher_schedule[teacher\][day .. (p + offset)\] then
conflict = true
break
end
end
if conflict then break end
end

if not conflict then
for offset = 0, required_periods - 1 do
if type(TimeTable[class_name\][day\][p\]) \~= "string" and type(TimeTable[class_name\][day\][p + 1\]) \~= "string" then
for _, subject in ipairs(subject_group) do
local teacher = Get_teacher((Classes[class_name\][subject\]))
TimeTable[class_name\][day\][p + offset\] = TimeTable[class_name\][day\][p + offset\] or {}
doubleCountperday[day\] = doubleCountperday[day\] + 1
table.insert(TimeTable[class_name\][day\][p + offset\], {
subject = subject,
teacher = teacher
})

if teacher then
teacher_schedule[teacher\] = teacher_schedule[teacher\] or {}
teacher_schedule[teacher\][day .. (p + offset)\] = true
end


for _, subject in ipairs(subject_group) do

Classes[class_name\][subject\].Subjects_done_in_week = Classes[class_name\][subject\].Subjects_done_in_week + 2

end
placed = true
break

end
end
end

end
if placed then break end
end
end
end
end

if not placed then
for p = 1, daily_limit do
local conflict = false
if TimeTable[class_name\][day\][p\] == nil then
for _, subject in ipairs(subject_group) do
local teacher = Get_teacher(Classes[class_name\][subject\])
for _, subject in ipairs(subject_group) do
print("Placing
single")
if Classes[class_name\][subject\].Subjects_done_in_week >= Classes[class_name\][subject\].subject_periods_per_week then break end
teacher = Get_teacher(Classes[class_name\][subject\])
if teacher_schedule[teacher\] and teacher_schedule[teacher\][day .. p\] then
conflict = true
break
end
end
end

if not conflict then
for _, subject in ipairs(subject_group) do
local teacher = Get_teacher(Classes[class_name\][subject\])
if not (teacher_schedule[teacher\] and teacher_schedule[teacher\][day .. p\]) then
TimeTable[class_name\][day\][p\] = {{
subject = subject,
teacher = teacher}}
teacher_schedule[teacher\][day .. p\] = true
Classes[class_name\][subject\].Subjects_done_in_week = Classes[class_name\][subject\].Subjects_done_in_week + 1
placed = true
break

end
end
end
end
if placed then break end
end
end
end
end
end
end

https://redd.it/1l45bie
@r_lua
Data entry for lua

This is probably a basic question but I just can't find a good answer. I'm working on a card game in defold and need to add a bunch of cards. I have some in lua files but adding them all manually is a pain. Is there a tool I can use to write the entries in an actual table and have them come out as lua? Is it just, do it as CSV and find a converter?

https://redd.it/1l46h3q
@r_lua
Interest in a Lua based modular game engine written in Rust?

I have an idea and a project I have planned out that is based heavily on modularity and the Lua programming language

For more detail I expect every UI element of the engine to be a "module" (can be written by any user and swapped or replaced and such) for example lets say we have a simple explorer that comes with the engine but is quickly seen to be too simple or lacking features you could either write your own file explorer and replaces its position in UI or download one off the internet and use that instead obviously this is just an example and I plan that other tools separate from the base ones can be created as well

This idea comes from when I was checking out Roblox Studio and saw the potential in the user made extensions where every user can create these UI elements and upload them to be used elsewhere within the engine I think a engine based on this potential would be extremely useful to those with a very unique workflow

https://redd.it/1l49ew7
@r_lua
Where can i learn lua for free?

Lua is my first programming language. I've been learning it for six months, ever since I found a YouTube channel that teaches it for free. After watching their videos, I learned the basics of the language, but now I want to improve my knowledge and skills.

Where can I find more free Lua content? And what tips would you give me, knowing that I already understand the basic functions?

https://redd.it/1l63gim
@r_lua
can someone create a game engine 3d and 2d like unity interface not exactly the same but in lua please i beg u

please.

https://redd.it/1l63kw6
@r_lua
How can I add a simple kill switch which can prevent my obfuscated noscripts posted on the internet from executing after i "disable" them in ant way?



https://redd.it/1l6h6n1
@r_lua
Creating an object by reference

I'm sure there's a better way to phrase the noscript, but that's what i came up with. Here's my issue: I have multiple tables and other objects that have a property that needs to be changed based on some condition. But I'm unable to get any of the tables to get the updated value.

Sample code illustrating this:

TestVariable = 123

TestObject =
{
['VarToChange'] = TestVariable,
['SomethingStatic'] = 789
}

print (TestObject.VarToChange)

TestVariable = 456

print (TestObject.VarToChange)


The output of the above is:

123
123


But I am expecting to get:

123
456


How can I achieve this behaviour? And please don't suggest updating all objects manually every time the variable changes. That rather defeats the entire purpose of a variable.

https://redd.it/1l7d8cj
@r_lua
Lua: message dispatching API design for the engine

I am making a Lua API for the engine and wonder how messaging (noscripts can exchange messages by the engine design) API should be exposed.

In C#, it would look like this:

...
class SomeEvent {
public int value;
}
...
Subscribe<SomeEvent>(msg => {
var s = msg.value;
});
...
Dispatcher.Send(new SomeEvent {value = "42"});
...

How should this look in Lua?

The most canonical version seems to be:

...
SomeEvent = {}
SomeEvent.index = SomeEvent
...
subscribe(SomeEvent, function(msg)
local s = msg.value
end)
...
dispatcher:send(setmetatable({value = "42"}, SomeEvent))
...

And its runtime implementation on the binding side is quite convenient.

However, I have two concerns:

1. Assigning the metatable to each message seems boilerplate-heavy. Even if it's moved to the "constructor," it doesn't help much, as many messages will still be created only in a few places.

Moreover, it's unlikely that messages will have any methods, so using metatables doesn't seem very appropriate.

1. To describe engine types and methods, I used Lua Annotations (https://luals.github.io/wiki/annotations/), which is extremely convenient for simulating OOP, allowing the IDE to correctly suggest methods and types, as well as enabling almost complete static analysis of the game code if rules are followed. However, the constructs in the "canonical" style above don't fit into Lua Annotations without boilerplate.

Here's how it looks:

---@generic T
---@param classid `T`
---
@param callback fun(msg: T)
function Subscribe(class
id, callback)
end

---@param m any
function Send(m)
end

---@class SomeEvent
---@field value string
SomeEvent = {}
SomeEvent.index = SomeEvent

Subscribe('SomeEvent', function (msg)
local s = msg.value
end)

--- Here "value" is outside the IDE analysis
Send(setmetatable({ value = "42"}, SomeEvent))

--- But this works fine, although it's more boilerplate
local a = setmetatable({}, SomeEvent)
a.value = "42"
Send(a)

--- The constructor makes usage cleaner when sending, but sending the same type of message will only happen in a few places. This makes the constructor unnecessary boilerplate.

---@param value string
---@return SomeEvent
function SomeEvent:new(value)
local a = setmetatable({}, SomeEvent)
a.value = value
return a
end

Send(SomeEvent:new("42"))

In general, I see the message system design without crossing into the type system. As boilerplate-free as possible, but support for IDE message dispatching is lost.

SomeEventId = ...
...
subscribe(SomeEventId, function(m)
local s = m.value
end)
...
dispatcher:send(SomeEventId, { value = "42"})
...

Or even this (easier to integrate with the current engine integration code than the previous example):

SomeEventId = ...
...
subscribe({type = SomeEventId }, function(m)
local s = m.value
end)
...
dispatcher:send({type = SomeEventId, value = "42"})
...

Do we even need to pursue type support in the IDE? Or is it enough to just provide suggestions for the engine API itself, and forget about IDE assistance in user code, since Lua programmers generally don't care about such things?

What do you recommend?

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