Quantcast WoWInterface - View Single Post - Object-Oriented System for lua
View Single Post
09-08-11, 03:11 AM   #2
kurapica.igas
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 93
Sample Addon to show the thread system, to count money you spent on the merchant for every time:

TestAddon.toc
Code:
## Interface: 40200
## Title: TestAddon
## DefaultState: Enabled
## RequiredDeps: IGAS
## LoadOnDemand: 0
TestAddon.lua
TestAddon.lua
Code:
-- make codes run under one addon's environment 
IGAS:NewAddon "TestAddon"

function OnLoad(self)        -- also can be written as 'function _Addon:OnLoad()'
    -- This script fired when the addon is loaded.

    -- make OnEnable script run as a thread
    self:ActiveThread("OnEnable")
end

function OnEnable(self)
    -- This script fired when the addon is enabled.Will occur once at the beginning, 
    -- or enable addon when it's disabled.

    local money

    while _Enabled do
        -- _Enabled is the addon's enabled property, also can be written as '_Addon._Enabled'

        -- make thread wait for visiting merchant
        System.Threading.WaitEvent("MERCHANT_SHOW")
    
        money = GetMoney()
        
        -- make thread wait for close merchant
        System.Threading.WaitEvent("MERCHANT_CLOSED")

        -- now count the cost
        if GetMoney() > money then
           print("You got "..FormatMoney(GetMoney() - money))
        elseif GetMoney() < money then
           print("You spent "..FormatMoney(money - GetMoney()))
        end
    end
end

-- Get Format money
function FormatMoney(money)
	if money > 10000 then
		return (GOLD_AMOUNT_TEXTURE.." "..SILVER_AMOUNT_TEXTURE.." "..COPPER_AMOUNT_TEXTURE):format(math.floor(money / 10000), 0, 0, math.floor(money % 10000 / 100), 0, 0, money % 100, 0, 0)
	elseif money > 100 then
		return (SILVER_AMOUNT_TEXTURE.." "..COPPER_AMOUNT_TEXTURE):format(math.floor(money % 10000 / 100), 0, 0, money % 100, 0, 0)
	else
		return (COPPER_AMOUNT_TEXTURE):format(money % 100, 0, 0)
	end
end
  Reply With Quote