Thread Tools Display Modes
04-09-13, 07:14 AM   #1
Septih
A Defias Bandit
Join Date: Jan 2007
Posts: 3
Update Code Running Twice

I'm trying to use the pattern of waiting till x seconds have passed before running the update code, but for some reason once it gets passed the if statement it runs the update twice.

Code is below, should count up 1 every 8 seconds, instead counts up 1 then another 1 instantly every 8 seconds. e.g.

00:00: 1
00:00: 2
00:08: 3
00:08: 4
00:16: 5
00:16: 6
etc

Code:
local MyAddon = CreateFrame("Frame","MyAddon",UIParent)
MyAddon.Units = {}
MyAddon_UpdateInterval = 8.0
loaded=false
e=0

function MyAddon:Initialize(self)
	loaded=true
end

function MyAddon:Update()
	e = e + 1
	print(e)
end

local timeElapsed = 0;
MyAddon:SetScript("OnUpdate", function(self,elapsed)
		timeElapsed = timeElapsed + elapsed
		if(timeElapsed > MyAddon_UpdateInterval) then
			timeElapsed = 0.0
			MyAddon:Update()
		end
end)
Code:
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/..\FrameXML\UI.xsd">
	<Script File="addon.lua" />
</Ui>
It seems like it's a concurrency issue, but that would break the whole idea of slowing the update method wouldn't it?

I also tried putting in an update counter so that it would only update every 5 calls, but it still behaved the same way.
  Reply With Quote
04-09-13, 07:27 AM   #2
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Try to make your 'e' variable local instead (and do the same to loaded). Keep everything as local as possible. This fixes all kinds of strange problems, possibly this one as well, and it's general good practice.

Also, lua makes no distinction between integers and float/double, so there is no need to add decimals to your counter variable.

Edit: I tried this and the counter is actually working fine, maybe I misunderstood the problem? You're not loading the code twice (through ToC and XML), are you?

Last edited by Haleth : 04-09-13 at 07:34 AM.
  Reply With Quote
04-09-13, 07:35 AM   #3
Septih
A Defias Bandit
Join Date: Jan 2007
Posts: 3
That didn't fix it, but it did point me in the right direction. With that change it started doing:

1
1
2
2
3
3

which looked like the addon was running twice (and because the e variable wasn't local before, it was being incremented by each version). I removed the <Script> tag from the xml file and it's working fine now.

Thank you!
  Reply With Quote
04-09-13, 07:41 AM   #4
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
You are calling MyAddon:Update() within your OnUpdate script. That translates into something like "While looping until you get to 8 seconds, loop during the initial 8 seconds".

Side note: the variables "loaded" and "MyAddon_UpdateInterval" and "e" should be local.

Final code might look something like the following. I did away with your xml file, since everything can be done in Lua, and xml is nasty to debug, hard to look at, and I don't like it.
Lua Code:
  1. local MyAddon, pt = ... -- string reference to your AddOn, private table shared between all files in your AddOn
  2. local MyAddon_frame = CreateFrame("Frame")
  3. local updateInterval = 8
  4. local e -- needs a descriptive name
  5. local addon_loaded
  6. local MyAddon_units = {}
  7.  
  8. function MyAddon:Initialize(self)
  9.     addon_loaded = true
  10. end
  11.  
  12. local timer = 0
  13. MyAddon_frame:SetScript("OnUpdate", function(self, elapsed)
  14.     timer = timer + elapsed
  15.     if timer <= updateInterval then
  16.         -- do something
  17.     end
  18.     timer = 0
  19. end)
  Reply With Quote
04-09-13, 07:45 AM   #5
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
Here is a WoWPedia link that might help. It uses xml, but gives the general idea. http://www.wowpedia.org/Using_OnUpdate_correctly
  Reply With Quote
04-09-13, 07:53 AM   #6
Haleth
This Space For Rent
 
Haleth's Avatar
Featured
Join Date: Sep 2008
Posts: 1,173
Yeah, it looks like the problem was that you were loading the lua file in the ToC, and in the XML as well, so you just ended up with the same code running twice. Bescause 'e' was global it kept incrementing the same variable twice each time.

Also a tip, make sure you use descriptive names for your variables. Anyone should be able to figure out more or less what a variable is used for just by looking at its name, and especially in larger projects, 'e' just won't cut it.
  Reply With Quote
04-09-13, 11:48 AM   #7
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
I have a typo in line 8 above.
Lua Code:
  1. MyAddon.Initialize(self)
  2.     addon_loaded = true
  3. end
But if you wanted to really know if your AddOn was loaded, register the event ADDON_LOADED. Adding to the above code...
Lua Code:
  1. MyAddon_frame:RegisterEvent("ADDON_LOADED")
  2. function MyAddon_frame:ADDON_LOADED(...)
  3.     if ... == MyAddon then
  4.         addon_loaded = true
  5.         MyAddon_frame:UnregisterEvent("ADDON_LOADED") -- we don't check for other AddOns
  6.     end
  7. end
  Reply With Quote
04-09-13, 08:03 PM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,323
Originally Posted by myrroddin View Post
Lua Code:
  1. MyAddon_frame:RegisterEvent("ADDON_LOADED")
  2. function MyAddon_frame:ADDON_LOADED(...)
  3.     if ... == MyAddon then
  4.         addon_loaded = true
  5.         MyAddon_frame:UnregisterEvent("ADDON_LOADED") -- we don't check for other AddOns
  6.     end
  7. end
This won't work without the following OnEvent handler.
Lua Code:
  1. MyAddon_frame:SetScript("OnEvent",function(self,event,...)
  2.     local func=self[event];
  3.     if func then func(...); end
  4. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Update Code Running Twice


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