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

PanSeek

t.me/dailypanseek
Всефорумный модератор
899
1,745
Братан, если мож, сделай код который я скидывал, я прост такой думал будет) мне в таком легче будет работать.
/code
script_name('FamilyHelper v 1.0')
script_author('Tsunami_Nakamura')
script_description('FamilyHelper v 1.0')
script_version('1.0')

require "lib.moonloader"
local imgui = require 'imgui'
local key = require 'vkeys'

local act = 0

local main_window_state = imgui.ImBool(false)
local checkbox = imgui.ImBool(false)

function imgui.OnDrawFrame()
local iScreenWidth, iScreenHeight = getScreenResolution()
local btn_size = imgui.ImVec2(-1, 0)
if main_window_state.v then
imgui.SetNextWindowSize(imgui.ImVec2(1000, 500), imgui.Cond.FirstUseEver)
imgui.Begin('Family Helper by Nakamura', main_window_state, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoCollapse)
imgui.BeginChild('##main menu', imgui.ImVec2(120, 460), true)
if imgui.Button('Основное меню', imgui.ImVec2(172.5,40)) then act = 0 end
if imgui.Button('2 tab') then act = 1 end
imgui.EndChild()
imgui.SameLine()
if act == 0 then
imgui.BeginChild('##once', imgui.ImVec2(730, 460), true)
imgui.Text('This is once tab')
imgui.EndChild()
elseif act == 1 then
imgui.BeginChild('##twice', imgui.ImVec2(730, 460), true)
imgui.Text('This is twice tab')
imgui.EndChild()
end
imgui.SameLine()
imgui.BeginChild('##tabs2', imgui.ImVec2(120, 460), true)
if imgui.Button('1 tab') then act = 0 end
if imgui.Button('2 tab') then act = 1 end
imgui.EndChild()
end
imgui.End()
end


function main()
if not isSampfuncsLoaded() or not isSampLoaded() then return end
while not isSampAvailable() do wait(2000) end

sampAddChatMessage('[FamHelper] ????? ???????: Tsunami_Nakamura. ????????: Adam_Karleone [ARIZONA 10]', 0x00BFFF)
sampAddChatMessage('[FamHelper] ???? ???????? ?? - lcn.maks', 0x00BFFF)
sampAddChatMessage('[FamHelper] ????????? ???????: /famh', 0x00BFFF)
sampAddChatMessage('[FamHelper] ?????? ?? ?????????? ?????? ? ??', 0x00BFFF)
sampRegisterChatCommand('famh', function() main_window_state.v = not main_window_state.v end)

while true do
wait(0)

imgui.Process = main_window_state.v and true or false

if checkbox.v then
printStringNow('test', 1000)
end
end
end
Так? В плане сделать код? Я же беру твой.
Lua:
script_name('FamilyHelper v 1.0')
script_author('Tsunami_Nakamura')
script_description('FamilyHelper v 1.0')
script_version('1.0')

require "lib.moonloader"
local imgui = require 'imgui'
local key = require 'vkeys'

local act = 0

local main_window_state = imgui.ImBool(false)
local checkbox = imgui.ImBool(false)

function imgui.OnDrawFrame()
    local iScreenWidth, iScreenHeight = getScreenResolution()
    local btn_size = imgui.ImVec2(-1, 0)
    if main_window_state.v then
        imgui.SetNextWindowSize(imgui.ImVec2(1000, 500), imgui.Cond.FirstUseEver)
        imgui.Begin('Family Helper by Nakamura', main_window_state, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoCollapse)
        imgui.BeginChild('##main menu', imgui.ImVec2(120, 460), true)
        if imgui.Button('Основное меню', imgui.ImVec2(-1, 40)) then act = 0 end
        if imgui.Button('2 tab') then act = 1 end
        imgui.EndChild()
        imgui.SameLine()
        if act == 0 then
            imgui.BeginChild('##once', imgui.ImVec2(730, 460), true)
            imgui.Text('This is once tab')
            imgui.EndChild()
        elseif act == 1 then
            imgui.BeginChild('##twice', imgui.ImVec2(730, 460), true)
            imgui.Text('This is twice tab')
            imgui.EndChild()
        end
        imgui.SameLine()
        imgui.BeginChild('##tabs2', imgui.ImVec2(120, 460), true)
        if imgui.Button('1 tab') then act = 0 end
        if imgui.Button('2 tab') then act = 1 end
        imgui.EndChild()
    end
    imgui.End()
end


