A simple Derma Menu.

Hi, I was wondering, if someone could just write me a simple Derma Menu, that will be populated with buttons depending on a string array’s size. Each button’s text will be the text from the array. Also, once this is done, how and where do I use it? Does it need to be in the form of an addon? or can I just put it in the lua/autorun folder? Thanks.

Wiki.

Unhelpful.

Here’s an example of making buttons based on text length (you can tweak the numbers based on how it looks):

[lua]local function CreateButton( x, y, str, parent, func)
local btn = vgui.Create( “DButton”, parent )
btn:SetPos( x, y )
local width = string.len( str ) * 2 + 15
btn:SetSize( width, 25)
btn:SetText( str )
btn.DoClick = func
end
CreateButton( 0, 0, “This is a button!”, frame, function() print(“Hi”) end)
[/lua]

Thank-you, but how do I use this? do I put it in the: garrysmod/garrysmod/lua/autorun/ folder? This is where I need the most help with.

So once you’ve made your menu, you generally put it in the folder:
garrysmod/garrysmod/lua/autorun/client

And then you can run your script one of two ways:

A. go into singleplayer, or change the map if you’re already there
OR
B. type this into console
lua_openscript_cl autorun/client/<file name>.lua

Replacing <file name> with the name of your file, of course.

How would I go about making it run the script on a chat command? or something. Some kind of method that does not require “lua_openscript_cl” also, thanks for the reply.

lua_openscript_cl is a way of refreshing the script. You should always use that. It’s not just for running the script once.

For running on a chat command clientside, you would want to use the OnPlayerChat hook. Check out some of the documentation.

I know to use the OnPlayerChat hook, I mean, like, how would I build a script, that runs automatically upon server creation, and will draw the Derma menu.
Example:

In the Script, it will have the Derma Menu, someone types: !help
and the Menu pop’s up.

Here’s an example:



 local function CreateButton( x, y, str, parent, func)  
     local btn = vgui.Create( "DButton", parent )  
     btn:SetPos( x, y )  
     local width = string.len( str ) * 2 + 15  
     btn:SetSize( width, 25)  
     btn:SetText( str )  
     btn.DoClick = func  
 end  

function OnPlayerChat( player, strText, bTeamOnly, bPlayerIsDead )
 if string.lower(strText) == "!help" then
  CreateButton( 0, 0, "This is a button!", frame, function() print("Hi") end)
 end
end

hook.Add( "PlayerSay", "playerhastalked", OnPlayerChat )


If this does work, and I saved it as: test.lua, would I put this in the: garrysmod/garrysmod/lua/autorun/ folder? Last time I tried something like this, it gave me an error about ‘vgui’.

Perhaps I got ahead of myself. I would look at this example here:

to get a better understanding of derma.

Well, I do understand how Derma works, I’ve already looked at that example, what I NEED to know is how I would use it in the code as shown in my above post. It doesn’t seem like you are fully understanding my problem.

Well, I mean, I assumed that you could apply the example and understand that it needs an actual DFrame to be put in before you create the menu (hence the “parent” variable). But the idea is you have a function where you make the frame and the buttons. You call that function when the chat text == “!help”.

If you could, please, write me a simple lua script (and tell me where to place it) that when the player says: !help that it will show the derma menu. If you could do that, it would solve this problem.

This will help you for the derma stuff : http://www.facepunch.com/showthread.php?t=543184

Gah… That is very helpful, thanks, HOEVER, It’s still not telling me where to palce the .lua files… That is all I need to know now. If you could, please be ever so helpful. Thanks!