Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open.mp compatibility #1

Open
Mergevos opened this issue Oct 6, 2023 · 0 comments
Open

open.mp compatibility #1

Mergevos opened this issue Oct 6, 2023 · 0 comments

Comments

@Mergevos
Copy link

Mergevos commented Oct 6, 2023

#include <open.mp>
#include <YALP>

main()
{
    return;
}

new Lua:l;

public OnGameModeInit()
{
    print("Test");
    new File:scriptfile = fopen("main.lua", io_read);
    l = lua_newstate();
    if(lua_load(l, "ScriptReader", _:scriptfile, flength(scriptfile) char) || !lua_bind(l))
    {
        lua_stackdump(l);
        lua_close(l);
    }
    return 1;
}

forward ScriptReader(Lua:L, buffer[], File:file, size);
public ScriptReader(Lua:L, buffer[], File:file, size)
{
    new length = flength(file);
    new read = fblockread(file, buffer, length * 8 / cellbits);
    buffer[read] =
        ((fgetchar(file, false) & 0xFF)) |
        ((fgetchar(file, false) & 0xFF) << 8) |
        ((fgetchar(file, false) & 0xFF) << 16);
    fclose(file);
    return -length;
}
do return "test" end

local interop = require "interop"

local public = interop.public
local SendClientMessage, SetPlayerHealth
import(interop.native) -- assigns SendClientMessage and SetPlayerHealth from interop.native.SendClientMessage and interop.native.SetPlayerHealth

local COLOR_WHITE = 0xFFFFFFFF

local function tofloat(num) -- when calling native functions with a float parameter, a float value must be ensured
    return 0.0 + num
end

function public.OnPlayerConnect(playerid) -- adding a function to the "public" table automatically registers the callback (based on the name)
    SendClientMessage(playerid, COLOR_WHITE, "Hello from Lua!") -- calling a native function requires almost no modifications
end

local commands = {}

function commands.hp(playerid, params)
    local hp = tonumber(params)
    if not hp then
        return SendClientMessage(playerid, COLOR_WHITE, "Usage: /hp [value]")
      end
      SetPlayerHealth(playerid, tofloat(hp))
      SendClientMessage(playerid, COLOR_WHITE, "Your health was set to "..hp.."!")
      return true
end

function public.OnPlayerCommandText(playerid, cmdtext)
      playerid = interop.asinteger(playerid) -- YALP cannot guess the type of the arguments, so they must be explicitly converted
      cmdtext = interop.asstring(cmdtext)
  
      local ret
      cmdtext:gsub("^/([^ ]+) ?(.*)$", function(cmd, params)
        local handler = commands[string.lower(cmd)]
        if handler then
              ret = handler(playerid, params)
        end
      end)
      return ret
end

print("Lua script initialized!")

[2023-10-06T02:45:44+0200] [Info] Loading plugin: YALP
[2023-10-06T02:45:44+0200] [Info] YALP v1.1 loaded
[2023-10-06T02:45:44+0200] [Info] Created by IllidanS4
[2023-10-06T02:45:44+0200] [Info] Test
[2023-10-06T02:45:44+0200] [Error] (sleep mode)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant