Thread Tools Display Modes
10-20-10, 09:18 PM   #1
scoteecopter
A Murloc Raider
Join Date: Oct 2010
Posts: 4
New to Lua and wondering what I'm doing wrong

So I recently decided I wanted to learn Lua and be able to make addons. So I figured I would start out small to test what I have learned so far. Yet for some reason I can't figure out, it isn't working as intended.

What I have so far is:
Code:
local time = GetPVPTimer()

while time == 270000 do 
   print ("4:30 remaining!")
   end
end
What I'm trying to do is just announce in the chat box the time remaining on a PVP timer at certain intervals. However it seems to only run the script when I load into the game, instead of constantly checking to see when time hits 270000.
  Reply With Quote
10-20-10, 09:42 PM   #2
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
well thats exactly what its doing. It runs the code once and the start of the loading of the UI and then its done.

You need to trigger events or use scripts in order to make references to it.

so make the lua this instead

Code:
local function checkTime()
    local pvpTime = GetPVPTimer()
    if pvpTime == 27000 then
        print("4:30 remaining")
    end
end
local f = CreateFrame("Frame") -- creates a frame for you
f:SetScript("OnUpdate", function(self, elapsed)
    self.elapsed = ( self.elapsed or 0 ) + elapsed
    if self.elapsed > 0.5 then --check every half second
        checkTime()
        self.elapsed = 0
    end
end)
A good starting point is WowWiki's How to make an addon
http://www.wowwiki.com/Getting_start...writing_addons
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
10-20-10, 10:03 PM   #3
scoteecopter
A Murloc Raider
Join Date: Oct 2010
Posts: 4
Thanks for the help! Hopefully I can pick this apart and get a better understanding of things. I was looking at the article you linked beforehand but found it rather uninformative :/

I've been Googling my ass off but it seems every resource I find tells me how to print("hello world") or wants to assume that I already know C. :/

My current dilemma is what on earth does "function" actually do?

EDIT: I also copied and pasted your code and it doesn't seem to do anything :/

Last edited by scoteecopter : 10-20-10 at 10:10 PM.
  Reply With Quote
10-20-10, 10:38 PM   #4
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
a function is a reference to a line of commands, and it can accept parameters.

like

Code:
local a,b,c = 10,20,30

function add(x,y)
  return x + y
end

print(add(a,b)) --outputs 30
print(add(b,c)) --outputs 50
print(add(a,c)) --outputs 40
You dont have to know C, you need to know Lua.

http://www.wowwiki.com/AddOn_program...l/Introduction
that is the first link on the content part of the page of the previous link... its a lot of help when starting out.

try adding print("this is a test") at the top of the file that you pasted. if it doesnt show up, then you havent implemented your *.lua file correctly, that link will show you how. which is how you get started.

Lots of snippets here
http://www.wowwiki.com/Category:HOWTOs
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
10-20-10, 10:51 PM   #5
scoteecopter
A Murloc Raider
Join Date: Oct 2010
Posts: 4
I currently have:

Code:
print("this is a test")
local function checkTime()
    local pvpTime = GetPVPTimer()
    if pvpTime == 290000 then
        print("4:30 remaining")
    end
end
local f = CreateFrame("Frame") 
f:SetScript("OnUpdate", function(self, elapsed)
    self.elapsed = ( self.elapsed or 0 ) + elapsed
    if self.elapsed > 0.5 then
        checkTime()
        self.elapsed = 0
    end
end)
Ok interesting results. 300000 is the full 5m pvp timer. So I changed it to printout at 290000 so I don't have to sit around and wait. If I keep it at ' == ' I get nothing out of it. If I set it to ' < ' It will print out every 0.5 seconds after it hits 290000. So is it somehow not checking the time at exactly 290000 with the previous code?

Last edited by scoteecopter : 10-20-10 at 10:55 PM.
  Reply With Quote
10-21-10, 12:18 AM   #6
Xubera
A Cobalt Mageweaver
 
Xubera's Avatar
AddOn Author - Click to view addons
Join Date: May 2009
Posts: 207
thats probably because it was more like 29001 or 28999 or something along those lines

OnUpdate is called everyime the frame refreshes. so if you have 30 fps, you call onupdate 30 times a second.

the self.elapsed statement is there to make sure it only calls the function ATLEAST every 0.5 seconds...

if the actual elapsed time was 4.9 seconds and then your game had a sudden drop in frame rate to 0 for 3 minutes... the function wouldnt be called until your FPS actually went up. then your elapsed would be 184.9 (3 minutes, 4.9 sec) and the function would call.

so the odds that you will ever hit 29000 are next to none.

you could do

if time < 29000 and time > 28998 then

giving it a 2 second window.

or just

print(format("%d:%02d", time/6000, time%6000))
__________________
Chat Consolidate is the solution to any out of control trade chat. Ignore lines, throttle chat, consolidate posts!Follow the link to find out how!

▲ ▲ WoWInterface wont let me triforce >.>
  Reply With Quote
10-21-10, 12:20 AM   #7
Mikord
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 61
That code is telling it to only check the time every 0.5 seconds (give or take).

self.elapsed starts at zero and adds how ever long it has been since the last update occurred (elapsed) until it reach a value higher than 0.5 (1/2 a second) at which point checkTime is invoked and self.elapsed is reset.

That said, even if you were to check it on every single frame update (which would be horribly inefficient), there is no guarantee your code will be called when the PvP timer has exactly 290000 remaining. In fact, it's highly unlikely it would be given the non-deterministic nature of OnUpdate callbacks.
  Reply With Quote
10-21-10, 12:27 AM   #8
lilsparky
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 117
switch to checking if time remaining is less than 29000 or whatever number and instead of just printing, print and then disable the onUpdate script so it no longer is called. this can be done a number of ways. either call f:Hide() after printing (be sure to declare f prior to the checkTime function) or call f:SetScript("OnUpdate", nil). either will work. hiding a frame might be nicer since you could Show() the frame to resume your checking.
  Reply With Quote
10-21-10, 07:33 PM   #9
scoteecopter
A Murloc Raider
Join Date: Oct 2010
Posts: 4
Thank you all for your help! I picked up a Lua for WoW book and was just wanting to play around some. Seems I got a bit ahead of myself and should go back to reading
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » New to Lua and wondering what I'm doing wrong


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