Thread Tools Display Modes
04-30-19, 05:41 PM   #1
MoreUmph
A Defias Bandit
Join Date: Apr 2019
Posts: 3
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
  Reply With Quote
04-30-19, 06:04 PM   #2
Terenna
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
Any time a, b, c, or d is not true you'll enter an infinite loop causing the crash
  Reply With Quote
04-30-19, 06:30 PM   #3
MoreUmph
A Defias Bandit
Join Date: Apr 2019
Posts: 3
Thats the point, it should loop till the player learns the spells
  Reply With Quote
04-30-19, 06:44 PM   #4
Terenna
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
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
  Reply With Quote
04-30-19, 06:45 PM   #5
Terenna
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Jun 2016
Posts: 105
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
  Reply With Quote
04-30-19, 08:13 PM   #6
jeruku
A Cobalt Mageweaver
 
jeruku's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 223
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")
__________________
"I have not failed, I simply found 10,000 ways that did not work." - Thomas Edison
  Reply With Quote
04-30-19, 08:24 PM   #7
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
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
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 04-30-19 at 08:27 PM.
  Reply With Quote
04-30-19, 08:52 PM   #8
MoreUmph
A Defias Bandit
Join Date: Apr 2019
Posts: 3
I was hoping itd get offloaded to another thread instead of stalling the engine :/ thanks for the info, very much appreciate the suggestions
  Reply With Quote
05-01-19, 06:10 AM   #9
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
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.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » Loop freezes game on load

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