Quantcast
Functions for dummies? - WoWInterface
Thread Tools Display Modes
11-01-10, 10:43 PM   #1
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
Functions for dummies?

So im a Noob not going to lie. And ive done the simple "hello world" file. Worked fine ect.

Now im just trying to get a little more in depth by doing simple yet not so simple things like functions. Im used to C++ where you make a prototype and a function call. And from what im understanding that kind of how .LUA and .XML work?

so heres what im trying:

GLUA.toc:
Code:
## Interface: 40000
## X-Curse-Packaged-Version: 1.0.0.1
## X-Curse-Project-Name: GLUA
## notes: this should spam a lvl up message in your guild chat stating that you have leveled up. It should call your class and lvl.

GLUA.xml
GLUA.lua
GLUA.lua
Code:
GLUA = {};
local GuildLevelUp = GLUA;

function GuildLevelUp.OnLoad(self) -- Loads on player login --
	self:RegesterEvent("PLAYER_LEVEL_UP");
	DEFAULT_CHAT_FRAME:AddMessage("[Scare Bears INC] Guild Level Up Announcer has been initialized!");
	DEFAULT_CHAT_FRAME:AddMessage(" Ver. 1.0.0.9");
	DEFAULT_CHAT_FRAME:AddMessage(" Currently there are no /commands.");
end

function GuildLevelUp.OnEvent(event) -- should process on event --
	local lvl = UnitLevel("player"); -- local variable that tells me what lvl the toon is --
	local race = UnitClass("player"); -- local variable that tells me what cls the toon is --
	if ( (event == "PLAYER_LEVEL_UP") ) then
		SendChatMessage("DING! Yeah thats right im now a level "..lvl.." "..race..".", "GUILD", nil, nil);
	end
end
GLUA.xml
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/">
	<Script file="GLUA.lua" />
	<Frame name ="GLUAFrame" hidden ="false">
		<Scripts>
			<OnLoad>GuildLevelUp.OnLoad(self);</OnLoad>
			<OnEvent>GuildLevelUp.OnEvent(event);</OnEvent>
		</Scripts>
	</Frame>
</UI>
Question 1:
Now from my understanding i shouldn't have to load GLUA.xml & GLUA.lua in the toc file just the GLUA.xml, because the <script file="glua.lua"/> should initialized the lua file?

Question 2:
Why doesnt my code work? lol am i missing a function or handler or something? Or do every program have to have a frame?

Question 3:
How would i write a test function and implement it in the game? ie i want to simulate a lvl up?

Not exactly sure what im doing. just have a generalized coding idea. I have been reading and just looking at examples. so this is kind of where i stand. Any help would be greatly appreciated.

Last edited by TransformedBG : 11-01-10 at 10:51 PM.
  Reply With Quote
11-02-10, 04:23 AM   #2
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
1. Correct. You should take out the .lua file from the .toc file.

2. You misspelled RegisterEvent as RegesterEvent

3. Just run your function.

/run GLUA.OnEvent("PLAYER_LEVEL_UP")

should do it from your chat.
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes
  Reply With Quote
11-02-10, 05:13 AM   #3
yj589794
A Rage Talon Dragon Guard
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 314
Originally Posted by TransformedBG View Post
Im used to C++ where you make a prototype and a function call. And from what im understanding that kind of how .LUA and .XML work?
If you are familiar with other languages then you should be able to make quick work of reading the following:

Lua 5.1. Reference Manual
Programming in Lua

The above links should give you all the technical info about Lua that you could ever wish for.
  Reply With Quote
11-02-10, 02:42 PM   #4
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
Originally Posted by Xinhuan View Post
1. Correct. You should take out the .lua file from the .toc file.

2. You misspelled RegisterEvent as RegesterEvent

3. Just run your function.

/run GLUA.OnEvent("PLAYER_LEVEL_UP")

should do it from your chat.
Ty kind sir.. let me give this a shot and see what happens
  Reply With Quote
11-02-10, 03:34 PM   #5
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
okay so after /run GLUA.OnEvent("PLAYER_LEVEL_UP") the function was successful.

However I cant get the OnLoad function to load when the game starts.. any idea?
  Reply With Quote
11-02-10, 04:33 PM   #6
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
First, see if your frame got created, do this:

/print GLUAFrame

You should see "Table: 0x29347293" or whatever if it got created.

If it didn't, you might want to edit your XML and remove the empty spaces before the equal signs

<Frame name="GLUAFrame" hidden="false">
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes
  Reply With Quote
11-02-10, 04:40 PM   #7
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,257
Your OnEvent is a couple parameters short too:
xml
Code:
<OnEvent>GuildLevelUp.OnEvent(self, event, ...);</OnEvent>
lua
Code:
function GuildLevelUp.OnEvent(self, event, ...)
Secondly it looks like the script handler in the xml are trying to call functions that are out of scope.

You have 'local GuildLevelUp' on top of your .lua file.

