SA:MP Пояснения в скрипте

Pampers5

Участник
Автор темы
34
3
Кому не сложно, можете пожалуйста максимально подробно объяснить что за что отвечает каждая строка кода?
вот:
local text = '123456'
local step = text:len() + 1

function main()
  repeat wait(0) until isSampAvailable()
  while true do
    wait(0)
    if wasKeyPressed(220) and not sampIsDialogActive() and not sampIsChatInputActive() and not isSampfuncsConsoleActive() then
      textLength = string.len(text)
      step = 0
    end
    if step <= text:len() then
      sampSetChatInputEnabled(true)
      sampSetChatInputText(text:sub(1, step))
    if textLength == 5 and text ~= ""..text then
      lua_thread.create(function ()
        sampSetChatInputText(""..text)
        wait(100)
        sampProcessChatInput(""..text)
      end)
    end
  end
    if sampIsDialogActive() then
      local id = sampGetCurrentDialogId()
      if (id == 1997 or id == 8869 or id == 8868 or id == 27746) then
        local t = sampGetCurrentDialogEditboxText()
          text = tostring(t)
          step = text:len() + 1
      end
    end
  end
end

function onWindowMessage(msg, wparam, lparam)
  if msg == 0x100 then
      if step < text:len() then
          step = step + 1
      elseif step == text:len() then
          step = step + 1
          sampSetChatInputEnabled(false)
          sampSetChatInputText("")
          sampSendChat(text)
      end
  end
end
 

alexroq

Участник
114
14
Кому не сложно, можете пожалуйста максимально подробно объяснить что за что отвечает каждая строка кода?
вот:
local text = '123456'
local step = text:len() + 1

function main()
  repeat wait(0) until isSampAvailable()
  while true do
    wait(0)
    if wasKeyPressed(220) and not sampIsDialogActive() and not sampIsChatInputActive() and not isSampfuncsConsoleActive() then
      textLength = string.len(text)
      step = 0
    end
    if step <= text:len() then
      sampSetChatInputEnabled(true)
      sampSetChatInputText(text:sub(1, step))
    if textLength == 5 and text ~= ""..text then
      lua_thread.create(function ()
        sampSetChatInputText(""..text)
        wait(100)
        sampProcessChatInput(""..text)
      end)
    end
  end
    if sampIsDialogActive() then
      local id = sampGetCurrentDialogId()
      if (id == 1997 or id == 8869 or id == 8868 or id == 27746) then
        local t = sampGetCurrentDialogEditboxText()
          text = tostring(t)
          step = text:len() + 1
      end
    end
  end
end

function onWindowMessage(msg, wparam, lparam)
  if msg == 0x100 then
      if step < text:len() then
          step = step + 1
      elseif step == text:len() then
          step = step + 1
          sampSetChatInputEnabled(false)
          sampSetChatInputText("")
          sampSendChat(text)
      end
  end
end
1) `local text = '123456'` — переменная с текстом для печати в чат
2) `local step = text:len() + 1` — счётчик позиции символа (изначально 7)
3) (пустая строка)
4) `function main()` — определение главной функции скрипта
5) ` repeat wait(0) until isSampAvailable()` — ждёт загрузки SA-MP
6) ` while true do` — бесконечный основной цикл
7) ` wait(0)` — пауза для уменьшения нагрузки на ЦП
8) ` if wasKeyPressed(220) and not sampIsDialogActive() and not sampIsChatInputActive() and not isSampfuncsConsoleActive() then` — если нажата клавиша 220 (\|, бэкслеш) И не активны диалог/чат/консоль
9) ` textLength = string.len(text)` — сохраняет длину текста
10) ` step = 0` — сбрасывает счётчик для начала печати заново
11) ` end` — конец блока if
12) ` if step <= text:len() then` — если счётчик в пределах длины текста
13) ` sampSetChatInputEnabled(true)` — активирует поле ввода чата
14) ` sampSetChatInputText(text:sub(1, step))` — выводит текст от 1-го до step-го символа
15) ` if textLength == 5 and text ~= ""..text then` — проверка условия (логически странная)
16) ` lua_thread.create(function ()` — создаёт асинхронный поток
17) ` sampSetChatInputText(""..text)` — вставляет полный текст в поле
18) ` wait(100)` — пауза 100мс
19) ` sampProcessChatInput(""..text)` — обрабатывает ввод (имитирует Enter)
20) ` end)` — конец анонимной функции потока
21) ` end` — конец блока if textLength
22) ` end` — конец блока if step <= text:len()
23) ` if sampIsDialogActive() then` — если открыто диалоговое окно
24) ` local id = sampGetCurrentDialogId()` — получает ID диалога
25) ` if (id == 1997 or id == 8869 or id == 8868 or id == 27746) then` — если ID совпадает с одним из указанных
26) ` local t = sampGetCurrentDialogEditboxText()` — получает текст из поля диалога
27) ` text = tostring(t)` — преобразует в строку и сохраняет как новый text
28) ` step = text:len() + 1` — сбрасывает счётчик на новую длину
29) ` end` — конец проверки ID
30) ` end` — конец блока if sampIsDialogActive
31) ` end` — конец while true цикла
32) `end` — конец функции main()
33) (пустая строка)
34) `function onWindowMessage(msg, wparam, lparam)` — функция обработки сообщений окна Windows
35) ` if msg == 0x100 then` — если сообщение = нажатие клавиши (WM_KEYDOWN)
36) ` if step < text:len() then` — если счётчик меньше длины текста
37) ` step = step + 1` — увеличивает счётчик (печатает следующий символ)
38) ` elseif step == text:len() then` — или если счётчик равен длине текста
39) ` step = step + 1` — увеличивает счётчик финально
40) ` sampSetChatInputEnabled(false)` — деактивирует поле ввода
41) ` sampSetChatInputText("")` — очищает поле ввода
42) ` sampSendChat(text)` — отправляет текст в чат (нажимает Enter)
43) ` end` — конец блока elseif
44) ` end` — конец блока if msg == 0x100
45) `end` — конец функции onWindowMessage()
 
  • Вау
  • Нравится
Реакции: Winstаl и Pampers5