ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   Lua/XML Help (https://www.esoui.com/forums/forumdisplay.php?f=175)
-   -   Help with toggle value/function (https://www.esoui.com/forums/showthread.php?t=902)

Etupa 04/14/14 04:24 PM

Help with toggle value/function
 
Hi here, need your help I've messed up with else if or etc make 0 1 as a var... don't get it at all ;_;

I'm trying to make a toggle function with one keybinds, bindings.xml part is ok for me but have no more idea for lua... u_u

Basicaly, I want 0 became 1 or 1 became 0 ... ^^;

Lua Code:
  1. local function helm()  
  2.         local control = ZO_OptionsWindow.controlTable[3][8]
  3.         SetSetting(control.system, control.settingId, 0)   
  4.     end
  5. --or
  6.  
  7.     local function nohelm()
  8.         local control = ZO_OptionsWindow.controlTable[3][8]
  9.         SetSetting(control.system, control.settingId, 1)   
  10.     end

Thanks for your help :c




Edit :
where I am now ... ; ;


Lua Code:
  1. function toghelm()
  2. local control = ZO_OptionsWindow.controlTable[3][8]
  3. SetSetting(control.system, control.settingId, (var))
  4.     var = x
  5.         if x == 0 then return nohelm()
  6.         elseif x == 1 then return helm()
  7.     end
  8.  
  9. end
  10.            
  11.     local function helm()  
  12.         local control = ZO_OptionsWindow.controlTable[3][8]
  13.         SetSetting(control.system, control.settingId, 0)   
  14.     end
  15.     local function nohelm()
  16.         local control = ZO_OptionsWindow.controlTable[3][8]
  17.         SetSetting(control.system, control.settingId, 1)   
  18.     end

tried also something like

Lua Code:
  1. function toghelm()
  2. local control = ZO_OptionsWindow.controlTable[3][8]
  3. SetSetting(control.system, control.settingId, (var))
  4.    var = helmvar
  5.       if helmvar == 1 then var = 0
  6.       elseif helmvar == 0 then var = 1
  7.    end
  8. end

Well, those always return 0 (helm is on) ... but can't catch the way to make 0 to 1 et 1 to 0 at function execution ^^;


one more try...

Lua Code:
  1. function toghelm()
  2.     local control = ZO_OptionsWindow.controlTable[3][8]
  3.         local helmstate = GetSetting(control.system, control.settingId)
  4.         if helmstate == 1 then return helm()
  5.         elseif helmstate == 0 then return nohelm()  
  6.     end
  7. end
  8.  
  9.            
  10. local function helm()  
  11.     local control = ZO_OptionsWindow.controlTable[3][8]
  12.     SetSetting(control.system, control.settingId, 0)   
  13. end
  14.  
  15. local function nohelm()
  16.     local control = ZO_OptionsWindow.controlTable[3][8]
  17.     SetSetting(control.system, control.settingId, 1)   
  18. end

really don't get it :D

Etupa 04/15/14 07:43 AM

Gonna test this, but server are down >.<

Lua Code:
  1. function toghelm()                         
  2. local control = ZO_OptionsWindow.controlTable[3][8]
  3. local var = GetSetting(control.system, control.settingId)                                  
  4.         if var == 1
  5.             then var = 0
  6.         else
  7.             var = 1
  8.     end                                    
  9. SetSetting(control.system, control.settingId,(var))
  10. end

Stormknight 04/15/14 08:29 AM

The simplest way to toggle a variable between zero and one is as follows:

Code:

var = 1 - var
You can do it using xor as well, but I don't believe that's fully implemented in the version of LUA that's embedded within ESO.

So your function could look as follows:

Code:

function toghelm()                         
    local control = ZO_OptionsWindow.controlTable[3][8]
    local var = GetSetting(control.system, control.settingId)                                 
    SetSetting(control.system, control.settingId,(1-var))
end

Hope that helps! :)

Etupa 04/15/14 11:26 AM

Quote:

Originally Posted by Stormknight (Post 4428)
...

I will try this too as soon as server is up ! seems right to me :D

Thanks a lot Stromknight !

Iyanga 04/15/14 01:00 PM

At least one code would produce an exception:"function expected but got nil" if used the way it is pasted here. So, when you paste code, it is important to paste it correctly and not modify it in between. The order of declarations etc. are important in LUA, unlike Python or JavaScript.


Your first snippet:
Lua Code:
  1. function toghelm()
  2. local control = ZO_OptionsWindow.controlTable[3][8]
  3. SetSetting(control.system, control.settingId, (var))
  4.     var = x
  5.         if x == 0 then return nohelm()
  6.         elseif x == 1 then return helm()
  7.     end
  8.  
  9. end


There is no x. If there is no x, it is nil. If x is nil, then the comparisons are "nil == 0" which is false and "nil == 1" which is also false. Neither helm() nor norhelm() gets executed.

Garkin 04/15/14 03:37 PM

Quote:

Originally Posted by Iyanga (Post 4491)
Your first snippet:
Lua Code:
  1. function toghelm()
  2. local control = ZO_OptionsWindow.controlTable[3][8]
  3. SetSetting(control.system, control.settingId, (var))
  4.     var = x
  5.         if x == 0 then return nohelm()
  6.         elseif x == 1 then return helm()
  7.     end
  8.  
  9. end


There is no x. If there is no x, it is nil. If x is nil, then the comparisons are "nil == 0" which is false and "nil == 1" which is also false. Neither helm() nor norhelm() gets executed.

I guess there should be x = var instead of var = x. :)


All times are GMT -6. The time now is 12:02 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2014 - 2022 MMOUI