Try changing the function names in the .xml to GLUA.OnEvent and GLUA.OnLoad
The whole getting a local reference to a global table deal is pointless for this particular case btw.

Last edited by Dridzt : 11-02-10 at 04:42 PM.
  Reply With Quote
11-02-10, 04:49 PM   #8
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
Originally Posted by Xinhuan View Post
First, see if your frame got created, do this:

/print GLUAFrame

You should see "Table: 0x29347293" or whatever if it got created.

If it didn't, you might want to edit your XML and remove the empty spaces before the equal signs

<Frame name="GLUAFrame" hidden="false">
so i tried /print in game and even tired print(GLUAFrame) in the .lua file

/print in game just give me refert to help yada yada yada
and when i put it in the lua file i get a return value of nil.

tried editing xml file as well.
  Reply With Quote
11-02-10, 06:01 PM   #9
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
Originally Posted by Dridzt View Post
Your OnEvent is a couple parameters short too:
xml
Code:
<OnEvent>GuildLevelUp.OnEvent(self, event, ...);</OnEvent>
lua
Code:
function GuildLevelUp.OnEvent(self, event, ...)
Secondly it looks like the script handler in the xml are trying to call functions that are out of scope.

You have 'local GuildLevelUp' on top of your .lua file.

Try changing the function names in the .xml to GLUA.OnEvent and GLUA.OnLoad
The whole getting a local reference to a global table deal is pointless for this particular case btw.

YTMD! Scope.. oh how i have forgot about that word..

But just curious.. what does "..." mean exactly?
  Reply With Quote
11-02-10, 06:13 PM   #10
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,257
... is a special variable name that holds an indefinite number of parameters.

It's called a vararg (variable arguments)

When you don't know how many params will be passed to a function
(OnEvent is a prime example as different events pass a different number of arguments)
you can use a vararg to catch them all.

To assign the individual arguments to variables you can just do:
local arg1, arg2, arg3, argX = ...
inside the function.
If you want to know the number of arguments in the vararg
local args = select("#",...) will give you that.

Last edited by Dridzt : 11-02-10 at 07:00 PM.
  Reply With Quote
11-02-10, 07:37 PM   #11
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,748
/run print(GLUAframe)
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
11-03-10, 03:17 PM   #12
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
Originally Posted by Dridzt View Post
... is a special variable name that holds an indefinite number of parameters.

It's called a vararg (variable arguments)

When you don't know how many params will be passed to a function
(OnEvent is a prime example as different events pass a different number of arguments)
you can use a vararg to catch them all.

To assign the individual arguments to variables you can just do:
local arg1, arg2, arg3, argX = ...
inside the function.
If you want to know the number of arguments in the vararg
local args = select("#",...) will give you that.

Aww okay i wasnt quite sure..


Have another question rather than make a new thread.

I want to extend my function to something that would tell a guildie who logs on hello. im thinking it would look something like this?

