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
Need help understanding how pcall validates its arguments

I came across this exercise in the PiL book. It interested me cause I wanted to test my understanding of pcall/xpcall. The exercise basically was asking how/why the following statement behaves as it does ...

local f = nil
local boolValue, retValue = pcall(pcall, f)
-- boolValue is: true
-- retValue is: false

We can break this down a bit and ask ourselves what happens in the following case. However, this just leads to more questions.

local f = nil
local boolValue, retValue = pcall(f)
-- boolValue is: false
-- retValue is: attempt to call a nil value

Does the inner pcall even validate f before calling it? Does the outter pcall even consider "attempt to call a nil value" a critical runtime error? It's hard for me to give an ordered list of what unfolds but here's my best guess ...

1. The outter pcall handles the invocation of the inner pcall
2. The inner pcall fails to call f cause it's nil
3. So the inner pcall returns false with the error message "attempt to call a nil value"
4. The outter pcall does not consider this a runtime error thus returns true for boolValue even though the inner pcall shows false for it's boolValue
5. As to why reValue, from the outter pcall, is false I have no idea. I would think it would be the error message from the inner pcall but I guess not.

I'm a little shaky on this so would appreciate a deeper insight and response in clearing up my confusion. As you can see i'm a bit lost.

https://redd.it/1fd46gt
@r_lua
Luxtra - static blog generator

hey, I've been working on Luxtra, a static blog generator based on Markdown.

I just did this because I wanted my blog to be written in Lua.

still in early development, feel free to create a PR ;)

https://github.com/ropoko/Luxtra

https://redd.it/1fdn17m
@r_lua
From Lua to C++?

I'm not a programmer by any means, but interested in learning lua so I can program my game(s) to run more efficiently. I know visual noscripting is great but I've heard it can't get rid of the bugs etc, that I'd have to use some sort of coding/programming language to solve it. Everyone says lua is far easier to learn than c/c# & c++, but that I would need both (lua & c++) to make video games. So my question is: if I code my game(s) using lua, is there a translator like Google translate or something to translate the code from lua to c++? Just wondering so I won't mess anything up along the way. Thanks!

https://redd.it/1fdr4b6
@r_lua
Table initialization order?

