Глобальная переменная возвращает nil

Scrix

Известный
Автор темы
232
293
Версия MoonLoader
.027.0-preview
Lua:
function main()
  if not isSampLoaded() or not isSampfuncsLoaded() then return end
  while not isSampAvailable() do wait(100) end
  sampRegisterChatCommand("mycmd", cmd_mycmd)
  while true do wait(0)
    if act == true then
        print(arg)
    end
  end
end
act = false
function cmd_mycmd(arg)
  if #arg == 0 then
    print("Command 'mycmd' syntax: /mycmd [something]")
  else
    print("MyCmd: input = " .. arg)
    act = true
  end
end

Почему-то возвращает nil в main функции
 

Vintik

Мечтатель
Проверенный
1,489
953
Lua:
function main()
  if not isSampLoaded() or not isSampfuncsLoaded() then return end
  while not isSampAvailable() do wait(100) end
  sampRegisterChatCommand("mycmd", cmd_mycmd)
  while true do wait(0)
    if act == true then
        print(arg)
    end
  end
end
act = false
function cmd_mycmd(arg)
  if #arg == 0 then
    print("Command 'mycmd' syntax: /mycmd [something]")
  else
    print("MyCmd: input = " .. arg)
    act = true
  end
end

Почему-то возвращает nil в main функции
Объявить её надо перед main.
 

Vintik

Мечтатель
Проверенный
1,489
953
Lua:
local act = false
local g_arg

function main()
  if not isSampLoaded() or not isSampfuncsLoaded() then return end
  while not isSampAvailable() do wait(100) end
  sampRegisterChatCommand("mycmd", cmd_mycmd)
  while true do wait(0)
    if act then
        print(g_arg)
    end
  end
end

function cmd_mycmd(arg)
  if #arg == 0 then
    print("Command 'mycmd' syntax: /mycmd [something]")
  else
    print("MyCmd: input = " .. arg)
    act = true
    g_arg = arg
  end
end
Да, у тебя arg была определена только в функции cmd_mycmd, ты не можешь её использовать в мэйне.
 
  • Нравится
Реакции: Scrix