Author Topic: Help with custom ULX command.  (Read 3788 times)

0 Members and 1 Guest are viewing this topic.

Offline kwinkywolf

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Help with custom ULX command.
« on: July 01, 2018, 01:28:11 PM »
Hello to whomever may be reading this and thank you,

I can't seem to figure out how to set up custom ulx commands.
I'm working on an XP/Credit shop system with levelling and perks and so on, and I want to be able to set, add, remove and do other things with credits, xp and levels by using ULX considering I thought it would be easier than making my own arguments in a chat command.

The code below is all I have so far for the ULX stuff, i've simply put it on the bottom of the serverside script I have running, could someone please share some advice? I understand it's documented, but I can barely find a decent documentation for someone brand new to the idea of Ulib and ULX.

Code: [Select]
local CATEGORY_NAME = "Combine F3"

function ulx.setxp( calling_ply, target_ply, xp )

    local oldxp = target_ply:GetPData("XP", 0)
    local newxp = oldxp + xp

    target_ply:SetPData("XP", newxp)

ulx.fancyLogAdmin( calling_ply, "#A set #T's xp to #i", target_ply, xp )
end

local setxp = ulx.command( CATEGORY_NAME, "ulx setxp", ulx.setxp, "!setxp" )
setxp:addParam{ type=ULib.cmds.PlayersArg }
setxp:addParam{ type=ULib.cmds.NumArg, min=0, default=0, hint="xp", ULib.cmds.round }
setxp:defaultAccess( ULib.ACCESS_ADMIN )
setxp:help( "Sets target's XP to given amount." )

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Help with custom ULX command.
« Reply #1 on: July 01, 2018, 08:30:46 PM »
What help are you needing? Does your command run? If not, what errors?
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline kwinkywolf

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Help with custom ULX command.
« Reply #2 on: July 01, 2018, 11:49:45 PM »
The command doesn't work, and doesn't appear in the menu. There's absolutely no errors, as if I didn't add it.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Help with custom ULX command.
« Reply #3 on: July 02, 2018, 07:49:40 PM »
I see no reason this should not appear.
The most common, if you are including it in your own code, is if your files are executing before ULib and ULX have started.
An error during server startup, in server console, would show that.

You could guarantee ULib and ULX was operational/running by moving the code to a ulx modules folder within your own addon.
gmod/addons/your_addon_name/lua/ulx/modules/your_file.lua
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline kwinkywolf

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Help with custom ULX command.
« Reply #4 on: July 04, 2018, 08:52:36 PM »
Ok! Awesome, so the command now works and responds to me typing !setxp, however it doesn't show in the ULX Menu, and when I try to run the command it returns the following error in the console...

attempt to call method 'GetPData' (a nil value)[GGG]Kwinky Wolf: !setxp kwinky 5

Code on the line it's reporting to be the problem...
Code: [Select]
    local oldxp = target_ply:GetPData("XP", 0)
Now, i'm unsure as to why it's failing to get the data, but i'm convinced it's to do with it not being able to find the player. So I tried to use "" and type in my full name letter for letter, spaces included and caps and yet it still didn't work. Thought I may have needed to do something like a for loop to loop through all players, but that didn't work either...

Code: [Select]
    function ulx.setxp( calling_ply, target_ply, xp )

for k, v in pairs( player.GetAll() ) do
    if v = target_ply then

    local oldxp = v:GetPData("XP", 0)
    local newxp = oldxp + xp

    target_ply:SetPData("XP", newxp)

    end
end

    ulx.fancyLogAdmin( calling_ply, "#A set #T's xp to #i", target_ply, xp )
end
« Last Edit: July 04, 2018, 09:13:00 PM by kwinkywolf »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Help with custom ULX command.
« Reply #5 on: July 05, 2018, 08:35:02 PM »
Yoru original code should be fine.
Again, any errors that would keep it from appearing in the help menu, if your group truly is or inherits admin access, would show in server console at startup.

As for error on run of the command, you likely need to make sure of two things -
1) - Where the Pdata is stored - It likely should be retrieved from the 'server' sv.db file - so, make sure the code only executes on the server side.
2) - Ensure that "XP" exists (ie, not "xp" or "Xp"...). Lua is case sensitive.
« Last Edit: July 05, 2018, 08:37:28 PM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline kwinkywolf

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Help with custom ULX command.
« Reply #6 on: July 06, 2018, 08:23:50 AM »
Yeah, i've made sure XP exists... I set the data multiple times. I also havethe script running on the server now, and that doesn't work. And there's no errors at all, it simply isn't showing in the menu... So, i'm completely stumped :/

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Help with custom ULX command.
« Reply #7 on: July 06, 2018, 11:58:04 AM »
attempt to call method 'GetPData' (a nil value)
Also take a closer look at target_ply. Use functions like type, print and PrintTable to inspect the data inside. You will notice that target_ply is a table. And the player you targeted is inside that table.

You are currently using parameter type cmds.PlayersArg. This type is used when you want to be able to target multiple players. This parameter type will require you to loop through each player inside the returned table and call the desired method(s) on each Player object.
Code: [Select]
for i=1, #target_plys do
    target_plys[ i ]:SetPData( 12 )
end

Alternatively, use cmds.PlayerArg if your command should only target a single player. There is no need to loop when this parameter type is used.
Code: [Select]
target_ply:SetPData( 12 )
it simply isn't showing in the menu...
You can run a command that doesn’t show up in XGUI? That’s odd.
Open the developer console on your game client and run "ulx help". Is your command listed in the output?
« Last Edit: July 11, 2018, 12:05:47 AM by Timmy »

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Help with custom ULX command.
« Reply #8 on: July 06, 2018, 01:19:43 PM »
Thanks for reminding me Timmy - I've given the exact same answer several times over many years regarding target and PlayerArg vs Args, yet, I missed it now.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline kwinkywolf

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Help with custom ULX command.
« Reply #9 on: July 07, 2018, 12:09:15 PM »
Yep, I caught that literally last night haha! Such an easy mistake to make, and yet such a fatal one obviously. Thank you so much though, both of you!

Offline kwinkywolf

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Help with custom ULX command.
« Reply #10 on: July 07, 2018, 01:19:41 PM »
Yep, it returns right at the top of the help thing with the following when doing ulx help in console...


Category: Combine F3
   o ulx setcredits <player> <credits: 0<=x, default 0> - Sets the target's credit balance to the specified amount. (say: !setcredits)
   o ulx setlevel <player> <level: 0<=x, default 0> - Sets the target's level to the specified amount. (say: !setlevel)
   o ulx setxp <player> <xp: 0<=x, default 0> - Sets the target's XP to the specified amount. (say: !setxp)

So, i'm absolutely stumped as to why it isn't showing

Offline Timmy

  • Ulysses Team Member
  • Sr. Member
  • *****
  • Posts: 252
  • Karma: 168
  • Code monkey
Re: Help with custom ULX command.
« Reply #11 on: July 07, 2018, 02:43:44 PM »
Small things like that are easily overlooked! :P

The reason why your command does not appear in XGUI is because it was declared on the server side. To avoid any issues, make sure to put your commands in a shared file.

Update
gmod/addons/your_addon_name/lua/ulx/modules/your_file.lua
to
gmod/addons/your_addon_name/lua/ulx/modules/sh/your_file.lua

Offline kwinkywolf

  • Newbie
  • *
  • Posts: 8
  • Karma: 0
Re: Help with custom ULX command.
« Reply #12 on: July 08, 2018, 04:26:57 PM »
Woop! Awesome, thanks man that's done the trick. An absolute life saver