I'm trying to do something like the following. I can't find examples of this. My linter is telling me it isn't valid, and, unsurprisingly, it doesn't actually work (I'm using Lua 5.3). I'm assuming it has to do with how Lua actually executes this, because the table and all its values don't exist until the closing brace.

SomeTable =
{
ValueMax = 100,
Value = ValueMax,
}

Is there a way this could work? The game I'm working on has a fair chunk of noscripts that are on the larger side and have a ton of associated data. It would be nice if I could do a little less typing.

https://redd.it/1fdwsyz
@r_lua
Game engines

As we know Roblox studio uses lua as programming language, are Thier any other good game engines that use lua to build 3d games.

https://redd.it/1fdzrcz
@r_lua
How can I get the last modified timestamp of a file which is in a folder?

How can I get the last modified timestamp of a file which is in a folder? like '2022-09-16 10:28:29.922116'

https://redd.it/1fe67ow
@r_lua
I'm looking for a Lua programmer to team up with me! :)

Hey y'all, I'm Yan, I'm a 3D Designer / Artist and Illustrator. I'm looking for a programmer to team up for a Roblox game. I did a lot of 3D Modelling in the past two years and was thinking that I could do something out of it, just like a little game. The only thing that is stopping me is the programming part. I want to focus on making good 3D assets and content for the game so I can do my best. I just build a whole city and a game concept in blender for university that maybe could be a first idea of what we could do. I'm really open to hear about your ideas for a game as well! I hope to find someone who works well with Lua and wants to be part of a creative project.

I'm aware that programming is a lot of work so the game itself doesn't have to be that complex or big - it can be what we both wanna do, I'm open to your ideas. If there will ever be any earnings out of the game I will do a 50/50 so we both get something out of it, but I also know that this is something for the future, just if the game pops out of the hundreds to thousands games that are already in Roblox.

You can find my 3D stuff here:

https://www.instagram.com/\_yanoto/

I hope someone is interested and wants to team up for a cool project!

https://redd.it/1feizww
@r_lua
Where to start

Where would be a good place to start in terms of maybe a basic noscript that can be ran in maybe gmod or Roblox. We used to code cheats years ago but I lost most understanding of it and would like to start writing noscripts again. Thanks!

https://redd.it/1fgoq5d
@r_lua
NOT learnimg lua bro

istg holding me at gun point, hang me by the balls but i am NOT learning lua.

https://redd.it/1fhie1v
@r_lua
embedding binary strings in code


was wondering how common is it?
do you actually write code like that:
local data = "\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x0a"

https://redd.it/1fi9jus
@r_lua
Can I annotate `__call` metamethod?

I am using \`classic\` for OOP, \`LuaLS\` for type annotation.

This is my code for a \`Block\` class

\`\`\`lua

local Physical = require('piss.physical')


---@class Block: Physical
---@field super Physical
---@field body love.Body
---@field fixture love.Fixture
---@field texture love.Texture
---@field __call fun(self: Block, world: love.World, x: number, y: number): Block
local Block = Physical:extend()


---@param world love.World
---@param x number
---@param y number
function Block:new(world, x, y)
Block.super.new(self, world, x, y, 'static', 'sprites/block.png')
end


return Block
```

When I try to create `Block` instance in `main.lua`, it doesn't show any type hint at all.

[I have to use \`\_\_call\` to see hints](https://preview.redd.it/0van574bkcpd1.png?width=1548&format=png&auto=webp&s=4233358fc6da2cca49fb2e32f9d9256bc4afae24)

Can I get hints on just calling constructor? If I can, how?

https://redd.it/1fivnsk
@r_lua
Looking for Resources to Start Learning FiveM Scripting

Hello everyone,

I want to learn how to noscript in FiveM and I have enough time to dedicate to it. I have some basic knowledge of Lua, but I'm not sure where to start or which resources to use.

Could you suggest resources as if I'm starting from scratch, with no Lua knowledge? What are the best guides or tutorials for learning basic Lua and FiveM noscripting? Also, any advice on which frameworks to work with would be greatly appreciated.

Thanks in advance! Any help would be really valuable.

https://redd.it/1fk8e1p
@r_lua
Using Pixi.js from fengari lua

I wanted to recreate this pixi.js [getting started](https://pixijs.com/8.x/guides/basics/getting-started) example using Lua, with [Fengari](http://fengari.io/).

I learned a lot about using js libraries in Fengari from [this](https://www.ucl.ac.uk/~rmhajc0/luafengari.pdf) article. One of the wrinkles is dealing with promises.

For example, in the Getting Started there are things like:

await app.init({ width:640, height: 360})

I found it awkward to keep nesting 'then' functions to wait for the promises. So I did some fiddling and created an 'await' function in lua which allows any js promise to be...awaited. Here it is, in case anyone cares:

<html><head>
<noscript>PIXI Getting Started (in Lua with fengari)</noscript>
<meta name="viewport" content="width=device-width, user-scalable=no">
<meta http-equiv="Content-Security-Policy" content="worker-src blob:">
<noscript src="pixi.js" type="text/javanoscript"></noscript>
<noscript src="fengari-web.js" type="text/javanoscript"></noscript>

<noscript type="application/lua">
local js=require('js')
local window=js.global
local document=window.document

function await(self,f,...)
-- await a js function which returns a promise
p=f(self,...)
-- The then() function defined below will be executed when the promise completes
p['then'](p,function (...)
resume(...) -- resume the execution of the await function, passing the result
end)
-- The await function execution continues immediately, asynchronously
_,result=coroutine.yield() -- yield. in this case effectively do nothing until resumed
-- the await function continues.
return result
end

function _init()
app=js.new(window.PIXI.Application)
-- in javanoscript, this would be: await app.init({ width:640, height: 360})
await(app,app.init,{width=640, height=360})
document.body:appendChild(app.canvas)
-- the await function will return the result of the promise execution (a Texture, in this case)
-- in javanoscript, this would be: await PIXI.Assets.load('sample.png')
window.console:log(await(window.PIXI.Assets,window.PIXI.Assets.load,'sample.png'))
-- use window.console:log rather than lua print, so the object is usefully presented in the console
end

function main()
_init()
local sprite = window.PIXI.Sprite:from('sample.png')
app.stage:addChild(sprite)
local elapsed = 0.0
app.ticker:add(function(self,ticker)
elapsed = elapsed + ticker.deltaTime
sprite.x = 100.0 + math.cos(elapsed/50.0) * 100.0
end)
end

resume=coroutine.wrap(main)

window:addEventListener("load", resume, false)
</noscript>
</html>

EDIT: fixed formatting

https://redd.it/1fkl9ip
@r_lua
Question about spawning new objects

Okay I didn't think something like this should be that hard to achieve but I can't for the life of me figure it out.

So I am trying to spawn food objects from 3 different types of food that are separated into their own files and do it randomly based on a random (unique to the food) timer that is initialized and stored in the specific food file itself that gets updated and managed by that food.

If that makes sense.

Hopefully just a conceptual rundown on what I have to do should suffice. Otherwise I can burden you with my code if you so wish.


P.S. I am also so new to lua so it's obvious I am missing something

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