Thread Tools Display Modes
02/13/17, 10:50 PM   #1
BrizonTG
Join Date: Feb 2017
Posts: 2
'wait()' for Elder Scrolls Online?

How would I prevent a while loop from crashing the client?
  Reply With Quote
02/13/17, 11:04 PM   #2
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
I broke my gigantic loops into smaller pieces and then called them at intervals like this:

EVENT_MANAGER:RegisterForUpdate(addon.name, milliseconds, function() loop() end)
  Reply With Quote
02/13/17, 11:32 PM   #3
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019



You get an (infinite) loop : You break the loop.

99.9% of loops crashing the client are bad codes and can be avoided by refactoring it, no need wait(), pause() or other sleep() you may know.
  Reply With Quote
02/14/17, 12:37 AM   #4
votan
 
votan's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2014
Posts: 577
Originally Posted by Ayantir View Post
You get an (infinite) loop : You break the loop.

99.9% of loops crashing the client are bad codes and can be avoided by refactoring it, no need wait(), pause() or other sleep() you may know.
Infinite loop just fixes the client. Crashing is done by for example creating/changing too many strings within one frame. The x86 client does not crash for that. And the keyword is not wait, but yield.
And he is not talking about infinite loop, but huge loop.
  Reply With Quote
02/14/17, 12:57 AM   #5
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
ESO can at least handle a loop of 500,000 iterations without being noticed by users if there's no output.. I know it, I did it.

I'd like to see the kind of loop requiring a pause in order to do this without the complaints of users having so many addons and after months of investigation by the author..

Here we have :

Originally Posted by BrizonTG
How would I prevent a while loop from crashing the client?
No example, no code snippet, no addon mentionned and a user with a post count of #1.

It can be nothing and anything, and for me the best answer to "how to prevent a while loop from crashing the client" asked by a newbie is "BETTER CODING" , not

Lua Code:
  1. EVENT_MANAGER:RegisterForUpdate(ADDON_NAME, 1, zo_callLater(control:SetHandler("OnUpdate", function() DoSomethingWeird() end, 1)))

It's disclose bad practices.

It's like when your grand'ma ask how to change a registry setting when she just bought Win10. The answer should be adapted. So Yes, the first answer can be accepted, but it's not (for my point of view) the best to give.


Infinite loop just fixes the client
If Lua become unresponsive, C++ crash.



control:RegisterForUpdate and zo_callLater can be interesting but in the long list of things a addon author should use when writing their first addon .. they comes at the very end.

Last edited by Ayantir : 02/14/17 at 12:59 AM.
  Reply With Quote
02/14/17, 02:32 AM   #6
Dolgubon
 
Dolgubon's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2016
Posts: 408
In some cases when I have an infinite loop (somehow) that shouldn't be there I"ll add a counter and break it if the counter is too high. That's just for debugging purposes though, and once I figure it out I remove it.
  Reply With Quote
