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 Code:
GLUA = {}; 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/"> 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. |
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. |
Quote:
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. |
Quote:
|
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? |
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"> |
Your OnEvent is a couple parameters short too:
xml Code:
<OnEvent>GuildLevelUp.OnEvent(self, event, ...);</OnEvent> Code:
function GuildLevelUp.OnEvent(self, event, ...) 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. |
Quote:
/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. |
Quote:
YTMD! Scope.. oh how i have forgot about that word.. But just curious.. what does "..." mean exactly? |
... 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. |
/run print(GLUAframe)
|
Quote:
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, ...) Code:
function GuildLevelUp.OnLoad(self) -- Loads on player login -- |
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 |
Quote:
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")? |
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, ...) 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. |
So i would need to extract the name then do a simple code like
Code:
local name = string.match(msg, "(.+) has came online.); |
Quote:
|
And those variables don't even need to be named self or event. They could be named mycoolframe and whenstuffhappens, for all Lua cares. ;)
|
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"); |
Quote:
Code:
local list=qoute_this["level"]; |
All times are GMT -6. The time now is 03:58 PM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI