Lua Plugin

Started by DizzasTeR, Nov 05, 2020, 12:53 PM

Previous topic - Next topic

DizzasTeR

Here's a Lua plugin for VCMP. It lets you script in Lua (No sh!t Sherlock :o) Also because python sucks kekw @Shad

https://github.com/DizzasTeR/VCMP-Lua

Downloads are available in the release section over at GitHub

Features:
  • Complete API coverage
  • Async features for database operations
  • Multiple utility libraries (Json, web requests, etc)
  • The power of Lua modules
  • Better API
  • Developer friendly

Blank Script (Lua Version): https://github.com/DizzasTeR/VCMP-Lua/wiki/Blank-Script

Important: You need OpenSSL (F**K MySQL and everything related to it): https://slproweb.com/products/Win32OpenSSL.html OR Just place the two DLLs available with plugin download and place them in server directory

If you are going to try this out, I've written a README on GitHub which you should totally check out. I've also written as much wiki on github as I could and its written enough to let anyone be able to do stuff.

Note: Please know that I may have missed some functions of VCMP, if someone is actually going to use this and you come across a functionality that doesn't exist, let me know I can try looking into it.

:edit: You CAN set up LuaRocks or LuaDist to use third-party pre-made lua libraries but I didn't really test them, so if you are really into Lua, you may try that.

Inferno

Viva la VU
VFS Developer
VCCNR Administrator

NicusorN5

Speed performance between LUA and Squirreil when?

DizzasTeR

#3
While there's more evidence towards Squirrel being more "efficient" than Lua because it provides functionality based on your needs (Take static and dynamic arrays in squirrel for example, you use one which fits more) however in Lua you just straight up throw Tables for anything and it just works. Both languages are close, one has advantages over the other.

However LuaJIT is faster than Squirrel I believe.

:edit: Also Lua is actively maintained right now and more popularly supported

DizzasTeR

