LibStub Question

I have a set of 2 files I want to combine into a single library. I’m trying to get this to work using LibStub and I think I’m close. But programming is not like hand grenades. Close doesn’t count. I’ll try to keep this concise. Here’s the setup:

My library, FooBar-1.0, resides in my addon’s Lib directory:

Lib\embeds.xml
Lib\LibStub\LibStub.lua
Lib\FooBar-1.0\Foo.lua
Lib\FooBar-1.0\Bar.lua

embeds.xml loads LibStub.lua and the two libraryfiles, Foo.lua and Bar.lua.

Foo.lua exports a single function,

foo:helloWorld() print( “Hello World!” ) end

Bar.lua also exports a single function,

bar:goodByeWorld() print(“Goodbye World!” ) end

In my test file I load the library using LibStub’s NewLibrary method

local foobar = LibStub:NewLibrary(“FooBar-1.0”, 1 )

What do I do next? Now that I’ve got foobar, how do I invoke the helloWord() and goodByeWorld() functions?

I apologize if this isn’t clear.

Probably the question here is “is this for use in just this addon or something you’re creating for other addons to use”?

If it’s “just this addon” then you better off using the addons private table:

The library is for use by other addons.

Cheers,

LibSub:NewLibrary(…) gives your library a table to use to “export” its functions.

LibSub(…) gives other addons the table your library added it’s function too.

With muliple .lua files in your librabry you would most likely use LibSub:NewLibrary in the first file loaded and LibSub in the others (to get the same table) or some internal method created in the initial file for adding extras.

Have you looked at the examples on the addon page?

https://www.curseforge.com/wow/addons/libstub

1 Like

embeds.xml loads LibStub.lua and the two libraryfiles, Foo.lua and Bar.lua.

but do you load Lib\embeds.xml in your mods toc file as thats what gets your library code loaded.

the two libraryfiles

if theyre two libraries (foo and bar) then libstub them separately, otherwise make them a single library so you can code them from a single object. otherwise its probably going to be a bit messier when it doesnt have to be.

ie dont use foo and bar as the “parent”, just pick one, and probably name it lib or the name of the actual library so its simpler to write and understand

What do I do next?

normally it would be foobar:function() but youve got foo and bar in there so probably foobar:foo.helloWorld() and foobar:bar.goodByeWorld() - but im not sure of the correct syntax - you see why it gets messy when it doesnt have to

although it really depends on how you defined foo and bar inside the files to actually know for sure, you may have done something that will make it work how most people expect a libstub library to work, or not.

youd have to post the full pseudo code to know for sure

1 Like

@elvenbane
Yes. I’ve looked at those and other apps

@Arkayenro
Now I see where I was confused. Evidently, the term “library” usually (or by convention) means one lua file. Given what I now realize, this makes perfect sense given LibStub’s design.

In my case I’m trying to convert my working standalone “library” to an embedded one. The challenge, as I now understand it, is my standalone library has multiple files, but only one of which exports methods available to other addons.

I suppose one solution might be to combine all the files into a single humongous lua file. Ugh!

But what about this: could I recode the subsidiary files (e.g., make all symbols and methods local) so that I could import them into the main file using “loadfile?”

Just spitballing, here.

The one file per library is convention, not concrete.

Again, LibStub:NewLibrary() creates a new table to use for your library functions.

LibStub() (or LibStub:GetLibrary()) returns that table (primarily for other addons to call your library functions). Because it’s the same table, you can also add additional function(s) to it from other addons or even other files in your library addon.

Your library file1.lua

local Major, Minor = "HanachashLib_V1", 1
local Lib = LibStub:NewLibrary(Major, Minor)

function Lib:Funtion1()
	print("Hello from Lib File 1")
end

Your library file2.lua

local Major = "HanachashLib_V1"
local Lib = LibStub(Major)

function Lib:Funtion2()
	print("Hello from Lib File 2")
end

Other addon.lua

local HanachashLib = "HanachashLib_V1"
local Lib = LibStub(HanachashLib)

Lib:Funtion1()
Lib:Funtion2()

The only rules here is that the first .lua file in your .toc/.xml file creates the NewLibrary table and addons using the library include all the files (in order…ish) in their .toc/exdended.xml files.

Got it. Thanks very much, each of you.

If you look at AceGui which has seperate widget files in a sub-folder you can see how they do essentially the same thing through the RegisterWidgetType() method.

Thanks, everyone. I just thought I’d close the loop. I’ve got it all working thanks to you all.

Cheers,