function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    while not isSampAvailable() do wait(2000) end

    sampAddChatMessage('[FamHelper] ????? ???????: Tsunami_Nakamura. ????????: Adam_Karleone [ARIZONA 10]', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ???? ???????? ?? - lcn.maks', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ????????? ???????: /famh', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ?????? ?? ?????????? ?????? ? ??', 0x00BFFF)
    sampRegisterChatCommand('famh', function() main_window_state.v = not main_window_state.v end)

    while true do
        wait(0)
        imgui.Process = main_window_state.v and true or false

        if checkbox.v then
            printStringNow('test', 1000)
        end
    end
end
2021-03-14 18-22-21-149.png
Ты указал слишком большой размер кнопки, что она должна рисоваться еще правее, а текст по середине из-за этого текст улетел.
 

tsunamiqq

Участник
428
16
Так? В плане сделать код? Я же беру твой.
Lua:
script_name('FamilyHelper v 1.0')
script_author('Tsunami_Nakamura')
script_description('FamilyHelper v 1.0')
script_version('1.0')

require "lib.moonloader"
local imgui = require 'imgui'
local key = require 'vkeys'

local act = 0

local main_window_state = imgui.ImBool(false)
local checkbox = imgui.ImBool(false)

function imgui.OnDrawFrame()
    local iScreenWidth, iScreenHeight = getScreenResolution()
    local btn_size = imgui.ImVec2(-1, 0)
    if main_window_state.v then
        imgui.SetNextWindowSize(imgui.ImVec2(1000, 500), imgui.Cond.FirstUseEver)
        imgui.Begin('Family Helper by Nakamura', main_window_state, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoCollapse)
        imgui.BeginChild('##main menu', imgui.ImVec2(120, 460), true)
        if imgui.Button('Основное меню', imgui.ImVec2(-1, 40)) then act = 0 end
        if imgui.Button('2 tab') then act = 1 end
        imgui.EndChild()
        imgui.SameLine()
        if act == 0 then
            imgui.BeginChild('##once', imgui.ImVec2(730, 460), true)
            imgui.Text('This is once tab')
            imgui.EndChild()
        elseif act == 1 then
            imgui.BeginChild('##twice', imgui.ImVec2(730, 460), true)
            imgui.Text('This is twice tab')
            imgui.EndChild()
        end
        imgui.SameLine()
        imgui.BeginChild('##tabs2', imgui.ImVec2(120, 460), true)
        if imgui.Button('1 tab') then act = 0 end
        if imgui.Button('2 tab') then act = 1 end
        imgui.EndChild()
    end
    imgui.End()
end


function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    while not isSampAvailable() do wait(2000) end

    sampAddChatMessage('[FamHelper] ????? ???????: Tsunami_Nakamura. ????????: Adam_Karleone [ARIZONA 10]', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ???? ???????? ?? - lcn.maks', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ????????? ???????: /famh', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ?????? ?? ?????????? ?????? ? ??', 0x00BFFF)
    sampRegisterChatCommand('famh', function() main_window_state.v = not main_window_state.v end)

    while true do
        wait(0)
        imgui.Process = main_window_state.v and true or false

        if checkbox.v then
            printStringNow('test', 1000)
        end
    end
end
Ты указал слишком большой размер кнопки, что она должна рисоваться еще правее, а текст по середине из-за этого текст улетел.
А пон, сяб
 

BRaze

Участник
84
0
Как можно закрасить время слева от сообщения в тот же цвет, с которого начинается само сообщение?

Скриншот 14-03-2021 173806.png
 

Smeruxa

Известный
1,297
681
Lua:
 local pos = {getCharCoordinates(v)}
                            local pos_1 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] - 1)}
                            local pos_2 = {convert3DCoordsToScreen(pos[1], pos[2] + 0.3, pos[3] - 1)}
                            renderDrawLine(pos_2[1], pos_2[2], pos_1[1], pos_1[2], elements.sliders.widthBox.v, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                            local pos_3 = {convert3DCoordsToScreen(pos[1], pos[2] + 0.3, pos[3] + 1)}
                            renderDrawLine(pos_2[1], pos_2[2], pos_3[1], pos_3[2], elements.sliders.widthBox.v, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                            local pos_4 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] + 1)}
                            renderDrawLine(pos_3[1], pos_3[2], pos_4[1], pos_4[2], elements.sliders.widthBox.v, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                            local pos_5 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] - 1)}
                            renderDrawLine(pos_4[1], pos_4[2], pos_5[1], pos_5[2], elements.sliders.widthBox.v, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
 

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,771
11,214
Lua:
function pedBox2d() --я рот ваших 2д боксов долбил
    lua_thread.create(function()
        for k,v in ipairs(getAllChars()) do
            if isCharOnScreen(v) then       
                size = ch_wh_slider_boxsize.v
                size_vertical = 0.7
                thickness = ch_wh_slider_boxthickness.v
                local pos = {getCharCoordinates(v)}
                local pos_1 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] - 1)}
                local pos_2 = {convert3DCoordsToScreen(pos[1], pos[2] + 0.3, pos[3] - 1)}
                renderDrawLine(pos_2[1], pos_2[2], pos_1[1], pos_1[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                local pos_3 = {convert3DCoordsToScreen(pos[1], pos[2] + 0.3, pos[3] + 1)}
                renderDrawLine(pos_2[1], pos_2[2], pos_3[1], pos_3[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                local pos_4 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] + 1)}
                renderDrawLine(pos_3[1], pos_3[2], pos_4[1], pos_4[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                local pos_5 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] - 1)}
                renderDrawLine(pos_4[1], pos_4[2], pos_5[1], pos_5[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
              
            end
        end
    end)
 

Fomikus

Известный
Проверенный
472
341
Lua:
function pedBox2d() --я рот ваших 2д боксов долбил
    lua_thread.create(function()
        for k,v in ipairs(getAllChars()) do
            if isCharOnScreen(v) then    
                size = ch_wh_slider_boxsize.v
                size_vertical = 0.7
                thickness = ch_wh_slider_boxthickness.v
                local pos = {getCharCoordinates(v)}
                local pos_1 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] - 1)}
                local pos_2 = {convert3DCoordsToScreen(pos[1], pos[2] + 0.3, pos[3] - 1)}
                renderDrawLine(pos_2[1], pos_2[2], pos_1[1], pos_1[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                local pos_3 = {convert3DCoordsToScreen(pos[1], pos[2] + 0.3, pos[3] + 1)}
                renderDrawLine(pos_2[1], pos_2[2], pos_3[1], pos_3[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                local pos_4 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] + 1)}
                renderDrawLine(pos_3[1], pos_3[2], pos_4[1], pos_4[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
                local pos_5 = {convert3DCoordsToScreen(pos[1], pos[2] - 0.3, pos[3] - 1)}
                renderDrawLine(pos_4[1], pos_4[2], pos_5[1], pos_5[2], thickness, sampGetPlayerColor(select(2, sampGetPlayerIdByCharHandle(v))))
           
            end
        end
    end)
1615738369849.png

Вообщем примерно то что я имею ввиду, дальше через синусы/косинусы

Еще можно узнать разницу по Y на экране (Голова минус ноги) и получить некий множитель и через него при рендере делать
Аля 1/2 точка = head.x - 5 * (mnoj / 10), head.y (Для второй точки там плюс)
Аля 3/4 точка = foot.x - 5 * (mnoj / 10), foot.y
Более точный через картинку выше скину в сниппеты/сюда через неопределенное время

p.s если скинешь в лс код для 3д боксов - мб будет быстрее
 

tsunamiqq

Участник
428
16
Смотри
Так? В плане сделать код? Я же беру твой.
Lua:
script_name('FamilyHelper v 1.0')
script_author('Tsunami_Nakamura')
script_description('FamilyHelper v 1.0')
script_version('1.0')

require "lib.moonloader"
local imgui = require 'imgui'
local key = require 'vkeys'

local act = 0

local main_window_state = imgui.ImBool(false)
local checkbox = imgui.ImBool(false)

function imgui.OnDrawFrame()
    local iScreenWidth, iScreenHeight = getScreenResolution()
    local btn_size = imgui.ImVec2(-1, 0)
    if main_window_state.v then
        imgui.SetNextWindowSize(imgui.ImVec2(1000, 500), imgui.Cond.FirstUseEver)
        imgui.Begin('Family Helper by Nakamura', main_window_state, imgui.WindowFlags.NoResize + imgui.WindowFlags.NoCollapse)
        imgui.BeginChild('##main menu', imgui.ImVec2(120, 460), true)
        if imgui.Button('Основное меню', imgui.ImVec2(-1, 40)) then act = 0 end
        if imgui.Button('2 tab') then act = 1 end
        imgui.EndChild()
        imgui.SameLine()
        if act == 0 then
            imgui.BeginChild('##once', imgui.ImVec2(730, 460), true)
            imgui.Text('This is once tab')
            imgui.EndChild()
        elseif act == 1 then
            imgui.BeginChild('##twice', imgui.ImVec2(730, 460), true)
            imgui.Text('This is twice tab')
            imgui.EndChild()
        end
        imgui.SameLine()
        imgui.BeginChild('##tabs2', imgui.ImVec2(120, 460), true)
        if imgui.Button('1 tab') then act = 0 end
        if imgui.Button('2 tab') then act = 1 end
        imgui.EndChild()
    end
    imgui.End()
end


function main()
    if not isSampfuncsLoaded() or not isSampLoaded() then return end
    while not isSampAvailable() do wait(2000) end

    sampAddChatMessage('[FamHelper] ????? ???????: Tsunami_Nakamura. ????????: Adam_Karleone [ARIZONA 10]', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ???? ???????? ?? - lcn.maks', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ????????? ???????: /famh', 0x00BFFF)
    sampAddChatMessage('[FamHelper] ?????? ?? ?????????? ?????? ? ??', 0x00BFFF)
    sampRegisterChatCommand('famh', function() main_window_state.v = not main_window_state.v end)

    while true do
        wait(0)
        imgui.Process = main_window_state.v and true or false

        if checkbox.v then
            printStringNow('test', 1000)
        end
    end
end
Ты указал слишком большой размер кнопки, что она должна рисоваться еще правее, а текст по середине из-за этого текст улетел.
Смотри, а как сделать перенос текста. Вот допустим в одной кнопке. С верху написано "Управление" с низу "Участниками" как так можно сделать? прост друг не отвечает, у тебя спрошу.
 

PanSeek

t.me/dailypanseek
Всефорумный модератор
899
1,745
Смотри

Смотри, а как сделать перенос текста. Вот допустим в одной кнопке. С верху написано "Управление" с низу "Участниками" как так можно сделать? прост друг не отвечает, у тебя спрошу.
\n
Например:
Lua:
imgui.Button('Управление\nУчастниками')