Вопросы по Lua скриптингу

Общая тема для вопросов по разработке скриптов на языке программирования Lua, в частности под MoonLoader.
  • Задавая вопрос, убедитесь, что его нет в списке частых вопросов и что на него ещё не отвечали (воспользуйтесь поиском).
  • Поищите ответ в теме посвященной разработке Lua скриптов в MoonLoader
  • Отвечая, убедитесь, что ваш ответ корректен.
  • Старайтесь как можно точнее выразить мысль, а если проблема связана с кодом, то обязательно прикрепите его к сообщению, используя блок [code=lua]здесь мог бы быть ваш код[/code].
  • Если вопрос связан с MoonLoader-ом первым делом желательно поискать решение на wiki.

Частые вопросы

Как научиться писать скрипты? С чего начать?
Информация - Гайд - Всё о Lua скриптинге для MoonLoader(https://blast.hk/threads/22707/)
Как вывести текст на русском? Вместо русского текста у меня какие-то каракули.
Изменить кодировку файла скрипта на Windows-1251. В Atom: комбинация клавиш Ctrl+Shift+U, в Notepad++: меню Кодировки -> Кодировки -> Кириллица -> Windows-1251.
Как получить транспорт, в котором сидит игрок?
Lua:
local veh = storeCarCharIsInNoSave(PLAYER_PED)
Как получить свой id или id другого игрока?
Lua:
local _, id = sampGetPlayerIdByCharHandle(PLAYER_PED) -- получить свой ид
local _, id = sampGetPlayerIdByCharHandle(ped) -- получить ид другого игрока. ped - это хендл персонажа
Как проверить, что строка содержит какой-то текст?
Lua:
if string.find(str, 'текст', 1, true) then
-- строка str содержит "текст"
end
Как эмулировать нажатие игровой клавиши?
Lua:
local game_keys = require 'game.keys' -- где-нибудь в начале скрипта вне функции main

setGameKeyState(game_keys.player.FIREWEAPON, -1) -- будет сэмулировано нажатие клавиши атаки
Все иды клавиш находятся в файле moonloader/lib/game/keys.lua.
Подробнее о функции setGameKeyState здесь: lua - setgamekeystate | BlastHack — DEV_WIKI(https://www.blast.hk/wiki/lua:setgamekeystate)
Как получить id другого игрока, в которого целюсь я?
Lua:
local valid, ped = getCharPlayerIsTargeting(PLAYER_HANDLE) -- получить хендл персонажа, в которого целится игрок
if valid and doesCharExist(ped) then -- если цель есть и персонаж существует
  local result, id = sampGetPlayerIdByCharHandle(ped) -- получить samp-ид игрока по хендлу персонажа
  if result then -- проверить, прошло ли получение ида успешно
    -- здесь любые действия с полученным идом игрока
  end
end
Как зарегистрировать команду чата SAMP?
Lua:
-- До бесконечного цикла/задержки
sampRegisterChatCommand("mycommand", function (param)
     -- param будет содержать весь текст введенный после команды, чтобы разделить его на аргументы используйте string.match()
    sampAddChatMessage("MyCMD", -1)
end)
Крашит игру при вызове sampSendChat. Как это исправить?
Это происходит из-за бага в SAMPFUNCS, когда производится попытка отправки пакета определенными функциями изнутри события исходящих RPC и пакетов. Исправления для этого бага нет, но есть способ не провоцировать его. Вызов sampSendChat изнутри обработчика исходящих RPC/пакетов нужно обернуть в скриптовый поток с нулевой задержкой:
Lua:
function onSendRpc(id)
  -- крашит:
  -- sampSendChat('Send RPC: ' .. id)

  -- норм:
  lua_thread.create(function()
    wait(0)
    sampSendChat('Send RPC: ' .. id)
  end)
end
 
Последнее редактирование:

XRLM

Против ветра рождённый
Модератор
1,659
1,339
И как сделать поиск в list диалоге и выбрать нужный пункт? И как выводить все данные записанные в .ini файле? Например в mainIni.config.
функция поиска нужного листбокса в диалоге https://www.blast.hk/threads/80351/
гайд по конфигам https://www.blast.hk/threads/96313/

Как в imgui повернуть изображение?
https://www.blast.hk/threads/13380/page-10#post-433209 не понял, для имгуи это или же для мимгуи
upd:
нашел то что тебе нужно https://www.blast.hk/threads/13380/page-11
 

si6on1

Новичок
7
0
как удалять ненужные строчки? типо HA:1 ; RM:3 (мигающие две строчки при бизваре)
 

si6on1

Новичок
7
0
скинь скрин или чатлог этих строк
sa-mp-296.png

они мигают еще
 
D

deleted-user-139653

Гость
че за кринж, хп не хочется обновлятся, рендерит только изначальное значение, когда вошел в игру
и да, я пытаюсь сделать, чтобы работало без sampfuncs.asi и cleo.asi

Код:
require "sampfuncs"
require "moonloader"

local health = getCharHealth(PLAYER_PED)
local font = renderCreateFont(Arial, 10, 13)

function main()
while true do
    wait(0)
        renderFontDrawText(font, health, 1161, 112, -1)
    end
end
 

chapo

tg/inst: @moujeek
Всефорумный модератор
9,238
12,672
че за кринж, хп не хочется обновлятся, рендерит только изначальное значение, когда вошел в игру
и да, я пытаюсь сделать, чтобы работало без sampfuncs.asi и cleo.asi

Код:
require "sampfuncs"
require "moonloader"

local health = getCharHealth(PLAYER_PED)
local font = renderCreateFont(Arial, 10, 13)

function main()
while true do
    wait(0)
        renderFontDrawText(font, health, 1161, 112, -1)
    end
end
ну ты получаешь хп только один раз, ясное дело что значение будет "статичным"
Lua:
require "moonloader"
local font = renderCreateFont(Arial, 10, 13)

function main()
    while true do
        wait(0)
        local health = getCharHealth(PLAYER_PED)
        renderFontDrawText(font, health, 1161, 112, -1)
    end
end
 

ARMOR

God has forsaken us
Модератор
5,067
7,436
Lua:
local samper = require 'lib.samp.events'
function sampev.onDisplayGameText(style, time, text)
    if style == 3 and text:find('~n~~n~~n~~n~~n~~n~.+') then
        return false
    end
end

как скрывать/показывать радар?
Lua:
local memory = require 'memory'

memory.setuint8(0xBA676C, 2, true) -- Выключить радар
memory.setuint8(0xBA676C, 0, true) -- Включить радар
 
  • Нравится
Реакции: XRLM

ARMOR

God has forsaken us
Модератор
5,067
7,436
в режиме спектатора никак его не включить?
 
  • Нравится
Реакции: XRLM
D

deleted-user-139653

Гость
ну ты получаешь хп только один раз, ясное дело что значение будет "статичным"
Lua:
require "moonloader"
local font = renderCreateFont(Arial, 10, 13)

function main()
    while true do
        wait(0)
        local health = getCharHealth(PLAYER_PED)
        renderFontDrawText(font, health, 1161, 112, -1)
    end
end
Чапуня, еще такой вопрос, а как сделать так, чтобы когда я скрывал худ на F7, hp тоже исчезало?
 

chapo

tg/inst: @moujeek
Всефорумный модератор
9,238
12,672
Чапуня, еще такой вопрос, а как сделать так, чтобы когда я скрывал худ на F7, hp тоже исчезало?
Lua:
require "moonloader"
local font = renderCreateFont(Arial, 10, 13)

function main()
    while true do
        wait(0)
        if sampGetChatDisplayMode() ~= 0 then
            local health = getCharHealth(PLAYER_PED)
            renderFontDrawText(font, health, 1161, 112, -1)
        end
    end
end
 
  • Нравится
Реакции: deleted-user-139653

RaMero

Известный
444
137
Есть вот такая функция от @chapo (использовал в фортнайт худе), я немножно переделал под себя, добавил 1 чекбокс, нажимается, работает, но когда ставлю второй чекбокс, ужене работает, нажимаю но ничего не происходит, голову ломаю, как тут поступить? (ImGUi)

Код:
function imgui.FunctionChild(title, description, bool, button, size, colors)
    local colors = {
        back = imgui.ImVec4(0.13, 0.16, 0.2, 1),
        line = imgui.ImVec4(1, 0, 0.3, 1),
        text = imgui.ImVec4(1, 1, 1, 1),
        textd = imgui.ImVec4(0.5, 0.5, 0.5, 1),
    }
    local c = imgui.GetCursorPos()
    local p = imgui.GetCursorScreenPos()
    local dl = imgui.GetWindowDrawList()

    dl:AddRectFilled(p, imgui.ImVec2(p.x + size.x, p.y + size.y), imgui.GetColorU32(colors.back), 5, 2 + 8)
    dl:AddRectFilled(p, imgui.ImVec2(p.x + 5.5, p.y + size.y), imgui.GetColorU32(colors.line), 5, 1 + 8)

    dl:AddText(imgui.ImVec2(p.x + 10, p.y + 10), imgui.GetColorU32(colors.text), title)
    dl:AddText(imgui.ImVec2(p.x + 10, p.y + 25), imgui.GetColorU32(colors.textd), description)

    imgui.SetCursorPos(imgui.ImVec2(c.x + size.x - 20 - 10, c.y + 10))
    if bool ~= nil then
        imgui.Checkbox('##'..title..'::'..description, bool)
    end
    imgui.SetCursorPos(imgui.ImVec2(c.x + size.x - 20 - 40, c.y + 10))
    if button ~= nil then
        imgui.Button(fa.ICON_FA_COG, imgui.ImVec2(25, 23), button)
    end
    imgui.SetCursorPos(imgui.ImVec2(c.x + 10, c.y + 20 + 25))
    imgui.BeginChild('child:controls:'..title..':'..description, imgui.ImVec2(size.x - 15, size.y - 25 - 25), false)
end

В OnDrawFrame
Код:
imgui.SetCursorPos(imgui.ImVec2(15, 50 + 15))
            imgui.FunctionChild(u8'WallHack', u8'Настройки WH', bool1, button1, imgui.ImVec2(200, 50), colors) imgui.EndChild()
            imgui.SetCursorPos(imgui.ImVec2(15, 110 + 15))
                imgui.FunctionChild(u8'Render', u8'Настройки рендера', bool2, button2, imgui.ImVec2(200, 50), colors) imgui.EndChild()
                imgui.SetCursorPos(imgui.ImVec2(15, 170 + 15))
                imgui.FunctionChild(u8'Лазер', u8'Настройки лазера', bool3, button3, imgui.ImVec2(200, 50), colors) imgui.EndChild()
1.png
 

вайега52

Налуашил состояние
Модератор
3,002
3,139
Как можно обратиться к индексу в массиве, используя функцию мимгуи?
Lua:
local OUTCOMING_RPC = {
    {23, {"playerId", "int16"}, {"source", "int8"}, description = "Отправляется при нажатии на игрока в списке игроков.", nop = false},
}

for i = 1, #OUTCOMING_RPC do
    local info = getRPC(OUTCOMING_RPC[i])
    local title = info.name .. " [" .. info.id .. "]"
    if title:lower():find(str(config.search):lower()) then
        if imgui.Selectable(config.hints[0] and "##" .. info.name .. " [" .. info.id .. "]" or info.name .. " [" .. info.id .. "]", Nop_OutRPC.selected == i) then
            Nop_OutRPC.selected = i
        end
    
        if config.hints[0] then
            imgui.SameLine(config.hints and -0.000000001 or nil)
            imgui.TextQuestion(config.hints[0] and info.name .. " [" .. info.id .. "]" or "( ? )", u8(OUTCOMING_RPC[i].description))
        end

        if Nop_OutRPC.selected == i then
            imgui.ToggleButton(OUTCOMING_RPC[Nop_OutRPC.selected].nop and "NOP: " .. info.name .. " [ON]" or "NOP: " .. info.name .. " [OFF]", OUTCOMING_RPC[Nop_OutRPC.selected].nop)
        end
    end
end

function imgui.ToggleButton(str_id, value)
    local duration = 0.3
    local p = imgui.GetCursorScreenPos()
    local DL = imgui.GetWindowDrawList()
    local size = imgui.ImVec2(40, 20)
    local title = str_id:gsub("##.*$", "")
    local ts = imgui.CalcTextSize(title)
    local cols = {
        enable = imgui.GetStyle().Colors[imgui.Col.ButtonActive],
        disable = imgui.GetStyle().Colors[imgui.Col.TextDisabled]   
    }
    local radius = 6
    local o = {
        x = 4,
        y = p.y + (size.y / 2)
    }
    local A = imgui.ImVec2(p.x + radius + o.x, o.y)
    local B = imgui.ImVec2(p.x + size.x - radius - o.x, o.y)

    if AI_TOGGLE[str_id] == nil then
        AI_TOGGLE[str_id] = {
            clock = nil,
            color = value[0] and cols.enable or cols.disable,
            pos = value[0] and B or A
        }
    end
    local pool = AI_TOGGLE[str_id]
    
    imgui.BeginGroup()
        local pos = imgui.GetCursorPos()
        local result = imgui.InvisibleButton(str_id, imgui.ImVec2(size.x, size.y))
        if result then
            value[0] = not value[0]
            pool.clock = os.clock()
        end
        if #title > 0 then
            local spc = imgui.GetStyle().ItemSpacing
            imgui.SetCursorPos(imgui.ImVec2(pos.x + size.x + spc.x, pos.y + ((size.y - ts.y) / 2)))
            imgui.Text(title)
        end
    imgui.EndGroup()

     if pool.clock and os.clock() - pool.clock <= duration then
        pool.color = bringVec4To(
            imgui.ImVec4(pool.color),
            value[0] and cols.enable or cols.disable,
            pool.clock,
            duration
        )

        pool.pos = bringVec2To(
            imgui.ImVec2(pool.pos),
            value[0] and B or A,
            pool.clock,
            duration
        )
    else
        pool.color = value[0] and cols.enable or cols.disable
        pool.pos = value[0] and B or A
    end

    DL:AddRect(p, imgui.ImVec2(p.x + size.x, p.y + size.y), ToU32(pool.color), 10, 15, 1)
    DL:AddCircleFilled(pool.pos, radius, ToU32(pool.color))

    return result
end

Код:
[ML] (error) RakNet Changer: C:\ÂÀÆÍÎÅ!!!\ega-nrp\moonloader\raknet.lua:962: attempt to index local 'value' (a boolean value)
stack traceback:
    C:\ÂÀÆÍÎÅ!!!\ega-nrp\moonloader\raknet.lua:962: in function 'ToggleButton'
    C:\ÂÀÆÍÎÅ!!!\ega-nrp\moonloader\raknet.lua:577: in function '_draw'
    C:\ÂÀÆÍÎÅ!!!\ega-nrp\moonloader\lib\mimgui\init.lua:107: in function <C:\ÂÀÆÍÎÅ!!!\ega-nrp\moonloader\lib\mimgui\init.lua:91>
[ML] (error) RakNet Changer: Script died due to an error. (24457A24)

[ATTACH type="full"]175997[/ATTACH]
 

Вложения

  • 1667535548600.png
    1667535548600.png
    19 KB · Просмотры: 38