ULX

Author Topic: custom commands  (Read 30541 times)

0 Members and 1 Guest are viewing this topic.

Offline JackYack13

  • Newbie
  • *
  • Posts: 28
  • Karma: -1
custom commands
« on: November 11, 2011, 03:41:00 PM »
I'm creating a gamemode for my gmod server and I'm using ULX on it. Is there anyway to add my own custom commands to ULX? If yes, could I see an example of how it's done? Thanks.

To be more specific I'd like to make commands like give <player> <weapon> or givemoney <player> <amount> and make them work like standard ulx commands (display in ulx help, show usage info, etc.)
« Last Edit: November 11, 2011, 03:47:35 PM by JackYack13 »

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: custom commands
« Reply #1 on: November 11, 2011, 03:58:34 PM »
Yes it is. Even the stock ULX commands are added modularly.

Take a look at any of the pre-existing ulx modules for all the examples you could want. garrysmod/garrysmod/addons/ULX/lua/ulx/modules/sh


If you are making a gamemode, I would assume you aren't a novice so you should be able to figure it out from there. If you require any more help though, let us know.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: custom commands
« Reply #2 on: November 11, 2011, 06:50:34 PM »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JackYack13

  • Newbie
  • *
  • Posts: 28
  • Karma: -1
Re: custom commands
« Reply #3 on: November 12, 2011, 03:57:16 AM »
Thanks. And is there any way to make my commands appear when I type them in the console? I added a command "ulx give" but it doesn't appear on the list when I type for example "ulx g". Do I have to do something client-side as well?

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: custom commands
« Reply #4 on: November 12, 2011, 04:21:25 AM »
If you did it correctly it should be showing up. How did you add the command? Did you create your own file or append an existing ULX file? The prior is the preferred method.

Code: [Select]

function ulx.cloak( calling_ply, target_plys, amount, should_uncloak )
if not target_plys[ 1 ]:IsValid() then
Msg( "You are always invisible.\n" )
return
end

amount = 255 - amount

for _, v in ipairs( target_plys ) do
ULib.invisible( v, not should_uncloak, amount )
end

return true
end
local cloak = ulx.command( CATEGORY_NAME, "ulx cloak", ulx.cloak, "!cloak" )
cloak:addParam{ type=ULib.cmds.PlayersArg, ULib.cmds.optional }
cloak:addParam{ type=ULib.cmds.NumArg, min=0, max=255, default=255, hint="amount", ULib.cmds.round, ULib.cmds.optional }
cloak:addParam{ type=ULib.cmds.BoolArg, invisible=true }
cloak:defaultAccess( ULib.ACCESS_ADMIN )
cloak:help( "Cloaks target(s)." )
cloak:logString( "#1s cloaked #2s by amount #3i" )
cloak:setOpposite( "ulx uncloak", {_, _, _, true}, "!uncloak" )
cloak:oppositeLogString( "#1s uncloaked #2s" )
ulx.addToMenu( ulx.ID_MCLIENT, "Cloak", "ulx cloak" )
ulx.addToMenu( ulx.ID_MCLIENT, "Uncloak", "ulx uncloak" )

Above is from fun.lua

Code: [Select]
cloak:addParam{ type=ULib.cmds.PlayersArg, ULib.cmds.optional }
cloak:addParam{ type=ULib.cmds.NumArg, min=0, max=255, default=255, hint="amount", ULib.cmds.round, ULib.cmds.optional }
cloak:addParam{ type=ULib.cmds.BoolArg, invisible=true }
The above is the code that adds the auto complete options to the console I believe.
« Last Edit: November 12, 2011, 04:24:38 AM by MrPresident »

Offline JackYack13

  • Newbie
  • *
  • Posts: 28
  • Karma: -1
Re: custom commands
« Reply #5 on: November 12, 2011, 05:21:33 AM »
I have this in cmd.lua:
Code: [Select]
function ulx.give(ply, targets, weap)
local affected_plys = {}

for i = 1, #targets do
local v = targets[i]
if not v:IsValid() then
print("You can't give the server console a weapon!.")
else
v:Give(weap)
table.insert( affected_plys, v )
end
end

ulx.fancyLogAdmin( ply, "#A gave #T #s.", affected_plys, weap )
end
local give = ulx.command("Awesome Build Server", "ulx give", ulx.give, "!give", false)
give:addParam{ type = ULib.cmds.PlayersArg }
give:addParam{ type = ULib.cmds.StringArg, hint = "name of weapon / item" }
give:defaultAccess(ULib.ACCESS_ADMIN)
give:help("Give the player a weapon / item.")

