SA-MP Forums Archive
[Plugin] Yet Another Lua Plugin - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Plugin Development (https://sampforum.blast.hk/forumdisplay.php?fid=18)
+--- Thread: [Plugin] Yet Another Lua Plugin (/showthread.php?tid=660169)



Yet Another Lua Plugin - IllidanS4 - 26.10.2018

YALP v1.0
Download
Tutorials
Introduction
This plugin allows you to use Lua, your favourite dynamic flexible scripting language, in SA-MP, for all purposes, be it sandboxing user scripts, debugging the server, or developing filterscripts and gamemodes. YALP provides lightweight and flexible Lua environment that can use existing or future natives from SA-MP or plugins.

The main feature of YALP is a powerful interop API to interact with existing Pawn natives and callbacks. It is very easy to call existing functions, without needing to declare them anywhere:
Code:
interop.native.SetPlayerPos(0, 1235.31, -2331.84, 12.33)
YALP automatically converts all arguments to their proper Pawn cells, and allows you to specify what the function returns in the actuall call. All standard cell and single-dimensional array types are supported.

Callbacks can be specified in a similar simple way:
Code:
function interop.public.OnPlayerConnect(playerid)
  -- ...
end
Thanks to these mechanisms, you can use any framework you want, or build your own in Lua, without depending on new versions of this plugin.

Configuration
There is no special XML, JSON or other configuration of this plugin, because you can specify everything when you create a new Lua state (you can create any number you wish, and run any code inside). You can specify what packages should be loaded or available in the Lua instance, how much maximum memory it should take, or even limit for how long Lua functions can run.

Features
All standard Lua packages are available (although some of the more unsafe ones aren't allowed by default). The powerful interop package can interface with any Pawn native function or callback. There is also an advanced timer library with support for simple asynchronous functions (with Lua coroutines) and all sorts of timers. The remote package contains functions for remote communication between two separate Lua instance (via serialization or proxies).

The standard Lua packages are also enhanced with a couple of useful functions. Please see the wiki for a list of all new functions.

The Pawn API is basically a port of the Lua C API, allowing advanced manipulation of the Lua machine. It is recommended to use the Lua API, since it can do everything that Pawn can do, but if you need to interact with an existing complex Pawn library, it is possible as well.

Sample code
pawn Code:
#include <a_samp>
#include <YALP>

public OnFilterScriptInit()
{
    new Lua:l = lua_newstate(); // creates a new Lua instance
   
    if(lua_loadfile(l, "script.lua")) // loads a Lua script and returns an error code
    {
        lua_stackdump(l); // prints the stack for more information about the error
        return;
    }
    lua_bind(l); // binds the Lua instance to the current Pawn filterscript/gamemode, so all interation with it is rerouted to Lua
    // if the binding succeeds, this code is not run (and the Lua instance is destroyed if the script is unloaded)
    lua_stackdump(l);
}
Code:
-- script.lua
local interop = require "interop"
interop.native.print("Hello from Lua!")



Re: Yet Another Lua Plugin - IllidanS4 - 26.10.2018

YALP v0.2 released!



Re: Yet Another Lua Plugin - IllidanS4 - 17.11.2018

YALP v0.3 released!



Re: Yet Another Lua Plugin - lepegadore - 19.11.2018

Is it possible to write an entire Gamemode (including MySQL, Streamer, some LUA anticheat etc) with this? And
I wonder if there are too much speed difference handling a big gamemode with many players (No premature optimization).

Also there is a Lua Mysql package? can you post some simple example? same with Streamer.


Thanks for the Plugin.


Re: Yet Another Lua Plugin - IllidanS4 - 19.11.2018

Quote:
Originally Posted by lepegadore
View Post
Is it possible to write an entire Gamemode (including MySQL, Streamer, some LUA anticheat etc) with this? And
I wonder if there are too much speed difference handling a big gamemode with many players (No premature optimization).

Also there is a Lua Mysql package? can you post some simple example? same with Streamer.


Thanks for the Plugin.
Writing a gamemode is definitely possible with this plugin. In fact, you can replace OnFilterScriptInit with OnGameModeInit in the sample code above, and it will work like a normal gamemode.

Standard Lua packages can be easily used from your code, just use "scriptfiles/lua/*.lua" for Lua packages, and "plugins/lua/*.dll" for C packages, and the require function will load them automatically as usual.

Any SA-MP plugin can also be used, be it streamer, MySQL or something else. Native functions from all plugins are available in the interop.native table, and can be called without any prior definitions. For more information, please see the wiki.


Re: Yet Another Lua Plugin - narwn - 20.11.2018

thread single/multi?


Re: Yet Another Lua Plugin - IllidanS4 - 20.11.2018

Quote:
Originally Posted by narwn
View Post
thread single/multi?
I assume you inquire about the multithreading capabilities of the plugin. Since Lua is singlethreaded (like Pawn), and the server API is not thread-safe, there is no special way to execute Lua code in a different process thread. The only way is to use a plugin that offers multithreading (like PawnPlus) and invoke Lua code from a new thread (but note that some YALP packages are not thread-safe as well).

However, you can use Lua coroutines freely, and there are functions like async and timer.wait that help you run asynchronous code.

There is also timer.parallel which can execute any piece of Lua code, but pauses it after a given number of instructions, and resumes it on the next tick. If you want to execute a piece of code that can take a long time but without blocking the server (and without any corruption caused by calling thread-unsafe functions), you can use this function.

That being said, I do have an idea how to implement multi-threading in Lua, with the use of the already existing remote package. Nevertheless, I have not found a proper use case for this, and it would be fairly limited anyway (but 100 % thread-safe).


Re: Yet Another Lua Plugin - IllidanS4 - 28.12.2018

YALP v0.4 released!



Re: Yet Another Lua Plugin - IllidanS4 - 08.02.2019

YALP v0.5 released!



Re: Yet Another Lua Plugin - Bolex_ - 08.02.2019

Thanks.