Create derma and storing it inside a table?

Hello

I was wondering if Garry’s Mod has a way of recursively creating items like how C# does for example. In C# you can create a List as store things inside it (including your own classes). I was wondering if this was in anyway possible in Garry’s Mod’s Lua.

Here is my pseudo/lua thinking



myFrames = {}

for k,v in pairs(myTable) do
     local backFrame = vgui.Create( "DFrame" )
     backFrame:SetPos( ScrW() / 2 - 500, ScrH() / 2 - 300 )
     backFrame:SetSize( 1000, 600 )
     table.insert(myFrames, backFrame)
end


Would these still work, I am just confused, is there a better way to do this?

That pseudo code is what you actually do, and there’s not really a better way. However, to avoid table.insert and a separate table, you could do this:



local myFrames = {}

for i = 1, 10 do -- Add 10 frames
    local backFrame = vgui.Create( "DFrame" )
    backFrame:SetSize( 1000, 600 )
    backFrame:Center()
    myFrames[ i ] = backFrame -- Tables start at 1 in Lua
end


[editline]27th September 2017[/editline]

In Lua, things are simple, and somewhat limited. Tables are the only thing close to lists, arrays, dictionaries, sets, or whatever else they’re called in other languages. There’s also only one way to loop through them, and that’s with a for loop like you’re doing. Your code is about as equivalent as it can get.

There are a few other methods like while or until loops, but it’s easier and safer in my opinion to just do it normally.

Thanks for that, I will give it a go now and post my results :slight_smile:

Actually, will it still save DoClick functions if it was say a Derma Button?

vgui.Create makes a new metatable, which is a special kind of table that can have functions (such as DoClick).
Apparently these are called metamethods. They can be called using a colon, like panel functions use:



SomeMetatable:SomeMetamethod()


When you call vgui.Create, DoClick will be saved as part of the panel’s metatable.

I don’t really understand metatables myself, but there’s lots of tutorials, such as these:

[editline]27th September 2017[/editline]

Sorry, this makes no sense. I’m just trying to explain how it gets saved, but I don’t really know either.

Worked perfectly, it even saved the DoClick methods, thanks for your help.