Thread Tools Display Modes
02-23-09, 06:56 AM   #1
ZikO
An Aku'mai Servant
Join Date: Jun 2008
Posts: 36
AddOn purely coded in lua.

Hi guys

I probably have very uncommon request to you and if you think you would have to put too much effort to help me just skip this threat.

I hate all this stuff with XML but unfortunately I need to know this as I can't get through running AddOn coded only in lua.

Can somebody please write very very simple AddOn (like showing frame with one button) in lua not involving XML at all. How to do that? Please.



So far. I have created xml file to handle OnEvent and OnLoad properties so my xml code looks like:

Code:
<ui> ...
    <OnLoad>MyAddon:Load()</OnLoad>
    <Onevent>MyAddon:EventHandler(frame, firstArg, ...)</OnEvent>
</ui>
Now, in lua i have to create functions whichmight look like:

Code:
function MyAddon:Load()
-- something to do when AddOn is loaded
end

function MyAddon:EventHandler(frame, firstArg, ...)
-- handle particular events
end
I tried to remove xml file and make frames in lua like below:
Code:
frame = CreateFrame("FRAME", "MyAddon")

frame:SetScript("OnLoad", MyAddon:Load)
frame:SetScript("OnEvent", myAddon:Eventhandler)
and it's not working. I'm still doing something wrong. Please if someone has enough time and write these a few lines on lua to show me that. The rest i should handle and manage to do. Many thanks.
  Reply With Quote
02-23-09, 07:10 AM   #2
Mera
Retired of WoW, In ESO :)
 
Mera's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 331
this basic code is often used to delay a mod after PLAYER_LOGIN fires so you are sure all datas you will need clientside are loaded

Code:
local function eventFunction()
--stuff that will execute after PLAYER_LOGIN fired
end

local frame = CreateFrame("Frame")
frame:SetScript("OnEvent", eventFunction)
frame:RegisterEvent("PLAYER_LOGIN")
OnLoad isn't really required but you could call it that way too

Also think to declare lesser globals than locals because the way you wrote it

frame = means global frame = so your mod will ninja the name "frame" globaly and this could cause very strange bugs in other mods 7
local frame = is better

I agreee with you XML sux hehe
__________________
If you need to reach me I'm in ESO, @class101 or "Fathis Ules i"
addons: SpamBayes, BrokerCPU
projects: ThunderBayes
Mera[xeh]? - La CroisadeEcarlate (wow)

Last edited by Mera : 02-23-09 at 07:13 AM.
  Reply With Quote
02-23-09, 02:58 PM   #3
ZikO
An Aku'mai Servant
Join Date: Jun 2008
Posts: 36
Thanks Mera. Hehe My first pure lua AddOn has been run and it's working perfectly. I have also keep in mind all this stuff about local functions, variables etc.

To be honest, one more question just arose :P

What if my AddOn becomes more complicated and I needed to keep it tidy so I would like to divide it into more modules? How those modules "communicate" between each other? They can't be local anymore, aren't they? I mean if I wanted to run a function from different module I would need this function define as global i suspect. Isn't it better then to define a package such as:

MyAddonFunctions = {}
MyAddonFunctions:Something()
MyAddonFunctions:OtherStuff()
MyAddonFunctions:Text()
MyAddonFunctions:Warnings()

etc

Is it good practice to keep package like MyAddonFunction ={} ?

or is there anything else betterwhich I don't know about it yet :P

regards
  Reply With Quote
02-23-09, 03:08 PM   #4
DreamStorm
A Deviate Faerie Dragon
 
DreamStorm's Avatar
Join Date: Feb 2009
Posts: 15
Originally Posted by ZikO View Post
Code:
frame:SetScript("OnLoad", MyAddon:Load)
frame:SetScript("OnEvent", myAddon:Eventhandler)
If you do it that way the latter part of the set script should have a dot not a colon, like

Code:
frame:SetScript("OnEvent", myAddon.Eventhandler)
__________________


ultima ratio regum
  Reply With Quote
02-23-09, 05:11 PM   #5
Mera
Retired of WoW, In ESO :)
 
Mera's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Apr 2008
Posts: 331
you can code with locals and define a later time the function that will build a global copy of it

myAddon = blabla global {}

local function eventFunction()
--stuff that will execute after PLAYER_LOGIN fired
end

local function InitGlobalFunctions()
myAddon.eventFunction = eventFunction -- store eventFunction in a global myAddon.eventFunction
end

local frame = CreateFrame("Frame")
frame:SetScript("OnEvent", eventFunction)
frame:RegisterEvent("PLAYER_LOGIN")


so when calling then your init global function you control what and when your global myAddon.eventFunction() is built and you are also less tempted to build everything global because most functions does not need to be global and it costs some extra cpu access times

then in the other module you can do vise versa send back the global to local

local NeweventFunction = myAddon.eventFunction

etc
__________________
If you need to reach me I'm in ESO, @class101 or "Fathis Ules i"
addons: SpamBayes, BrokerCPU
projects: ThunderBayes
Mera[xeh]? - La CroisadeEcarlate (wow)

Last edited by Mera : 02-23-09 at 05:16 PM.
  Reply With Quote
02-24-09, 02:50 AM   #6
ZikO
An Aku'mai Servant
Join Date: Jun 2008
Posts: 36
I got your point Mera ty vm =)

I was reading the tutorial about the scope here and I found this line
Because of the implementation of Lua it is more efficient to use local variables wherever possible. The technical reason for this is that local variables are referenced via an assigned number, whereas global variables are stored in a table which is accessed with a key (the variable name). Table lookups are very fast in Lua, but still not as fast as local register lookups.
I think it's what you meant about CPU =)

DreamStorm
I thought those two form are the same for lua. But I will keep it in my mind. Thanks.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » AddOn purely coded in lua.


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