WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   First time addon author, not sure why my code isn't working (https://www.wowinterface.com/forums/showthread.php?t=56860)

bonksnp 11-23-18 11:01 PM

First time addon author, not sure why my code isn't working
 
hello, I am a senior in high school doing an independent study program with my CS teacher, which basically lets us choose a project that will both look good on a future resume and further our knowledge in computer science. I chose to write an addon for wow, which would basically be a very simple DK interface to replace the blizzard one (the concept isn't to make something revolutionary or completely new, it's just to make something that you can say you did yourself without just copying someone elses' code). It's supposed to track diseases, runic power, and the cooldown on the 6 runes.

I did a fair amount of it on my own, but since this is the first project I'm doing in Lua my teacher had to help in a few places (the OOP-simulating was her idea), but after that I should be able to do everything myself.

I'm getting an error: line 208 attempt to call field 'constructor' (a nil value), not really sure why this is happening. does anyone know?

code is here https://pastebin.com/MrZAaJNU

apologies if it's not the best, I only started Lua in September and I've only been coding for a few years :P

myrroddin 11-24-18 12:29 AM

You never defined constructor as file scope, so it doesn't exist on line 208. I think you meant to type either self.constructor or framesConstructor. I didn't really pay enough attention to make sure which one to use, sorry.

Also, table.getn was depreciated a few years ago. use # instead, like
Code:

local table_length = #MyTable

Fizzlemizz 11-24-18 01:13 AM

Code:

framesConstructor:create
Create is a method/function of framesConstructor
Code:

self.constructor = constructor
Same as
Code:

framesConstructor.constructor = constructor
Code:

function framesConstructor:create(name, width, height, constructor, updater)--method for frame creation - simulates OOP
        local tbl = {}
        tbl.frame = CreateFrame("Frame", name .. "BackgroundFrame", mainFrame)
        tbl.name = name
        tbl.height = height
        tbl.width = width
        tbl.constructor = constructor
        tbl.updater = updater
        return tbl
end


bonksnp 11-24-18 01:56 AM

Quote:

Originally Posted by myrroddin (Post 330912)
You never defined constructor as file scope, so it doesn't exist on line 208. I think you meant to type either self.constructor or framesConstructor. I didn't really pay enough attention to make sure which one to use, sorry.

Also, table.getn was depreciated a few years ago. use # instead, like
Code:

local table_length = #MyTable

I didn't know about table.getn, thanks!

The intent by using allFrames instead of framesConstructer was to simulate object oriented programming.


I wanted to do this with
Code:

        allFrames[#allFrames + 1] = framesConstructor:create("runicpower", 120, 16, setupRpower, updateRpower)
        allFrames[#allFrames + 1] = framesConstructor:create("runes", 120, 12, setupRunes, updateRunes)
        allFrames[#allFrames + 1] = framesConstructor:create("diseases", 64, 64, setupDis, updateDis)

The idea was to have each index in allFrames contain an 'object' that could be referenced back to. Sort of like object.constructor(), but instead of object it's just allFrames[i] (in the for loops).

Fizzlemizz 11-24-18 02:06 PM

Lua Code:
  1. local allFrames = {}
  2. local framesConstructor = {}
  3. function framesConstructor:create(name, width, height, constructor, updater)--method for frame creation - simulates OOP
  4.     self.frame = CreateFrame("Frame", name .. "BackgroundFrame", mainFrame)
  5.     self.name = name
  6.     self.height = height
  7.     self.width = width
  8.     self.constructor = constructor
  9.     self.updater = updater
  10.     return self
  11. end
  12.  
  13. function framesConstructor:create2(name, width, height, constructor, updater)--method for frame creation - simulates OOP
  14.     local tbl = {}
  15.     tbl.frame = CreateFrame("Frame", name .. "BackgroundFrame", mainFrame)
  16.     tbl.name = name
  17.     tbl.height = height
  18.     tbl.width = width
  19.     tbl.constructor = constructor
  20.     tbl.updater = updater
  21.     return tbl
  22. end
  23.  
  24. tinsert(allFrames, framesConstructor:create("runicpower", 120, 16, "setupRpower", "updateRpower"))
  25. tinsert(allFrames, framesConstructor:create("runes", 120, 12, "setupRunes", "updateRunes"))
  26. tinsert(allFrames, framesConstructor:create("diseases", 64, 64, "setupDis", "updateDis"))
  27.  
  28. tinsert(allFrames, framesConstructor:create2("runicpower", 120, 16, "setupRpower", "updateRpower"))
  29. tinsert(allFrames, framesConstructor:create2("runes", 120, 12, "setupRunes", "updateRunes"))
  30. tinsert(allFrames, framesConstructor:create2("diseases", 64, 64, "setupDis", "updateDis"))
  31.  
  32. for i=1, #allFrames do
  33.     print(allFrames[i].constructor)
  34. end

The first 3 tinserts replicate what you are doing and in the print, they all end up containing the information of the last call to create() because "return self" means return framesConstructor (always returning the same table, not a new instance). The last 3 create/return a new table each call containing the passed information.

If you wanted an inherited "instance" of framesConstructor for each entry:

Lua Code:
  1. tinsert(allFrames, Mixin({}, framesConstructor))
  2. allFrames[#allFrames]:create("runicpower", 120, 16, "setupRpower", "updateRpower")
  3.  
  4. tinsert(allFrames, Mixin({}, framesConstructor))
  5. allFrames[#allFrames]:create("runes", 120, 12, "setupRunes", "updateRunes")
  6.  
  7. tinsert(allFrames, Mixin({}, framesConstructor))
  8. allFrames[#allFrames]:create("diseases", 64, 64, "setupDis", "updateDis")

bonksnp 11-24-18 08:26 PM

Fizzle, thanks! I guess I missed that. There are no more errors, and it prints the 3 setup lines (runic power setup/runes setup/diseases setup) but now the problem is that there's nothing actually showing on the screen.. I thought "allFrames[i].frame:Show()" would show all 3 of them.

Fizzlemizz 11-24-18 08:30 PM

By default, frames are shown.

Is your latest code on PasteBin?

Xrystal 11-24-18 08:38 PM

The only time I haven't had my frames showing is when I have either forgotten to specify a size when it needs it or anchors or told it to hide but forgot to change it or that it was supposed to be like that.

If nothing else surfaces as a reason you might want to make sure that that isn't your problem.

Fizzlemizz 11-24-18 08:45 PM

In framesConstructor:create
.frame has no SetPoint()

It appears everything else is anchored to it

bonksnp 11-24-18 09:54 PM

Quote:

Originally Posted by Fizzlemizz (Post 330921)
In framesConstructor:create
.frame has no SetPoint()

It appears everything else is anchored to it

Whoops! Added.

Quote:

Originally Posted by Xrystal (Post 330920)
The only time I haven't had my frames showing is when I have either forgotten to specify a size when it needs it or anchors or told it to hide but forgot to change it or that it was supposed to be like that.

If nothing else surfaces as a reason you might want to make sure that that isn't your problem.

This was what I was missing! I updated the code here:https://pastebin.com/NyrWB9K6

Thanks a ton guys! There are still a few things missing that need to be worked out but I think I'll be able to manage them myself! Thank you again:)


All times are GMT -6. The time now is 08:47 PM.

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