WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Functions for dummies? (https://www.wowinterface.com/forums/showthread.php?t=36473)

TransformedBG 11-01-10 10:43 PM

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.

Xinhuan 11-02-10 04:23 AM

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.

yj589794 11-02-10 05:13 AM

Quote:

Originally Posted by TransformedBG (Post 215843)
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.

TransformedBG 11-02-10 02:42 PM

Quote:

Originally Posted by Xinhuan (Post 215874)
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

TransformedBG 11-02-10 03:34 PM

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?

Xinhuan 11-02-10 04:33 PM

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">

Dridzt 11-02-10 04:40 PM

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.

TransformedBG 11-02-10 04:49 PM

Quote:

Originally Posted by Xinhuan (Post 216022)
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.

TransformedBG 11-02-10 06:01 PM

Quote:

Originally Posted by Dridzt (Post 216023)
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?

Dridzt 11-02-10 06:13 PM

... 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.

Seerah 11-02-10 07:37 PM

/run print(GLUAframe)

TransformedBG 11-03-10 03:17 PM

Quote:

Originally Posted by Dridzt (Post 216037)
... 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.

Dridzt 11-03-10 05:29 PM

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

TransformedBG 11-03-10 06:13 PM

Quote:

Originally Posted by Dridzt (Post 216239)
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")?

Xinhuan 11-03-10 06:22 PM

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.

TransformedBG 11-03-10 06:40 PM

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


Beoko 11-03-10 07:36 PM

Quote:

Originally Posted by TransformedBG (Post 216256)
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.

Seerah 11-03-10 08:01 PM

And those variables don't even need to be named self or event. They could be named mycoolframe and whenstuffhappens, for all Lua cares. ;)

TransformedBG 11-17-10 07:24 PM

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.

SDPhantom 11-17-10 08:15 PM

Quote:

Originally Posted by TransformedBG (Post 218345)
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.


All times are GMT -6. The time now is 03:58 PM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI