Вопросы по 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
 
Последнее редактирование:

Карен

Участник
112
17
Lua:
---@param x number
---@param y number
---@param z number
---@return boolean Success
---@return { handle: any, dist: number }
local function getNearestVehicleFromPoint(x, y, z)
    local result = { handle = nil, dist = math.huge };
    for _, veh in pairs(getAllVehicles()) do
        local dist = getDistanceBetweenCoords3d(x, y, z, getCarCoordinates(veh));
        if (dist < result.dist) then
            result = {
                handle = veh,
                dist = dist
            };
        end
    end
    return result.handle ~= nil, result;
end
1693512687063.png

Вызвал функцию вот-так
1693512716593.png

Выдаёт краш
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,776
11,226
Убрал local и всё заработало, только остался один нюнас, res1 и res2 попробовал вывести их в чат, в итоге просто пустота, с чем это связано?
res1 - true/false
res2 - таблица с полями handle и dist
 

Карен

Участник
112
17
res1 - true/false
res2 - таблица с полями handle и dist
Как блин её вывести, я и res[1], res[2], res[0] юзаю, и просто print(res2), чёт нихуя нету, в первом случае только nil а в 2м случае вот это вот:
[ML] (script) FindCar: table: 0x0fe9b128
[ML] (script) FindCar: table: 0x0fe9b2c8
[ML] (script) FindCar: table: 0x0fe9b468
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,776
11,226
Как блин её вывести, я и res[1], res[2], res[0] юзаю, и просто print(res2), чёт нихуя нету, в первом случае только nil а в 2м случае вот это вот:
[ML] (script) FindCar: table: 0x0fe9b128
[ML] (script) FindCar: table: 0x0fe9b2c8
[ML] (script) FindCar: table: 0x0fe9b468
if (res1) then print(res2.handle) end
 

tsunamiqq

Участник
429
16
Как сделать сохранение темы?
Попытался сделать таким способом, не получилось.

Lua:
--Библиотеки

--Вне окна
local inicfg = require('inicfg')
local directIni = 'Script.ini'
local mainIni = inicfg.load(inicfg.load({
    settings = {
        radioButtonColorRed = false
        selectedTheme = selectedTheme or 0
    }
}, directIni))
inicfg.save(mainIni, directIni)
--Вне окна
local settings = {
    radioButtonColorRed = new.int(mainIni.settings.radioButtonColorRed),
    selectedTheme = new.int(mainIni.settings.selectedTheme)
}

--В окне
local COLOR_SELECT = imgui.ImVec4(1, 0, 0, 1)
if imgui.ColoredRadioButtonBool('##colorRed', settings.radioButtonColorRed[0], COLOR_SELECT) then
    Theme(settings.selectedTheme[0], COLOR_SELECT)
    mainIni.settings.radioButtonColorRed = not mainIni.settings.radioButtonColorRed
    settings.radioButtonColorRed[0] = mainIni.settings.radioButtonColorRed
end

--Вне окна
function imgui.ColoredRadioButtonBool(label, state, color)
    imgui.PushStyleColor(imgui.Col.CheckMark, color)
    imgui.PushStyleColor(imgui.Col.FrameBg, color)
    imgui.PushStyleColor(imgui.Col.FrameBgActive, color)
    imgui.PushStyleColor(imgui.Col.FrameBgHovered, color)
    local radioButton = imgui.RadioButtonBool(label, state)
    imgui.PopStyleColor(4)
    return radioButton
end

