• Print

Author Topic: A ULX custom command  (Read 6214 times)

0 Members and 1 Guest are viewing this topic.

Offline Kitty³

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
A ULX custom command
« on: January 14, 2016, 12:51:05 pm »
Hi everyone.
I've been using ULX for quite some time now, but I've never actually made my own command for it.
I was wondering if anyone here knew how to, and if so, could help me.
Basically what I want to do is set up another MOTD sort of thing, except not an actual MOTD, but a little note that pops up when you say something like '!popup' in chat.
Thanks for your help!

Offline Caustic Soda-Senpai

  • Sr. Member
  • ****
  • Posts: 469
  • Karma: 54
  • <Insert something clever here>
    • Steam Page
Re: A ULX custom command
« Reply #1 on: January 14, 2016, 11:13:53 pm »
What exactly do you want? Just a notification with a message in it?
Once you get to know me, you'll find you'll have never met me at all.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: A ULX custom command
« Reply #2 on: January 15, 2016, 08:08:23 am »
A good way to figure out how to make commands is to look at the default ULX commands, located in garrysmod/addons/ulx/lua/modules/. All of the lua files in the cl and sh folders there contain commands, and if you look at them you should be able to figure out the general structure.

Here is the basic structure of a ULX command, if you can't figure it out from looking at the default commands.

Code: Lua
  1. function ulx.cmd( arguments ) --If you don't know basic programming, arguments are what are passed to the function. If you want to get input to the function, you use arguments.
  2.         --This is the body of the function. You write the code that you want to execute when the command is run in here.
  3. end
  4.  
  5. local cmd = ulx.command( CATEGORY_NAME, "ulx cmd", ulx.cmd, "!cmd" )
  6. --CATEGORY_NAME is a local variable defined in the default ULX command files, and just gives the name of the category where the command will be sorted.
  7. --"ulx cmd" is the console command to run the command, ulx.cmd is the function that contains the code for the function, and "!cmd" is the chat command.
  8. cmd:addParam{ type=ULib.cmds.PlayerArg } --for each argument in your function, you need to run addParam. It takes a table as its argument, containing different options for the argument.
  9. --I thought there was documentation for this, but I guess not. The addParam right under this gives some examples of the possible options.
  10. --Look at existing ULX commands to figure out what options are possible as well.
  11. cmd:addParam{ type=ULib.cmds.NumArg, min=0, default=0, hint="damage", ULib.cmds.optional, ULib.cmds.round }
  12. --type is what kind of argument it is. Possibilities are PlayerArg, StringArg, NumArg, and PlayersArg. Make sure you prefix these with ULib.cmds.
  13. --min is an option available for NumArgs, it makes sure that the supplied value is above a minimum.
  14. --default is an option available for NumArgs and I believe StringArgs as well. If no value is supplied, it will default to this.
  15. --optional specifies that the argument is optional, so people can run the command without supplying this value.
  16. --Beware that ULX cannot detect which is which, so you cannot have non-optional arguments after optional ones.
  17. --round just rounds the number to the nearest integer.
  18. cmd:defaultAccess( ULib.ACCESS_ADMIN ) --default access sets the default group(s) that have access to the command.
  19. --Possibilities are ULib.ACCESS_ALL, ULib.ACCESS_ADMIN, and ULib.ACCESS_SUPERADMIN
  20. cmd:help( "Help string" ) --Assigns the command a string that explains the command's usage.

On another note:

What happened to lua formatting? I miss it. The blue boxes are ugly :(.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: A ULX custom command
« Reply #3 on: January 18, 2016, 04:50:47 am »
Restored the GeSHi syntax highlighter -- we reinstalled and I just hadn't gotten around to re-installing it.
Experiencing God's grace one day at a time.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: A ULX custom command
« Reply #4 on: January 18, 2016, 06:02:54 am »
Thanks!

It looks different now; a bit smaller. I like it!
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

  • Print