Code:
function GuildLevelUp.OnEvent(self, event, ...)
	local lvl = UnitLevel("player");
	local race = UnitClass("player");
        local playerOn = ? -- what function would i call?
	if ( (event == "PLAYER_LEVEL_UP") ) then
		SendChatMessage("DING! Yeah thats right im now a level "..lvl.." "..race..".", "GUILD", nil, nil);
	elseif ( (event =="PLAYER_ENTERING_WORLD") then
		SendChatMessage("Hello "...playerOn..."! Nice to see you on today!", "GUILD", nil, nil);
		
		else
	
	end
end
I know i would need to change the OnLoad to something different. I belive it would be something like:

Code:
function GuildLevelUp.OnLoad(self) -- Loads on player login --
	self:RegisterEvent("PLAYER_LEVEL_UP");
	self:RegisterEvent("PLAYER_LOGIN");
	self:RegisterEvent("CHAT_MSG_GUILD_ACHIEVEMENT"); -- this would be later if someone got an achievement -- 

	DEFAULT_CHAT_FRAME:AddMessage("[Scare Bears INC] Guild Level Up Announcer has been initialized!");
	DEFAULT_CHAT_FRAME:AddMessage(" Ver. 1.0.0.11");
	DEFAULT_CHAT_FRAME:AddMessage(" Currently there are no /commands.");
end
I know that you pass in "self" being an event that happens to you, but how do you pass an arg that happens in guild? im not sure on that.
  Reply With Quote
11-03-10, 05:29 PM   #13
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,257
self has nothing to do with who the event concerns.

'self' holds a reference to the frame that the event is registered to.

In the particular example you have posted: GLUAFrame
  Reply With Quote
11-03-10, 06:13 PM   #14
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
Originally Posted by Dridzt View Post
self has nothing to do with who the event concerns.

'self' holds a reference to the frame that the event is registered to.

In the particular example you have posted: GLUAFrame

Okay, so self is a representation of an "argument" or event being passed into the function.

So if i pass in a (self, event, ...)

the self would be the player that logged on? or how could i record who that is? i would think i would have to use some sort of strlng? or array to record the toons name?

the event would be ("PLAYER_LOGIN")? or would it be ("PLAYER_ENTERS_WORLD")?
  Reply With Quote
11-03-10, 06:22 PM   #15
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
WoW doesn't give you any details about who logged on in the guild via any guild or player event. Therefore your only main option is to register for the CHAT_MSG_SYSTEM event, then parse the arguments that are passed in.

http://wowprogramming.com/docs/events/CHAT_MSG_SYSTEM

In this code, "self" is the frame that registered the event, I shall repeat this, it is the frame that registered the event, not some reference to your player, playername or whatever, it is a FRAME.

"event" is the event that was triggered.

Code:
function GuildLevelUp.OnEvent(self, event, ...)
	local lvl = UnitLevel("player")
	local race = UnitClass("player")
	if event == "PLAYER_LEVEL_UP" then
		SendChatMessage("DING! Yeah thats right im now a level "..lvl.." "..race..".", "GUILD", nil, nil);
	elseif event =="CHAT_MSG_SYSTEM" then
                local message = ...
                print(message)	
	end
end
Here, you want to replace the print(message) with code to extract the name out from the string "ABC" from the string "ABC has come online." and then use it to send a greeting over guild chat.

Google a bit for the Lua string functions string.match() or string.find().

You don't want to use PLAYER_ENTERING_WORLD. This event only fires whenever you (and only you) zone across worlds, and does not have any varargs passed in.
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes
  Reply With Quote
11-03-10, 06:40 PM   #16
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
So i would need to extract the name then do a simple code like

Code:
local name = string.match(msg, "(.+) has came online.);
if ( (name ~= self) ) then
     SendChatMessage("Hello "...name..."! Nice to see you on today!", "GUILD", nil, nil);
end
  Reply With Quote
11-03-10, 07:36 PM   #17
Beoko
A Cyclonian
 
Beoko's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2007
Posts: 41
Originally Posted by TransformedBG View Post
So i would need to extract the name then do a simple code like

Code:
local name = string.match(msg, "(.+) has came online.);
if ( (name ~= self) ) then
     SendChatMessage("Hello "...name..."! Nice to see you on today!", "GUILD", nil, nil);
end
You're getting there, but again, as Xinhuan stated: self is a reference to the frame, not the specific player's name. You may need to tweak your string matching to accommodate for false positives from other situations by CHAT_MSG_SYSTEM. After that, nil checking the name should be sufficient. Lastly, I'm not sure if this is just a typo, but I notice you have three dots when you're concatenating the variable in the string. Concatenation is two dots, and a Vararg is three dots.
  Reply With Quote
11-03-10, 08:01 PM   #18
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,748
And those variables don't even need to be named self or event. They could be named mycoolframe and whenstuffhappens, for all Lua cares.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
11-17-10, 07:24 PM   #19
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 20
So i got it all up and running fine.. now i want to expand on this function a little more. I want to have multiple saying that it could randomly select to say instead of one defined saying.

I was thinkign it would have to be something like this:

Code:
local me = UnitClass("player");
local number = UnitLevel("player");
number = number +1;
quote_this = {};

quote_this["level"] = {
"DING DING DING! Yeah thats right this guild needed "..me.."!",
"OH NOEZ! What just happened? Ohh that was me! I Just LEVELED! BOYAH!",
"Check this guys! Im now a level "..number.." "..me.."!",
"If you heard it once, you have now heard it "..number..." times!",
"DING DING DING Tell um what you just won "..me.."!",
"Go ahead and just say gratz already!",
}
But im not sure i would work the call into SendChatMessage(...)? any help would be appreciated.
  Reply With Quote
11-17-10, 08:15 PM   #20
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,935
Originally Posted by TransformedBG View Post
So i got it all up and running fine.. now i want to expand on this function a little more. I want to have multiple saying that it could randomly select to say instead of one defined saying.

I was thinkign it would have to be something like this:

Code:
local me = UnitClass("player");
local number = UnitLevel("player");
number = number +1;
quote_this = {};

quote_this["level"] = {
"DING DING DING! Yeah thats right this guild needed "..me.."!",
"OH NOEZ! What just happened? Ohh that was me! I Just LEVELED! BOYAH!",
"Check this guys! Im now a level "..number.." "..me.."!",
"If you heard it once, you have now heard it "..number..." times!",
"DING DING DING Tell um what you just won "..me.."!",
"Go ahead and just say gratz already!",
}
But im not sure i would work the call into SendChatMessage(...)? any help would be appreciated.
It would end up something like this.
Code:
local list=qoute_this["level"];
SendChatMessage(list[math.random(#list)],"GUILD");
This allows you to change the list later on, adding or removing lines as you wish without needing to change the call.
__________________
ESOUI AddOns | 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)

Last edited by SDPhantom : 11-17-10 at 08:21 PM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Functions for dummies?

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