• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua How shorten this Lua?

Helliot1

Owner of Empire Online
Joined
Jul 26, 2017
Messages
315
Solutions
1
Reaction score
59
Hello!!

How I can shorten this lua function?

LUA:
function resetHotkeys()
  spellsF1:setImageSource()
  spellsF1:setText('F1')
 
  spellsF2:setImageSource()
  spellsF2:setText('F2')
 
  spellsF3:setImageSource()
  spellsF3:setText('F3')

  spellsF4:setImageSource()
  spellsF4:setText('F4')
 
  spellsF5:setImageSource()
  spellsF5:setText('F5')
 
  spellsF6:setImageSource()
  spellsF6:setText('F6')
 
  spellsF7:setImageSource()
  spellsF7:setText('F7')
 
  spellsF8:setImageSource()
  spellsF8:setText('F8')
 
  spellsF9:setImageSource()
  spellsF9:setText('F9')
 
  spellsF10:setImageSource()
  spellsF10:setText('F10')
 
  spellsF11:setImageSource()
  spellsF11:setText('F11')
 
  spellsF12:setImageSource()
  spellsF12:setText('F12')
end
 
Solution
LUA:
local keys = {spellsF1, spellsF2, spellsF3, spellsF4, spellsF5, spellsF6, spellsF7, spellsF8, spellsF9, spellsF10, spellsF11, spellsF12}

function resetHotkeys()
	for i = 1, #keys do
		local spellKey = keys[i]
		spellKey:setImageSource()
		spellKey:setText(("F%d"):format(i))
	end	
end
LUA:
local keys = {spellsF1, spellsF2, spellsF3, spellsF4, spellsF5, spellsF6, spellsF7, spellsF8, spellsF9, spellsF10, spellsF11, spellsF12}

function resetHotkeys()
	for i = 1, #keys do
		local spellKey = keys[i]
		spellKey:setImageSource()
		spellKey:setText(("F%d"):format(i))
	end	
end
 
Solution
LUA:
function resetHotkeys()
    for i = 1, 12 do
        local spellsF = loadstring("spellsF"..i)
        spellsF:setImageSource()
        spellsF:setText('F'..i)
    end
end

If i'm not mistaken you can use loadstring to load a variable with a number on it, but the best approach would be to turn those spellsF.. objects into a table spellsF
 
Thank you guys!!! Helped me a lot.

But loadstring didnt work.

EDIT: I tried it on another function, but why spellsValue didn't work?

LUA:
local keys = {F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12}

for i = 1, #keys do
local spellsValue = "spells" .. keys[i]
    if onHotkeyClicada == spellsValue then
        onHotkeyClicada:setImageSource(iconeSelecionado)
        onHotkeyClicada:setText('')
      
        lastHotkeys[char] = {
          spellsValue = iconeSelecionado }
      
        hotkeys[char].spellsValue = {
          autoSend = "true",
          value = nomeSelecionado }
    end
 
Last edited:
Thank you guys!!! Helped me a lot.

But loadstring didnt work.

EDIT: I tried it on another function, but why spellsValue didn't work?

LUA:
local keys = {F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12}

for i = 1, #keys do
local spellsValue = "spells" .. keys[i]
    if onHotkeyClicada == spellsValue then
        onHotkeyClicada:setImageSource(iconeSelecionado)
        onHotkeyClicada:setText('')
   
        lastHotkeys[char] = {
          spellsValue = iconeSelecionado }
   
        hotkeys[char].spellsValue = {
          autoSend = "true",
          value = nomeSelecionado }
    end
Without Full code we cant say...
What is onHotkeyClicada? IS a string? spellValue is a string, be sure compare it with another string...(line 5)
 
Without Full code we cant say...
What is onHotkeyClicada? IS a string? spellValue is a string, be sure compare it with another string...(line 5)

Sorry :(

onHotkeyClicada is the last hotkey clicked, like spellsF1 or until spellsF12
 
Thank you guys!!! Helped me a lot.

But loadstring didnt work.

EDIT: I tried it on another function, but why spellsValue didn't work?

LUA:
local keys = {F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12}

for i = 1, #keys do
local spellsValue = "spells" .. keys[i]
    if onHotkeyClicada == spellsValue then
        onHotkeyClicada:setImageSource(iconeSelecionado)
        onHotkeyClicada:setText('')
     
        lastHotkeys[char] = {
          spellsValue = iconeSelecionado }
     
        hotkeys[char].spellsValue = {
          autoSend = "true",
          value = nomeSelecionado }
    end
This local spellsValue = "spells" .. keys[i] is what is wrong. Something for you to think about.
 
Back
Top