Thread Tools Display Modes
11-23-18, 11:01 PM   #1
bonksnp
A Murloc Raider
Join Date: Nov 2018
Posts: 4
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

Last edited by bonksnp : 11-23-18 at 11:10 PM.
  Reply With Quote
11-24-18, 12:29 AM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,240
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
  Reply With Quote
11-24-18, 01:56 AM   #3
bonksnp
A Murloc Raider
Join Date: Nov 2018
Posts: 4
Originally Posted by myrroddin View Post
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).
  Reply With Quote
11-24-18, 01:13 AM   #4
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
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
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-24-18 at 01:38 AM.
  Reply With Quote
11-24-18, 02:06 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
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")
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-24-18 at 03:55 PM.
  Reply With Quote
11-24-18, 08:26 PM   #6
bonksnp
A Murloc Raider
Join Date: Nov 2018
Posts: 4
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.
  Reply With Quote
11-24-18, 08:30 PM   #7
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
By default, frames are shown.

Is your latest code on PasteBin?
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-24-18 at 08:35 PM.
  Reply With Quote
11-24-18, 08:38 PM   #8
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
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.
__________________
  Reply With Quote
11-24-18, 08:45 PM   #9
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
In framesConstructor:create
.frame has no SetPoint()

It appears everything else is anchored to it
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 11-24-18 at 09:05 PM.
  Reply With Quote
11-24-18, 09:54 PM   #10
bonksnp
A Murloc Raider
Join Date: Nov 2018
Posts: 4
Originally Posted by Fizzlemizz View Post
In framesConstructor:create
.frame has no SetPoint()

It appears everything else is anchored to it
Whoops! Added.

Originally Posted by Xrystal View Post
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
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » First time addon author, not sure why my code isn't working

Thread Tools
Display Modes

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