--Вне окна
function Theme(id, color, chroma_multiplier, accurate_shades)
    local vec2, vec4 = imgui.ImVec2, imgui.ImVec4
    imgui.SwitchContext()
    local style = imgui.GetStyle()
    local colors = style.Colors
    local flags = imgui.Col
 
    do -- style
        --==[ STYLE ]==--
        imgui.GetStyle().WindowPadding = imgui.ImVec2(5, 5)
        imgui.GetStyle().FramePadding = imgui.ImVec2(5, 5)
        imgui.GetStyle().ItemSpacing = imgui.ImVec2(5, 5)
        imgui.GetStyle().ItemInnerSpacing = imgui.ImVec2(2, 2)
        imgui.GetStyle().TouchExtraPadding = imgui.ImVec2(0, 0)
        imgui.GetStyle().IndentSpacing = 0
        imgui.GetStyle().ScrollbarSize = 10
        imgui.GetStyle().GrabMinSize = 10
        --==[ ROUNDING ]==--
        imgui.GetStyle().WindowRounding = 8
        imgui.GetStyle().ChildRounding = 8
        imgui.GetStyle().FrameRounding = 5
        imgui.GetStyle().PopupRounding = 8
        imgui.GetStyle().ScrollbarRounding = 8
        imgui.GetStyle().GrabRounding = 8
        imgui.GetStyle().TabRounding = 8
        --==[ ALIGN ]==--
        imgui.GetStyle().WindowTitleAlign = imgui.ImVec2(0.5, 0.5)
        imgui.GetStyle().ButtonTextAlign = imgui.ImVec2(0.5, 0.5)
        imgui.GetStyle().SelectableTextAlign = imgui.ImVec2(0.5, 0.5)
    end
    local palette = monet.buildColors(id, color, chroma_multiplier, accurate_shades)
    if id == 0 then -- colors
        colors[imgui.Col.WindowBg] = imgui.ImVec4(0.14, 0.12, 0.16, 1.00)
        colors[imgui.Col.ChildBg] = imgui.ImVec4(0.30, 0.20, 0.39, 0.00)
        colors[imgui.Col.PopupBg] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.Border] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.BorderShadow] = imgui.ImVec4(0.00, 0.00, 0.00, 0.00)
        colors[imgui.Col.FrameBg] = imgui.ImVec4(0.28, 0.27, 0.27, 0.28)
        colors[imgui.Col.FrameBgHovered] = imgui.ImVec4(0.41, 0.19, 0.63, 0.68)
        colors[imgui.Col.FrameBgActive] = imgui.ImVec4(0.41, 0.19, 0.63, 1.00)
        colors[imgui.Col.TitleBg] = imgui.ImVec4(0.41, 0.19, 0.63, 0.45)
        colors[imgui.Col.TitleBgCollapsed] = imgui.ImVec4(0.41, 0.19, 0.63, 0.35)
        colors[imgui.Col.TitleBgActive] = imgui.ImVec4(0.41, 0.19, 0.63, 0.78)
        colors[imgui.Col.MenuBarBg] = imgui.ImVec4(0.30, 0.20, 0.39, 0.57)
        colors[imgui.Col.ScrollbarBg] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.ScrollbarGrab] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.ScrollbarGrabHovered] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.ScrollbarGrabActive] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.CheckMark] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.SliderGrab] = imgui.ImVec4(0.41, 0.19, 0.63, 0.24)
        colors[imgui.Col.SliderGrabActive] = imgui.ImVec4(0.41, 0.19, 0.63, 1.00)
        colors[imgui.Col.Button] = imgui.ImVec4(0.84, 0.01, 0.06, 0.84)
        colors[imgui.Col.ButtonHovered] = imgui.ImVec4(1, 0, 0.07, 1)
        colors[imgui.Col.ButtonActive] = imgui.ImVec4(0.84, 0.01, 0.06, 0.8)
        colors[imgui.Col.Header] = imgui.ImVec4(0.41, 0.19, 0.63, 0.76)
        colors[imgui.Col.HeaderHovered] = imgui.ImVec4(0.41, 0.19, 0.63, 0.86)
        colors[imgui.Col.HeaderActive] = imgui.ImVec4(0.41, 0.19, 0.63, 1.00)
        colors[imgui.Col.ResizeGrip] = imgui.ImVec4(0.41, 0.19, 0.63, 0.20)
        colors[imgui.Col.ResizeGripHovered] = imgui.ImVec4(0.41, 0.19, 0.63, 0.78)
        colors[imgui.Col.ResizeGripActive] = imgui.ImVec4(0.41, 0.19, 0.63, 1.00)
        colors[imgui.Col.PlotLines] = imgui.ImVec4(0.89, 0.85, 0.92, 0.63)
        colors[imgui.Col.PlotLinesHovered] = imgui.ImVec4(0.41, 0.19, 0.63, 1.00)
        colors[imgui.Col.PlotHistogram] = imgui.ImVec4(0.89, 0.85, 0.92, 0.63)
        colors[imgui.Col.PlotHistogramHovered] = imgui.ImVec4(0.41, 0.19, 0.63, 1.00)
        colors[imgui.Col.TextSelectedBg] = imgui.ImVec4(0.41, 0.19, 0.63, 0.43)
    
        COLOR_SELECT = color
    end
    if id == 1 then -- colors
 
        colors[flags.Text] = to_vec4(palette.neutral1.color_100)
        colors[flags.WindowBg] = to_vec4(palette.accent1.color_800)
        colors[flags.ChildBg] = to_vec4(palette.accent1.color_800)
        colors[flags.PopupBg] = to_vec4(palette.accent2.color_700)
        colors[flags.Border] = to_vec4(palette.neutral1.color_600)
        colors[flags.BorderShadow] = to_vec4(palette.neutral2.color_800)
        colors[flags.FrameBg] = to_vec4(palette.accent1.color_600)
        colors[flags.FrameBgHovered] = to_vec4(palette.accent1.color_600)
        colors[flags.FrameBgActive] = to_vec4(palette.accent1.color_500)
        colors[flags.TitleBgActive] = to_vec4(palette.accent1.color_700)
        colors[flags.ScrollbarBg] = to_vec4(palette.accent1.color_700)
        colors[flags.ScrollbarGrab] = to_vec4(palette.accent2.color_500)
        colors[flags.ScrollbarGrabHovered] = to_vec4(palette.accent2.color_400)
        colors[flags.ScrollbarGrabActive] = to_vec4(palette.accent2.color_300)
        colors[flags.CheckMark] = to_vec4(palette.neutral1.color_100)
        colors[flags.SliderGrab] = to_vec4(palette.accent2.color_400)
        colors[flags.SliderGrabActive] = to_vec4(palette.accent2.color_300)
        colors[flags.Button] = to_vec4(palette.accent1.color_600)
        colors[flags.ButtonHovered] = to_vec4(palette.accent1.color_500)
        colors[flags.ButtonActive] = to_vec4(palette.accent1.color_600)
        colors[flags.Header] = to_vec4(palette.accent1.color_700)
        colors[flags.HeaderHovered] = to_vec4(palette.accent1.color_600)
        colors[flags.HeaderActive] = to_vec4(palette.accent1.color_500)
        colors[flags.Separator] = to_vec4(palette.accent2.color_100)
        colors[flags.SeparatorHovered] = to_vec4(palette.accent2.color_200)
        colors[flags.SeparatorActive] = to_vec4(palette.accent2.color_100)
        colors[flags.ResizeGrip] = to_vec4(palette.accent2.color_800)
        colors[flags.ResizeGripHovered] = to_vec4(palette.accent2.color_700)
        colors[flags.ResizeGripActive] = to_vec4(palette.accent2.color_600)
        colors[flags.Tab] = to_vec4(palette.accent1.color_600)
        colors[flags.TabHovered] = to_vec4(palette.accent1.color_500)
        colors[flags.TabActive] = to_vec4(palette.accent1.color_400)
        colors[flags.PlotLines] = to_vec4(palette.accent3.color_200)
        colors[flags.PlotLinesHovered] = to_vec4(palette.accent3.color_100)
        colors[flags.PlotHistogram] = to_vec4(palette.accent3.color_200)
        colors[flags.PlotHistogramHovered] = to_vec4(palette.accent3.color_100)
        colors[flags.DragDropTarget] = to_vec4(palette.accent3.color_600)
     
        COLOR_SELECT = color
     end
 end
