Lua

331 readers
1 users here now

Lemmy community dedicated to the Lua programming language.

About Lua

Lua is a powerful and lightweight dynamically typed programming language, often embedded into applications to extend their API.

Resources

lua.org - official website
lua.org/pil - book
lua.org/manual - documentation
luarocks.org - package manager

Rules

You may read full set of rules here.
Too long; didn't read:

  1. Posts must relate to Lua
  2. Use descriptive titles
  3. Provide reproduction code
  4. Information about environment

founded 1 year ago
MODERATORS
1
2
 
 

I have installed various packages but none of them seem to work as expected at all, I mean they don't seem to work at all.

Does anyone know of a package that actually works?

3
4
5
 
 

This script renders a window with stats to the player as they play. Can someone help me increase the size of the window slightly and the font size? I have attempted a few parameters with no luck

local messages = {}
local glitter = {}
local glitterCount = 0

function addMessage(text, mood)
  for i = math.min(#messages + 1, 4), 2, -1 do
    messages[i] = messages[i - 1]
    messages[i].targetPos = i
  end
  messages[1] = { text = text, age = 0, targetPos = 1, currentPos = 1, mood = mood }
  if mood == 1 then
    for i = 1, 60 do
      local dir = vec2(math.random() - 0.5, math.random() - 0.5)
      glitterCount = glitterCount + 1
      glitter[glitterCount] = { 
        color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1), 
        pos = vec2(80, 140) + dir * vec2(40, 20),
        velocity = dir:normalize():scale(0.2 + math.random()),
        life = 0.5 + 0.5 * math.random()
      }
    end
  end
end

local function updateMessages(dt)
  comboColor = comboColor + dt * 10 * comboMeter
  if comboColor > 360 then comboColor = comboColor - 360 end
  for i = 1, #messages do
    local m = messages[i]
    m.age = m.age + dt
    m.currentPos = math.applyLag(m.currentPos, m.targetPos, 0.8, dt)
  end
  for i = glitterCount, 1, -1 do
    local g = glitter[i]
    g.pos:add(g.velocity)
    g.velocity.y = g.velocity.y + 0.02
    g.life = g.life - dt
    g.color.mult = math.saturate(g.life * 4)
    if g.life < 0 then
      if i < glitterCount then
        glitter[i] = glitter[glitterCount]
      end
      glitterCount = glitterCount - 1
    end
  end
  if comboMeter > 10 and math.random() > 0.98 then
    for i = 1, math.floor(comboMeter) do
      local dir = vec2(math.random() - 0.5, math.random() - 0.5)
      glitterCount = glitterCount + 1
      glitter[glitterCount] = { 
        color = rgbm.new(hsv(math.random() * 360, 1, 1):rgb(), 1), 
        pos = vec2(195, 75) + dir * vec2(40, 20),
        velocity = dir:normalize():scale(0.2 + math.random()),
        life = 0.5 + 0.5 * math.random()
      }
    end
  end
end

local speedWarning = 0
function script.drawUI()
  local uiState = ac.getUiState()
  updateMessages(uiState.dt)

  local speedRelative = math.saturate(math.floor(ac.getCarState(1).speedKmh) / requiredSpeed)
  speedWarning = math.applyLag(speedWarning, speedRelative < 1 and 1 or 0, 0.5, uiState.dt)

  local colorDark = rgbm(0.4, 0.4, 0.4, 1)
  local colorGrey = rgbm(0.7, 0.7, 0.7, 1)
  local colorAccent = rgbm.new(hsv(speedRelative * 120, 1, 1):rgb(), 1)
  local colorCombo = rgbm.new(hsv(comboColor, math.saturate(comboMeter / 10), 1):rgb(), math.saturate(comboMeter / 4))

  local function speedMeter(ref)
    ui.drawRectFilled(ref + vec2(0, -4), ref + vec2(180, 5), colorDark, 1)
    ui.drawLine(ref + vec2(0, -4), ref + vec2(0, 4), colorGrey, 1)
    ui.drawLine(ref + vec2(requiredSpeed, -4), ref + vec2(requiredSpeed, 4), colorGrey, 1)

    local speed = math.min(ac.getCarState(1).speedKmh, 180)
    if speed > 1 then
      ui.drawLine(ref + vec2(0, 0), ref + vec2(speed, 0), colorAccent, 4)
    end
  end

  ui.beginTransparentWindow('overtakeScore', vec2(uiState.windowSize.x * 0.5 - 600, 100), vec2(400, 400))
  ui.beginOutline()

  ui.pushStyleVar(ui.StyleVar.Alpha, 1 - speedWarning)
  ui.pushFont(ui.Font.Title)
  ui.text('Overtake Run')
  ui.popFont()
  ui.popStyleVar()
  ui.TextScaled(5)
  ui.TextSize(30)

  ui.pushFont(ui.Font.Huge)
  ui.text(totalScore .. ' pts')
  ui.sameLine(0, 40)
  ui.beginRotation()
  ui.textColored(math.ceil(comboMeter * 10) / 10 .. 'x', colorCombo)
  if comboMeter > 20 then
    ui.endRotation(math.sin(comboMeter / 180 * 3141.5) * 3 * math.lerpInvSat(comboMeter, 20, 30) + 90)
  end
  ui.popFont()
  ui.endOutline(rgbm(0, 0, 0, 0.3))
  
  ui.offsetCursorY(20)
  ui.pushFont(ui.Font.Title)
  local startPos = ui.getCursor()
  for i = 1, #messages do
    local m = messages[i]
    local f = math.saturate(4 - m.currentPos) * math.saturate(8 - m.age)
    ui.setCursor(startPos + vec2(20 + math.saturate(1 - m.age * 10) ^ 2 * 100, (m.currentPos - 1) * 30))
    ui.textColored(m.text, m.mood == 1 and rgbm(0, 1, 0, f) 
      or m.mood == -1 and rgbm(1, 0, 0, f) or rgbm(1, 1, 1, f))
  end
  for i = 1, glitterCount do
    local g = glitter[i]
    if g ~= nil then
      ui.drawLine(g.pos, g.pos + g.velocity * 4, g.color, 2)
    end
  end
  ui.popFont()
  ui.setCursor(startPos + vec2(0, 4 * 30))

  ui.pushStyleVar(ui.StyleVar.Alpha, speedWarning)
  ui.setCursorY(0)
  ui.pushFont(ui.Font.Main)
  ui.textColored('Keep speed above '..requiredSpeed..' km/h:', colorAccent)
  speedMeter(ui.getCursor() + vec2(-9, 4))
  ui.popFont()
  ui.popStyleVar()

  ui.endTransparentWindow()
