WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   General Authoring Discussion (https://www.wowinterface.com/forums/forumdisplay.php?f=20)
-   -   I admit defeat (https://www.wowinterface.com/forums/showthread.php?t=48623)

MoonWitch 12-06-13 01:27 PM

I admit defeat
 
I haven't a clue why all of the sudden it no longer makes sense to me, but after more than a year away - I can't seem to write lua anymore. :(

It refuses to register in my thick skull, leaving me feeling rather inadequate right now (Ok, I have the flu together with abdo flu, so I am feeling quite down.) I've been trying to wrap my head around how others do the scope, event handling, module setup, but it seems to elude me.
Say, p3lim, he handles it well I think, but I just don't understand : https://github.com/p3lim-wow/Inomena...er/Inomena.lua (I am certain if I understand it, I am saved)

Basically; here's what I've been trying to achieve :
- Have my basic UI setup in one add-on, which I had working - but then I made the feverish decision to redo the entire thing.
-- I was going to have it modular again, as it was. -- this is no longer working of course.
-- I wanted to have my core.lua contain the event handlers and utility functions

core.lua is setup and works, however, I seem to have lost the "module" effect, and I can't seem to figure it out. I looked at various other add ons written by people whom I greatly admire - but I just am not seeing the light. Not when it comes to the modular nature, not when it comes to event handling.

It's set up as follows :
Code:

Eavu.toc
cvars.lua -- just cvar edits and setting the UI scale, is actually self sufficient
Eavu.lua -- contains the config, createPanel fund and event handler (which should have been usable by the modules)
modules/autorepair.lua -- works when I include the frame:SetScript(event, method), when I try to get it to fall back on the event handler of eavu.lua - it won't work. I'm also calling a function Eavu.print() which is defined in eavu.lua - but alas.
modules/panels.lua -- works again
modules/broker.lua -- uses LibCargoShip, doesn't use anything of eavu, positioning is based on panels.lua, so that it can read fine
modules/.... -- haven't gotten to the others

for those interested: https://github.com/moonwitch/Eavu

Resike 12-06-13 02:16 PM

You need to register events before you can trigger them:

Lua Code:
  1. for k, v in pairs(Eavu) do
  2.  frame:RegisterEvent(k); -- Register all events for which handlers have been defined
  3. end
  4.  
  5. frame:SetScript("OnEvent", function(self, event, ...)
  6.  Eavu[event](self, ...); -- call one of the functions above
  7. end)

I think coding when you're sick is a bad idea, i tried do "programming" once i was drunk, thex next day was a massive omg wtf rollback. :)

However that table should contain the events you want to register:

Lua Code:
  1. local Events = {
  2.  [1] = "ADDON_LOADED",
  3.  [2] = "COMBAT_LOG_EVENT_UNFILTERED",
  4.  [3] = "ETCETC"
  5. }
  6.  
  7. for k, v in pairs(Events) do
  8.  frame:RegisterEvent(v); -- Register all events for which handlers have been defined
  9. end

Also if you use the in pairs loop then you need to register it's v part not the k. (But it depends how your table looks like.)

MoonWitch 12-06-13 02:29 PM

the only good things about this coding when sick area. I keep losing my train of thought.
b. I am not afraid to delete ENTIRE files (yep, I am missing half an add-on)
and especially c. I actually use github, and "gitted" almost everything.


I keep wondering though :
Lua Code:
  1. local __, Inomena = ...
I think this is : Set _ to the addonname (uhm ok) and Inomena gets made into a table.

Frame handler created just to deal with all the OnEvents
Lua Code:
  1. local handler = CreateFrame('Frame') -- creating frame, nothing special
  2. handler:SetScript('OnEvent', function(self, event, ...) -- on an event, the event name is the function name
  3. self[event](...) -- execute the function which has the same name as the event which fires the function
  4. end)

But then this mastodont .. just wild
Lua Code:
  1. local metatable = { -- create metatable
  2. __call = function(funcs, self, ...) -- no clue on __call
  3. for __, func in pairs (funcs) do
  4. func(self, ...)
  5. end
  6. end
  7. }

There has got to be an easier way that I *can* understand :P
But I am watching too many events to just do each one separately, all I wanted was to find that one "event handler" that I can drop into my eavu.lua (is loaded first btw) and then use Eavu.EVENT() in the other modules.

MoonWitch 12-06-13 02:33 PM

Quote:

Originally Posted by Resike (Post 287813)
However that table should contain the events you want to register:

Lua Code:
  1. local Events = {
  2.  [1] = "ADDON_LOADED",
  3.  [2] = "COMBAT_LOG_EVENT_UNFILTERED",
  4.  [3] = "ETCETC"
  5. }
  6.  
  7. for k, v in pairs(Events) do
  8.  frame:RegisterEvent(v); -- Register all events for which handlers have been defined
  9. end

Also if you use the in pairs loop then you need to register it's v part not the k.

Thanks :)

I actually declare my functions like so (Only the Eavu.Print doesn't work, simply because - well the modular aspect got lost somehow. You see how I have to redo the event handler again, even though this is a module?
lua Code:
  1. local __, Eavu = ...
  2. local frame = CreateFrame("Frame")-- sets frame to be used for the hooks, and Eavu to a usable table
  3.  
  4.  
  5. --------------------------------------------
  6. -- Util funcs
  7. --------------------------------------------
  8. function money_to_string(value)
  9.   if value == 0 then return nil end
  10.  
  11.  
  12.   local gold = math.floor(value / 10000)
  13.   local silver = mod(math.floor(value / 100), 100)
  14.   local copper = mod(value, 100)
  15.  
  16.  
  17.   return string.format("|cffffd700%i|r.|cffc7c7cf%02i|r.|cffeda55f%02i|r", gold, silver, copper)
  18. end
  19.  
  20.  
  21. function earned(value)
  22.   return value > 0 and "|cff00ff00Profit|r" or value < 0 and "|cffff0000Loss|r" or nil
  23. end
  24.  
  25.  
  26. function Eavu.MERCHANT_SHOW()
  27. -- Func to AutoRepair from guild if you can repair from guild and haven't exceeded the total amount
  28.   if (CanMerchantRepair()) then -- can we repair from this dude?
  29.     RepairAllItems(CanGuildBankRepair() and GetGuildBankWithdrawMoney() >= GetRepairAllCost())
  30.     Eavu.print('Repaired for:'..GetRepairAllCost())
  31.   end
  32. -- Func to vendor all greys and print out for how much
  33.  
  34.  
  35.   self:UnregisterEvent("MERCHANT_SHOW") -- Unregistering the correct event as well
  36. end
  37.  
  38.  
  39. frame:SetScript("OnEvent", function(self, event, ...)
  40.  Eavu[event](self, ...); -- call one of the functions above
  41. end)
  42.  
  43.  
  44. for k, v in pairs(Eavu) do
  45.  frame:RegisterEvent(k); -- Register all events for which handlers have been defined
  46. end

Rainrider 12-06-13 09:56 PM

What p3lim does is create his own implementation of RegisterEvent, where he can pass a handler function as a second argument to it. The code then checks whether a handler is already present for the passed event. If not, it creates a table entry with the event as the key and the handler as the value (the value is of type function). If there is already a handler, and the handler is a function, it changes the table entry so that the key is the event and the value is a new table containing the old and the new handler (so it is an array with 2 values, both are of type function) and sets his metatable to it (more on that below). If the handler is not of type function (it will then be a type table) it just injects the new handler into that table.

The __call metamethod allows a table to be called like a function and defines what the return of that should be. The first argument it gets is the table itself, followed by all the arguments passed in table call. In p3lim's case it tells it to execute all the handlers registered to the given event.

Torhal 12-07-13 02:20 AM

Quote:

Originally Posted by MoonWitch (Post 287809)
(Ok, I have the flu together with abdo flu, so I am feeling quite down.)

Go to bed. For the weekend. THEN look at code. Bleargh!

MoonWitch 12-07-13 08:30 AM

Quote:

Originally Posted by Torhal (Post 287824)
Go to bed. For the weekend. THEN look at code. Bleargh!

I should, but I feel guilty cuz mom's cleaning ;)

Also: Be nice to Kaelten - I've been stoned ranting about incoherent coding stuff to him for a day or two .... I think I've made him face palm quite a few times

Torhal 12-07-13 06:16 PM

That can be fun, though!

MoonWitch 12-07-13 09:04 PM

Quote:

Originally Posted by Torhal (Post 287844)
That can be fun, though!

He's a good guy... Clever too - won't admit it though. (At least not to me)


All times are GMT -6. The time now is 02:07 AM.

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