актуал
 

хромиус)

спокойно, это всего лишь слива
Друг
4,956
3,235
Lua:
local imgui = require 'mimgui'
local sampev = require('lib.samp.events')
local encoding = require 'encoding'
local ffi = require 'ffi'
local str = ffi.string
encoding.default = 'CP1251'
u8 = encoding.UTF8

local new = imgui.new
local DialogText = new.char[10000]()
local renderWindow = new.bool(false)
local dialog_text = ''


local newFrame = imgui.OnFrame(
    function() return renderWindow[0] end,
    function(player)
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 600, 450
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2, resY / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Main Window', renderWindow)
        imgui.StrCopy(DialogText, u8:decode(str(dialog_text)))
        imgui.InputTextMultiline(u8"##Многострочный инпут", DialogText, 10000,imgui.ImVec2(600, 250))
        
        imgui.End()
    end
)

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('okno', function()
        renderWindow[0] = not renderWindow[0]
    end)
    while true do
        wait(0)
        
    end
end

function sampev.onShowDialog(dialogId, style, title, button1, button2, text)
    if title:find('{') then
        title = title:gsub('{', '[')
        title = title:gsub('}', ']')
    end
    if text:find('{') then
        text = text:gsub('{', '[')
        text = text:gsub('}', ']')
    end
    if button1:find('{') then
        button1 = button1:gsub('{', '[')
        button1 = button1:gsub('}', ']')
    end
    if button2:find('{') then
        button2 = button2:gsub('{', '[')
        button2 = button2:gsub('}', ']')
    end
    dialog_text = string.format('Dialog ID: %s\nStyle: %s\nTitle: %s\nButton 1: %s\n Button 2: %s\nText: %s',dialogId, style, title, button1, button2, text)