And this in init.lua:
Code: [Select]
include("cmd.lua")
When I type "ulx g" in the console, commands like "ulx gimp", "ulx gag", "ulx god", "ulx goto" appear, but there's no "ulx give".

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: custom commands
« Reply #6 on: November 12, 2011, 10:07:32 AM »
I know Megiddo and Stickly Man would swoop in here and push MrP and I off our feet, but, heck, let's see if we can help exercise our brains.
:)

Does the command run correctly, though not autocompleted in console? <-- big necessary answer. If ULX says "I have no idea what command that is", then ULX can't place it in autocomplete.
Where are you putting cmd.lua?
Where are you putting init.lua?
If cmd.lua is placed in the proper location (say, addons/my_addon/lua/ulx/modules/sh ), you shouldn't need an init.lua.
I have another idea, but lets see answers to those first.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JackYack13

  • Newbie
  • *
  • Posts: 28
  • Karma: -1
Re: custom commands
« Reply #7 on: November 12, 2011, 01:00:19 PM »
1. The command runs exactly like it should. The autocomplete thing just doesn't work.
2. cmd.lua is in gamemodes/<gamemode name>/gamemode
3. init.lua is in that folder too

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: custom commands
« Reply #8 on: November 12, 2011, 03:59:53 PM »
okay, are you sending that file to the client? The idea behind the ULX module files is that they are shared. For it to autofill it needs to be sent to the client. add that file to your init.lua the same way that you send cl_init.lua and shared.lua

If there is one thing that I could probably answer better than anyone else here is gamemode related quests. I'd like to think that is my pseudo specialty.

The best way would be to do it as JamminR suggests though. Add cmd.lua to the /sh/ folder so that way ULX takes care of making sure the client has the information it needs to populate the menus and autofills. I can understand keeping it in the gamemode if you have other NON ULX commands in that file as well, but maybe you should consider splitting your ULX from your NON ULX commands into 2 separate files.
« Last Edit: November 12, 2011, 04:02:57 PM by MrPresident »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: custom commands
« Reply #9 on: November 12, 2011, 06:23:04 PM »
To add to MrPresident's splitting of files idea, it wouldn't be difficult to place both files in addons/my_ulx_addon/lua/ulx/modules/sh .. then in the file that you only want commands to run when in a particular gamemode, then use a if/then to check the gamemode.

You could do the same all in one file of course, but you might find it more difficult to separate the two later when you want to edit only the one or other.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline JackYack13

  • Newbie
  • *
  • Posts: 28
  • Karma: -1
Re: custom commands
« Reply #10 on: November 13, 2011, 10:58:12 AM »
I think I got it working at last. I kept all files in the gamemode folder and I simply created another file cl_cmd.lua and included it in cl_init.lua:
Code: [Select]
local give = ulx.command("Awesome Build Server", "ulx give", nil, "!give", false)
give:addParam{ type = ULib.cmds.PlayersArg }
give:addParam{ type = ULib.cmds.StringArg, hint = "name of weapon / item" }
give:defaultAccess(ULib.ACCESS_ADMIN)
give:help("Give the player a weapon / item.")

Maybe this is not the best way to do this but for some reason I like it the most :P

Anyways thank you all for helping me :)

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: custom commands
« Reply #11 on: November 20, 2011, 02:30:41 PM »
I think I got it working at last. I kept all files in the gamemode folder and I simply created another file cl_cmd.lua and included it in cl_init.lua:
Code: [Select]
local give = ulx.command("Awesome Build Server", "ulx give", nil, "!give", false)
give:addParam{ type = ULib.cmds.PlayersArg }
give:addParam{ type = ULib.cmds.StringArg, hint = "name of weapon / item" }
give:defaultAccess(ULib.ACCESS_ADMIN)
give:help("Give the player a weapon / item.")

Maybe this is not the best way to do this but for some reason I like it the most :P

Anyways thank you all for helping me :)

Your method is fine. I stuck the whole source files in the shared directories mostly out of laziness and the fact that sharing extraneous code with clients doesn't take much of a penalty hit anymore since Garry sends the files zipped.

Nice catch Mr. P, you were spot on with saying it needed to share that particular code with the client. :)
Experiencing God's grace one day at a time.

Offline Ty-combat

  • Newbie
  • *
  • Posts: 4
  • Karma: -2
Re: custom commands
« Reply #12 on: August 10, 2016, 06:33:59 PM »
Hello, i notice some command's does not work? 

Offline Undercover Orange

  • Full Member
  • ***
  • Posts: 139
  • Karma: -14
  • Leader of Undercover Gaming Community
Re: custom commands
« Reply #13 on: August 11, 2016, 11:18:31 AM »
Hello, i notice some command's does not work?
you should make a new post on the general chat &help and support tab. make sure to explain what commands dont work. what version of ulx and ulib you are using and what game mode your server is on.
~ Undercover Orange