Update
— Added SQLite3 Module (https://github.com/DizzasTeR/VCMP-Lua/wiki/SQLite3-Module)
— Small new features & logical fixes

Quote from: Athanatos on Nov 06, 2020, 05:24 PMSpeed performance between LUA and Squirreil when?
After testing, faster than squirrel. How much and in what scenarios is subjective.

DizzasTeR

Update
JSON Script Module. Provides following two functions: JSON.stringify and JSON.parse
— Server.shutdown() was missed, added now.

DMWarrior

Good work! Lua support can be very interesting, not only due to it's popularity, but because it has more libraries avaliable than Squirrel, too.

I still don't know what I could do with it, though. But for anyone interested in giving a try, I managed to rewrite most of the VC:MP Blank Server to Lua, as well looking through each function and finding which ones are missing. The only problem is that SendDataToClient() doesn't work, but everything else seems to be fine.

This, alongside the Wiki, should be enough for getting started:
VC:MP Blank Server (Lua Script)

DizzasTeR

#7
Such an amazing contribution @DMWarrior

I've looked through the code and everything seems great, I don't know regarding the SendDataToClient failing to work but I'm sure I did test all the stream stuff however I'll double check it again. However I would rather just recommend using the JSON module to just send a nice JSON string as a single string and then using the squirrel json script on client side to decode it back, its one of the easiest and best ways to just send info back and forth.

It seems you made a mistake writing the SendDataToClient function, when you're taking the stramType from argument[1], you no longer need to include the first parameter in your further data extraction, you should be going through arguments[2] to #arguments or basically #arguments - 1 (Updated version of your script is now on github with a fixed SendDataToClient)

I will also add that script to the github wiki as a source of code example and getting started.
:edit: Available now: https://github.com/DizzasTeR/VCMP-Lua/wiki

DMWarrior

#8
I made some minor changes on Blank Server:
Updated Blank Server (Lua script)

After setting up "package.path", we can easily load modules with require(). For anyone who's not familiar with Lua, here's an example:

MyModule.lua
----
-- @module MyModule
--
local M = {}

----
-- A function.
--
function M.hello()
  print("Hello, world!")
end

return M

Server script
local MyModule = require("MyModule")
MyModule.hello()

And since Lua has no classes, libraries like middleclass can be extremely useful, specially when combined with modules. It's easier to work with in comparison to Squirrel, at least in my opinion.

After noticing that VEH_ constants are missing, I decided to make a very simple list, too:
Vehicles List (Lua Script)

Nothing amazing or interesting yet, though, but it may gain some more interest once there's more scripts available to work with. One idea would be rewrite some scripts from Squirrel to Lua so they could be used as example or base for other scripts. I don't know if anyone would do it, but I could try doing this with some of my scripts to see how it looks.

DizzasTeR

Lua Plugin 2.0 Beta released

After knowing some interesting people who started using this plugin, the development and updates have been going strong to add the remaining bits and features.

The latest 2.0 release provides a CPR module which gives you access to Remote.fetch and Remote.post. More on them on wiki.

habi


MatheuS

Lua Plugin has been updated!

Now the plugin is in the latest version of VC-MP with the new features.

Features:
Player
  • player:kill()
  • player:set3DArrowToPlayer(Player targetPlayer, bool state)
  • player:get3DArrowToPlayer(Player targetPlayer)
  • player:setDrunkHandling(int drunkLevel)
  • player:getDrunkHandling()
  • player:setDrunkVisuals(int drunkVisual)
  • player:getDrunkVisuals()

Vehicle
  • vehicle:set3DArrowToPlayer(Player player, bool state)
  • vehicle:get3DArrowToPlayer(Player targetPlayer)

Vehicle Options
  • engine
  • boot
  • bonnet

Server Options
  • disableCrouch
  • bleeding

Github: https://github.com/rasikhq/VCMP-Lua
Wiki: https://github.com/rasikhq/VCMP-Lua/wiki

Blank Script (Lua Version): https://github.com/DizzasTeR/VCMP-Lua/wiki/Blank-Script

Downloads are available in the release section over at GitHub
if( !sucess ) tryAgain();

X Dukinja

 :( The Lua Plugin is not Loading

Plugin error >> LoadLibrary() 'plugins/LuaPlugin_x64.dll' failed: Code 126
Failed to load plugin: LuaPlugin_x64

DizzasTeR

Quote from: X Dukinja on Jul 24, 2025, 09:32 AM:( The Lua Plugin is not Loading

Plugin error >> LoadLibrary() 'plugins/LuaPlugin_x64.dll' failed: Code 126
Failed to load plugin: LuaPlugin_x64

Interesting, some other folks had this issue too. Did you try any previous versions if they work? Also it may be difficult to provide support since I cannot find enough time to maintain this project. Some members in the community are still actively using this plugin and even contribute to the plugin so they may be able to help.

Necroso

Changelog: Version 2.9 Beta (Unofficial)

This release introduces a major database backend upgrade by replacing MySQL with PostgreSQL, providing better stability, performance, and modern database features.

In addition, the plugin is now available for both 32-bit and 64-bit builds on Windows and Linux, making deployment easier across different server environments.





Database Migration

The plugin now uses PostgreSQL instead of MySQL.

Improvements

  • Replaced MySQL backend with PostgreSQL
  • Added automatic reconnect and connection validation
  • Added support for parameterized queries
  • Added native JSON / JSONB support
  • Added automatic Lua table ↔ JSON conversion
  • Improved query safety and stability
  • Better handling for integers, floats, booleans, NULL values, and arrays



Platform Support

Available Builds

  • Windows x86 (32-bit)
  • Windows x64 (64-bit)
  • Linux x86 (32-bit)
  • Linux x64 (64-bit)



PostgreSQL Usage Guide

Creating an Account

Code (lua) Select
local account = PostgreSQL.createAccount(
    "127.0.0.1",
    "postgres",
    "password",
    "database_name",
    5432
)

Creating a Connection

Code (lua) Select
local db = PostgreSQL.createConnection(account)

Executing Queries

Code (lua) Select
db:execute(
    "INSERT INTO players(name, score) VALUES($1, $2)",
    "player_name",
    100
)

Querying Data

Code (lua) Select
local result = db:query(
    "SELECT * FROM players WHERE id = $1",
    1
)

for _, row in ipairs(result) do
    print(row.name, row.score)
end

JSON Support

Lua tables are automatically converted to PostgreSQL JSON.

Code (lua) Select
local inventory = {
    weapons = {
        "M4",
        "Shotgun"
    },
    money = 5000
}

db:execute(
    "INSERT INTO users(data) VALUES($1)",
    inventory
)

JSON and JSONB fields are automatically converted back into Lua tables when queried.



General Improvements

  • Replaced MySQL with PostgreSQL as the default database backend
  • Added native support for Windows and Linux (x86 and x64)
  • Improved database abstraction layer
  • Internal stability improvements and refactoring

Downloads are available here: https://github.com/Necroso/VCMP-Lua/releases/tag/v2.9-beta