end
Или я что-то не так сделал,или imgui.strcopy не выводит русские символы,что не так?
1693515139002.png
 

triazov

Активный
250
57
Lua:
local imgui = require 'mimgui'
local sampev = require('lib.samp.events')
local encoding = require 'encoding'
local ffi = require 'ffi'
local str = ffi.string
encoding.default = 'CP1251'
u8 = encoding.UTF8

local new = imgui.new
local DialogText = new.char[10000]()
local renderWindow = new.bool(false)
local dialog_text = ''


local newFrame = imgui.OnFrame(
    function() return renderWindow[0] end,
    function(player)
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 600, 450
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2, resY / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Main Window', renderWindow)
        imgui.StrCopy(DialogText, u8:decode(str(dialog_text)))
        imgui.InputTextMultiline(u8"##Многострочный инпут", DialogText, 10000,imgui.ImVec2(600, 250))
       
        imgui.End()
    end
)

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('okno', function()
        renderWindow[0] = not renderWindow[0]
    end)
    while true do
        wait(0)
       
    end
end

function sampev.onShowDialog(dialogId, style, title, button1, button2, text)
    if title:find('{') then
        title = title:gsub('{', '[')
        title = title:gsub('}', ']')
    end
    if text:find('{') then
        text = text:gsub('{', '[')
        text = text:gsub('}', ']')
    end
    if button1:find('{') then
        button1 = button1:gsub('{', '[')
        button1 = button1:gsub('}', ']')
    end
    if button2:find('{') then
        button2 = button2:gsub('{', '[')
        button2 = button2:gsub('}', ']')
    end
    dialog_text = string.format('Dialog ID: %s\nStyle: %s\nTitle: %s\nButton 1: %s\n Button 2: %s\nText: %s',dialogId, style, title, button1, button2, text)
end
Или я что-то не так сделал,или imgui.strcopy не выводит русские символы,что не так?
Посмотреть вложение 213915
Так у тебя в переменной ничего нету
 

Akionka

akionka.lua
Проверенный
742
500
Lua:
local imgui = require 'mimgui'
local sampev = require('lib.samp.events')
local encoding = require 'encoding'
local ffi = require 'ffi'
local str = ffi.string
encoding.default = 'CP1251'
u8 = encoding.UTF8

local new = imgui.new
local DialogText = new.char[10000]()
local renderWindow = new.bool(false)
local dialog_text = ''


local newFrame = imgui.OnFrame(
    function() return renderWindow[0] end,
    function(player)
        local resX, resY = getScreenResolution()
        local sizeX, sizeY = 600, 450
        imgui.SetNextWindowPos(imgui.ImVec2(resX / 2, resY / 2), imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
        imgui.SetNextWindowSize(imgui.ImVec2(sizeX, sizeY), imgui.Cond.FirstUseEver)
        imgui.Begin('Main Window', renderWindow)
        imgui.StrCopy(DialogText, u8:decode(str(dialog_text)))
        imgui.InputTextMultiline(u8"##Многострочный инпут", DialogText, 10000,imgui.ImVec2(600, 250))
       
        imgui.End()
    end
)

function main()
    while not isSampAvailable() do wait(0) end
    sampRegisterChatCommand('okno', function()
        renderWindow[0] = not renderWindow[0]
    end)
    while true do
        wait(0)
       
    end
end

function sampev.onShowDialog(dialogId, style, title, button1, button2, text)
    if title:find('{') then
        title = title:gsub('{', '[')
        title = title:gsub('}', ']')
    end
    if text:find('{') then
        text = text:gsub('{', '[')
        text = text:gsub('}', ']')
    end
    if button1:find('{') then
        button1 = button1:gsub('{', '[')
        button1 = button1:gsub('}', ']')
    end
    if button2:find('{') then
        button2 = button2:gsub('{', '[')
        button2 = button2:gsub('}', ']')
    end
    dialog_text = string.format('Dialog ID: %s\nStyle: %s\nTitle: %s\nButton 1: %s\n Button 2: %s\nText: %s',dialogId, style, title, button1, button2, text)
end
Или я что-то не так сделал,или imgui.strcopy не выводит русские символы,что не так?
Посмотреть вложение 213915
dialog_text = u8(string.format('Dialog ID: %s\nStyle: %s\nTitle: %s\nButton 1: %s\n Button 2: %s\nText: %s',dialogId, style, title, button1, button2, text))
 
  • Нравится
Реакции: хромиус)