WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   AddOn Help/Support (https://www.wowinterface.com/forums/forumdisplay.php?f=3)
-   -   Loop freezes game on load (https://www.wowinterface.com/forums/showthread.php?t=57123)

MoreUmph 04-30-19 05:41 PM

Loop freezes game on load
 
Im trying to make a simple addon that switches the action bar if a player has certain spells learned. Dont ask

Without a loop, it works fine, upon reloading the bar switches successfully, but with the loop the game
becomes unresponsive upon load

I look up the 'OnUpdate' event but it says I need a frame to update and bunch of other things, the code I've tried doesnt work and is just too excessive for my needs

Would appreciate a point in the right direction


Code:

local looping = true, a, b, c, d
while looping do
  a = IsSpellKnown(72, false)
  b = IsSpellKnown(73, false)
  c = IsSpellKnown(74, false)
  d = IsSpellKnown(75, false)

  if a and b and c and d then
    ChangeActionBarPage(2)
    looping = false
  end
end


Terenna 04-30-19 06:04 PM

Any time a, b, c, or d is not true you'll enter an infinite loop causing the crash

MoreUmph 04-30-19 06:30 PM

Thats the point, it should loop till the player learns the spells

Terenna 04-30-19 06:44 PM

Ya, but you're in an infinite loop if they don't, so literally if there's like a millisecond where that function runs and it's not true, you'll enter an infinite loop you can't get out of because you can't "learn" a spell while the while loop is running

Terenna 04-30-19 06:45 PM

You're better off writing an OnUpdate function that runs until they're learned. That may not be optimal, because you haven't posted all of your code, just some random snippet out of context

jeruku 04-30-19 08:13 PM

To clarify, WoW addon code is loaded sequentially and an infinite loop would halt it.
Using an event like LEARNED_SPELL_IN_TAB may help as it fires when a new spell is added/learned.

Lua Code:
  1. local frame = CreateFrame('Frame')
  2. frame:SetScript('OnEvent', function(self, event, ...)
  3.     frame[event](self, ...)
  4. end
  5.  
  6. function frame:LEARNED_SPELL_IN_TAB(self, ...)
  7.     local a = IsSpellKnown(72, false)
  8.     local b = IsSpellKnown(73, false)
  9.     local c = IsSpellKnown(74, false)
  10.     local d = IsSpellKnown(75, false)
  11.  
  12.     if a and b and c and d then
  13.         ChangeActionBarPage(2)
  14.     end
  15. end
  16. frame:RegisterEvent("LEARNED_SPELL_IN_TAB")

Fizzlemizz 04-30-19 08:24 PM

Or Check for the spells (once) when you first login and then use an event like LEARNED_SPELL_IN_TAB to check again only when you learn something new.

What you are doing is telling your computer to run as hard and fast as it can until all conditions are met, so that's all it can do.

Edit: like jeruku said :)

MoreUmph 04-30-19 08:52 PM

I was hoping itd get offloaded to another thread instead of stalling the engine :/ thanks for the info, very much appreciate the suggestions

Kanegasi 05-01-19 06:10 AM

Lua is singlethreaded. This is a limitation of Lua itself and will never change. The rest of WoW may have recently been updated to multithreaded, but the UI will freeze if any for or while loops have an infinite state.


All times are GMT -6. The time now is 09:44 AM.

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