Перейти к содержанию

Как создать функцию, что бы оптимизировать код?


Рекомендуемые сообщения

Как создать функцию, что бы оптимизировать код? Нужно написать следующий код 20 раз.

Для примера:

Спойлер

endata_DextLuck = {} 
for x = 1, 12 do 
  endata_DextLuck[x] = UDF1["CEEdit"..x+532] 
end 

--write 1 byte 
for x = 0, 11 do 
  local offset = 104*(x // 2) + (x % 2) 
  local current_endata_DextLuck = endata_DextLuck[x+1] 

  endata_DextLuck[x+1].OnKeyPress = function(sender, key) 
      timer.Enabled = false 
      local a = tonumber(sender.Text) 
      if (a~=nil) and isKeyPressed(VK_RETURN) then 
        writeBytes(0x00A445C8+offset, current_endata_DextLuck.Text) 
        timer.Enabled = true 
      end 
      return key 
    end 
end

 

и другой такой же код

 

Спойлер

endata_AttMatt = {} 
for x = 1, 14 do 
  endata_AttMatt[x] = UDF1["CEEdit"..x+512] 
end 

--write 1 byte 
for x = 0, 13 do 
  local offset = 68*(x // 2) + (x % 2) 
  local current_endata_AttMatt = endata_AttMatt[x+1] 

  endata_AttMatt[x+1].OnKeyPress = function(sender, key) 
      timer.Enabled = false 
      local a = tonumber(sender.Text) 
      if (a~=nil) and isKeyPressed(VK_RETURN) then 
        writeBytes(0x00A44D30+offset, current_endata_AttMatt.Text) 
        timer.Enabled = true 
      end 
      return key 
    end 
end

 

 

Только два параметра изменяются, адрес (0x00A445C8 => 0x00A44D30) и имя таблицы (endata_DextLuck => endata_AttMatt).

Что нужно сделать, что бы избежать написание кода 20 раз? Может нужно создать функцию с передаваемыми параметрами?

Например вот так:

Спойлер

--создаем таблицу
cacheTable_CEEdits = {}
for x = 1, 135 do
  cacheTable_CEEdits[x] = UDF1['CEEdit'..x]
end

--создаем функцию с параметрами
function write1byte(tablename, address, x) -- или  local function write1byte(tablename, address, x)

  local current = tablename[x]

  tablename[x].OnKeyPress = function(sender, key)
      timer.Enabled = false
      local a = tonumber(sender.Text)
      if (a~=nil) and isKeyPressed(VK_RETURN) then
        writeBytes(address + offset, current.Text)
        timer.Enabled = true
      end
      return key
    end
end

--вызов функции
for x = 1, 135 do
  local offset = 0x84*((x-1) // 15) + (((x-1) % 15) + 1)
  write1byte(cacheTable_CEEdits, 0x009E8624, x)
end

 

 

Но функция записи не работает. Где ошибка?

 

Уже работает функция записи. Ответ на собственный вопрос:

Спойлер

local function write1byte(tablename, address)

  local current = tablename

  tablename.OnKeyPress = function(sender, key)
      timer.Enabled = false
      local a = tonumber(sender.Text)
      if (a~=nil) and isKeyPressed(VK_RETURN) then
        writeBytes(address, current.Text)
        timer.Enabled = true
      end
      return key
    end
end


for x = 1, 135 do
  local offset = 0x84*((x-1) // 15) + (((x-1) % 15) + 1)
  write1byte(cacheTable_CEEdits[x], 0x009E8624+offset)
end

 

 

Изменено пользователем Razi
Ссылка на комментарий
Поделиться на другие сайты

Принцип: SOLID, KISS, DRY

1) через OOP в Lua

2) через вложенные функции в Lua

3) через "код пишет код"

Спойлер

function calculateString (str)
    return  assert(loadstring( "return "  .. str))()
end 
Ссылка на комментарий
Поделиться на другие сайты

×
×
  • Создать...

Важная информация

Находясь на нашем сайте, Вы автоматически соглашаетесь соблюдать наши Условия использования.