ESOUI

ESOUI (https://www.esoui.com/forums/index.php)
-   AddOn Help/Support (https://www.esoui.com/forums/forumdisplay.php?f=164)
-   -   Assistance needed for registering events (https://www.esoui.com/forums/showthread.php?t=1728)

jtsmith1287 06/04/14 10:36 PM

Assistance needed for registering events
 
So, I'm learning lua and the ESO API. I had an idea for a super simple addon, but getting just the basics down is proving to be a nightmare for me. I'm going to (embarrassingly) paste my code and ask that you guys tell me where I'm going wrong. What I'm looking to accomplish is getting the information of the items I'm looting, but I can't even get the dang loot event registered. And excuse the weird variable names--lots of poking and testing has been going on, as well as changing my addon idea a few times after learning of API restrictions. That and some of this is pasted examples from the esoui wiki.

When I gain loot, I want to see these items printed into chat via d() or something so I can start figuring this out. Thanks in advance for the help.

PileOMobs.lua
Code:

function lootReceived(receivedBy, itemName, quantity, itemSound, lootType, self)

  Loot:SetText("You looted stuff!!!")
  d(itemName)

  end
 
function RegisterLootEvent(eventcode, name)

  if (name == "PileOMobs") then
    PileOMobs:RegisterForEvent(EVENT_LOOT_RECEIVED, lootReceived)
    Loot:SetText("Loot event registered.")
    end

  end

function PileOMobsInit()

  EVENT_MANAGER:RegisterForEvent("PileOMobs", EVENT_ADD_ON_LOADED, RegisterLootEvent)

  end


PileOMobs.xml
Code:

<GuiXml>
    <Controls>
        <TopLevelControl name="Whatever">
            <Dimensions x="250" y="80" />
            <Anchor point="RIGHT" />

            <OnInitialized>
                PileOMobsInit()
            </OnInitialized>
 
            <Controls>
                <Label name="Loot" font="ZoFontGame" color="CFDCBD" wrapMode="ELLIPSIS" verticalAlignment="TOP" text="Got yerself a errawr">
                    <AnchorFill />
                </Label>
            </Controls>
        </TopLevelControl>
    </Controls>
</GuiXml>


Tar000un 06/05/14 01:31 AM

A good practice is to prefix your public function with the addon name. You will avoid few kinds of conflict. As example :
Quote:

PileOMobs.lootReceived
I also saw with a :, instead the dot.


And i think you missed a name on the second, and got a wrong controller.
Quote:

PileOMobs:RegisterForEvent(EVENT_LOOT_RECEIVED, lootReceived)

Try to type it like the first, what will be like :
Quote:

EVENT_MANAGER:RegisterForEvent("PileOMobs", EVENT_LOOT_RECEIVED, lootReceived)

Xrystal 06/05/14 01:34 AM

You're almost there but but not quite got it right.

Give this slight change a go

Lua Code:
  1. local function lootReceived(eventCode,receivedBy, itemName, quantity, itemSound, lootType, self)
  2.   Loot:SetText("You looted stuff!!!")
  3.   d(itemName)
  4. end
  5.  
  6. local function RegisterLootEvent(eventcode, name)
  7.   if (name == "PileOMobs") then
  8.     EVENT_MANAGER:RegisterForEvent("PileOMobs",EVENT_LOOT_RECEIVED, lootReceived)
  9.     Loot:SetText("Loot event registered.")   --- Note: Message probably won't appear at this stage
  10.   end
  11. end
  12.  
  13. function PileOMobsInit(self)
  14.   self:RegisterForEvent("PileOMobs", EVENT_ADD_ON_LOADED, RegisterLootEvent)
  15. end

Sasky 06/06/14 02:01 PM

Quote:

Originally Posted by Tar000un (Post 9101)
I also saw with a :, instead the dot.

In Lua, using the colon ( : ) instead of the dot makes a variable "self" available in the function.

For example:
Lua Code:
  1. local foo = {}
  2. foo.val = 1
  3.  
  4. function foo:bar()
  5.     d(self.val) --Prints 1
  6. end

When you fully define the tables, it's not as critical, since you can simply use the table name instead:
Lua Code:
  1. function foo.bar()
  2.     d(foo.val) --Prints 1
  3. end

Where it's most useful is object-oriented programming style, where you have the same function with different objects and need self to differentiate between them.

Tar000un 06/07/14 01:58 AM

Thanks for the enlightenment !


All times are GMT -6. The time now is 03:16 AM.

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