Ulysses
General => Developers Corner => Topic started by: kwinkywolf 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.
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." )
-
What help are you needing? Does your command run? If not, what errors?
-
The command doesn't work, and doesn't appear in the menu. There's absolutely no errors, as if I didn't add it.
-
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
-
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...
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...
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
-
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.
-
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 :/
-
attempt to call method 'GetPData' (a nil value)
Also take a closer look at target_ply. Use functions like type (https://wiki.garrysmod.com/page/Global/type), print (https://wiki.garrysmod.com/page/Global/type) and PrintTable (https://wiki.garrysmod.com/page/Global/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 (https://ulyssesmod.net/docs/files/lua/ulib/shared/commands-lua.html#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.
for i=1, #target_plys do
target_plys[ i ]:SetPData( 12 )
end
Alternatively, use cmds.PlayerArg (https://ulyssesmod.net/docs/files/lua/ulib/shared/commands-lua.html#cmds.PlayerArg) if your command should only target a single player. There is no need to loop when this parameter type is used.
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?
-
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.
-
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!
-
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
-
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
-
Woop! Awesome, thanks man that's done the trick. An absolute life saver