Help with scripy im making!

Im learning lua for my server in garrys mod. I got help already from a friend for making a custom spawnmenu. Its a little hard to understand what was going on because he never told me what it all meant.

So here it is:

//cl_init.lua


hook.Add( "InitPostEntity", "custompropwhitelist", function()
    function GAMEMODE:PopulatePropMenu()
        for k,v in pairs( PROPSALLOWED ) do
            local proptable = {}
            for k1,v1 in pairs( v ) do
                proptable[#proptable + 1] = {type = "model", model = v1}
            end
            spawnmenu.AddPropCategory( "customprops_" .. string.lower( k ), k, proptable )
        end
    end
end )


//

//cl_list.lua


PROPSALLOWED = {
    Building = {
"models/props_phx/construct/metal_plate1.mdl",
"models/props_phx/construct/metal_plate1x2.mdl",
--and so on but yes the script works.

//

What i need help with is how it works. And how i can get a custom Tool list also.

Thanks!

Why didn’t you ask your friend to begin with?

I think I’ll say how this works, but I don’t know about custom tool lists. Do you mean STools or just a tool list rather than a model list?



hook.Add( "InitPostEntity", "custompropwhitelist", function()


This creates a hook (if you do not know what that is, a simple google search should answer that) that gets activated once the event InitPostEntity (probably something like when entities finish initializing).



function GAMEMODE:PopulatePropMenu()


The gamemode is just whatever the current gamemode is, and PopulatePropMenu would just mean adding something new to the menu of this gamemode.



for k,v in pairs( PROPSALLOWED ) do 


This gets the PROPSALLOWED table (that you have in cl_list.lua) and does a cycle through all the values of the table, ‘v’ meaning whatever value is of the certain key of the table, and the key number being ‘k’.



local proptable = {}


This creates a new empty table called ‘proptable’. ‘Local’ means the table will be local to this hook (I think). If you wanted to access the table OUTSIDE the hook, you would simply take away ‘local’, but this would set it to be global, meaning ANY script can access it, which may cause conflicts with other addons.



for k1,v1 in pairs( v ) do 


Since this script is already in a k,v loop, your friend used ‘k1’ and ‘v1’ instead so the values do not get replaced, which would cause a lot of messing up.



proptable[#proptable + 1] = {type = "model", model = v1}


This accesses the ‘proptable’ table, and sets the value of a key in it, the part in brackets is the keyvalue number, which would get 1 added to it, and this would happen until the table is complete. Your friend used a table because the spawnmenu.AddPropCategory command seems to require two values, the type of the value and the model path.



spawnmenu.AddPropCategory( "customprops_" .. string.lower( k ), k, proptable )


Finally the model actually gets added to the list.

I’m no Lua expert, so there are probably some errors in what I’ve said above.

Why is he adding a hook in a hook? Why is he then copying a table into a new table when he could just create the first one differently?

Don’t learn lua from this guy, he doesn’t know what he is doing.

It’s probably in Addon format ( which are loaded prior to GM being initialized, meaning if you want to add a new hook you would need to add it in a hook ). The issue is, though, that PopulatePropMenu is a SANDBOX hook, so overwriting it in that format makes no sense when it could’ve easily been added via hook.Add without the “hack”…

Also, using for k, v in pairs allowed, for kk, vv in pairs x … is n^2 ( exponential growth function ) so as the lists get larger, you’re exponentially increasing the work needed to be performed ( not ideal when better alternatives exist, and they do ). Better would be, if ( !PROPS_ALLOWED[ v:GetClass( ) ] ) then continue; end in the primary loop…

See how HUDShouldDraw is done here: https://dl.dropboxusercontent.com/u/26074909/tutoring/hud/proper_hud_creation.lua.html - alter your table to match…

Same format can be used…

For anyone who rated my post dumb- I didn’t write this script -_- I’m just guessing.