end
6
 
 

Hey all,

I'm learning Lua and come from a Java background, and have been diving deep into Lua "classes".

Realizing that a Lua "class" is just some clever syntactic sugar, and lacks encapsulation, I've found that closures can act like a Java class and protect it's state. An added bonus is my muscle memory won't struggle with : vs . for function calls, a pitfall I've already wasted time debugging.

I'm new to all this though, is there anything I'm missing? Is a Lua "class" with self and : a better choice than using a closure like a class?

7
 
 

Hi everyone!

I just wanted to share a small template library and supporting program I wrote using Lua (specifically wrote against LuaJIT and Lua5.1).

I haven't found any existing template engine that really fit my needs. The main one I saw was Jinja2, but it required pulling in Python, which on a lot of environments was a bit more of an ask than I really wanted to deal with (especially in enterprise environments). Many others I've tried over the years seem not to throw errors when a variable is referenced, but not defined, or at least the ones I could get testing with in about 15 minutes.

Can't promise it's perfect, but it replaced my need to create a templated text document for my RPG character that I had written in M4, and I have had a lot of instances where I wanted an easy-to-use templating engine for configurations.

8
9
 
 

A place where I can pick up subroutines for my Lua programs?

Lua Users! Welcome to Lemmy!

10
 
 

Lazy is a library that extends the standard Lua library.
It exposes function modules such as math, string, table, etc.

It's aimed to fill the gaps of Lua's standard libraries by providing functions such as math.round to round numbers, string.trim to remove leading and trailing white spaces from a string and many many many more.

One important feature of Lazy is that the require path of modules is automatically resolved, which allows you to require the lazy library from virtually anywhere.
For example the lazy folder may be located in the root of your project or any sub-directory. More so, upon setting your LUA_PATH correctly, lazy can be required anywhere from your PC without having to import it into your project. Please refer to the GitLab page for more information .


Example usage:

local lazy = require "lazy"

lazy.math.round(1234.5678, 2) --> 1234.57
lazy.os.capture("pwd") --> /home/name/projects/lua/lazy
lazy.string.split("Lua is amazing!", " ") --> { "Lua", "is", "amazing!" }
lazy.table.append({ "A" }, { "B" }, { "C" }, { "D" }) --> { "A", "B", "C", "D" }

Contributions and requests are welcome :P

11
1
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 
 

All the rules from this Lemmy instance apply.
Additionally, please try to follow these community-specific rules:

Rules

1. Posts must relate to Lua

All posts must be related to the Lua programming language.
This also includes related and derivative projects, such as Luau or MoonScript.

2. Use descriptive titles

Your title should in short describe the contents. That applies to all kinds of posts, no matter whether you're showcasing a project, discussing a feature or asking for help.

Examples of bad titles:

  • please help!
    ^ Doesn't provide any information about the post
  • How to rotate SENT?
    ^ Should also information about API and ideally expand SENT which is a rather niche acronym
  • request.lua
    ^ Could provide a short description of the library

Examples of good titles:

  • [Help] Lua errors when using string.format with the %p flag
  • How to rotate a Scripted Entity in Garry's Mod when spawned?
  • request.lua - Lua >=5.1 library to simplify HTTP requests

Please note that there are no strict rules related to tagging posts (eg. including [Help] or [Roblox] in the title), but they are very welcome nonetheless.

3. Provide reproduction code

When asking for help, you should provide a reproduction code whenever applicable. This doesn't mean that you should copy and paste your whole project or script into the post, in fact the opposite. Try to isolate the issue you're facing on your own and only include the code needed to reproduce it.

A screenshot or image of the code is never valid. Additionally the code should be provided using the following Markdown syntax:

```lua
<your code goes here>
```

4. Information about environment

When asking for help, and ideally releasing a project, you should include as much information as possible about the target environment.
Most important thing is the Lua API in use, that means the video game, game engine, framework, library, etc.
Other information such as Lua version, environment variables, operating system, etc. may be useful as well.


Posts not following the rules may be deleted without prior notice.
Repeated offenders may be issued a warning and banned.

If you do not agree with the rules or statements made here, please discuss your issue in the comment section, I'm sure something can be done about it.

Moderation

If you want to help and moderate this community, please message leave a comment under this post or contact me in any other way.