Thread Tools Display Modes
11-01-10, 10:43 PM   #1
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 23
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, 02:42 PM   #3
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 23
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   #4
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 23
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   #5
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   #6
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
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   #7
TransformedBG
A Fallenroot Satyr
Join Date: Oct 2010
Posts: 23
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, 05:13 AM   #8
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

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


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