02/14/17, 07:34 PM   #7
BrizonTG
Join Date: Feb 2017
Posts: 2
Lua Code:
  1. -- First addon, just mess around and figuring out the API.
  2.  
  3. -- NOTICE: PLEASE SEE 'README.TXT' FOR INFORMATION REGARDING THIS ADDON, COMPLIANT TO
  4. -- [url]https://account.elderscrollsonline.com/add-on-terms[/url]
  5.  
  6. local Addon =
  7. {
  8.     name,
  9.     inCombat,  
  10. };
  11.  
  12. Addon.name = "test"
  13.  
  14. -----------------------------------------------------------------------------
  15. -- Boilerplate Lua...
  16. -- Here's  our hack to get the 'connect line' back...
  17.  
  18. local AddonManager = {};
  19. function AddonManager:connect(f)
  20.     EVENT_MANAGER:RegisterForEvent(Addon.name, EVENT_ADD_ON_LOADED, f);
  21. end
  22.  
  23. function AddonManager:disconnect()
  24.     EVENT_MANAGER:UnregisterForEvent(Addon.name, EVENT_ADD_ON_LOADED);
  25. end
  26.  
  27. local CombatManager = {};
  28. function CombatManager:connect(f)
  29.     EVENT_MANAGER:RegisterForEvent(Addon.name, EVENT_PLAYER_COMBAT_STATE, f);
  30. end
  31.  
  32. function warn(string)
  33.     d(string);
  34. end
  35.  
  36. function wait(time)
  37.     local init = os.clock() / 100;
  38.     repeat until (os.clock() / 100) > init + time;
  39. end
  40.  
  41. local clock = os.clock
  42. function sleep(n)  -- seconds
  43.   local t0 = clock();
  44.   while clock() - t0 <= n do end --os.clock isn't available, and this crashes anyway if we use GetTimeFromMilliseconds()
  45. end
  46.  
  47. -----------------------------------------------------------------------------
  48. local timer = 0;
  49. local tldgui;   -- Make us our thing...
  50. local lblcntr;
  51.  
  52. function OnPlayerCombatState(event, inCombat)
  53.     if (inCombat ~= Addon.inCombat)
  54.     then
  55.         Addon.inCombat = inCombat;
  56.         tldgui:SetHidden(not inCombat);
  57.     end
  58.     if (inCombat)
  59.         then
  60.             warn("Entering Combat...");
  61.             while (inCombat) do
  62.                 timer = timer + 1;
  63.                 --if (timer > 0)
  64.                 --then
  65.                     lblCounter:SetText("Counter: " .. timer);
  66.                 --else
  67.                 --  lblCounter:SetText("Entering Combat!");
  68.                 --end
  69.                 --wait(1); --???? Why is this invalid?
  70.                 --sleep();
  71.             end
  72.         else
  73.             warn("Leaving Combat...");
  74.             timer = 0;
  75.     end
  76. end
  77.  
  78. -- Borrowed from the tutorial, hate XML, rather do this.
  79. function Addon:CreateGui()
  80.     tldgui = GetWindowManager():CreateTopLevelWindow("tldgui");
  81.     tldgui:SetDimensions(200, 100);
  82.     tldgui:SetResizeToFitDescendents(true);
  83.     tldgui:SetAnchor(CENTER, GuiRoot, CENTER, 0,0);
  84.     tldgui:SetMovable(true);
  85.     tldgui:SetMouseEnabled(true);
  86.    
  87.     local bdBackdrop = GetWindowManager():CreateControl("bdBackDrop", tldgui, CT_BACKDROP);
  88.     bdBackDrop:SetEdgeColor(0.4,0.4,0.4);
  89.     bdBackDrop:SetCenterColor(0.1,0.1,0.1);
  90.     bdBackDrop:SetAnchor(TOPLEFT, tldgui, TOPLEFT, 0, 0);
  91.     bdBackDrop:SetDimensions(200,100);
  92.     bdBackDrop:SetAlpha(1);
  93.     bdBackDrop:SetDrawLayer(0);
  94.    
  95.     lblCounter = GetWindowManager():CreateControl("lblCounter", tldgui, CT_LABEL)
  96.     lblCounter:SetColor(0.8, 0.8, 0.8, 1);
  97.     lblCounter:SetFont("ZoFontAlert");
  98.     lblCounter:SetScale(1);
  99.     lblCounter:SetWrapMode(TEX_MODE_CLAMP);
  100.     lblCounter:SetDrawLayer(1);
  101.     lblCounter:SetText("Click Counts: 0");
  102.     lblCounter:SetAnchor(CENTER, GuiRoot, CENTER, 0,0);
  103.     lblCounter:SetDimensions(100,25);
  104. end
  105.  
  106. function Addon:Init()
  107.     Addon:CreateGui();
  108.     tldgui:SetHidden(true);
  109.     Addon.inCombat = IsUnitInCombat("player");
  110.     CombatManager:connect(OnPlayerCombatState);
  111.     AddonManager:disconnect();
  112. end
  113.  
  114. AddonManager:connect(Addon.Init);







There's a lot more problems than just wait() here, and I'm still figuring out the differences between ESO's Lua and the implementation of Lua I know.

Last edited by BrizonTG : 02/15/17 at 01:34 PM.
  Reply With Quote
02/14/17, 08:18 PM   #8
Rhyono
AddOn Author - Click to view addons
Join Date: Sep 2016
Posts: 659
Because there are events for entering and exiting combat. So trying to tie up the thread with sleeping is a bad idea.
  Reply With Quote
02/14/17, 08:30 PM   #9
Ayantir
 
Ayantir's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 1,019
there is no os library, and please use the BBCode to help .. you don't need to end your lines with ";" .. creating GUI should be done in XML not lua

When you don't know how to do.. a good thing is to look at others does.
  Reply With Quote
02/15/17, 02:44 AM   #10
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,566
Hi BrizonTG.
Did you read the getting started guide or followed any of the tutorials we have on our wiki? They should clear up some of your questions.

As for differences between ESO Lua and "normal" Lua, ESO uses a variation of Lua5.1 with only math, string and table + global functions + zos api available.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » 'wait()' for Elder Scrolls Online?

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off