General > Developers Corner

Restrict xgui ConCommand

<< < (2/2)

MrPresident:
Now... let's say you're working within a ULX command structure.. which you probably are (maybe?)

Take a look at the code for the !slap command


--- Code: --------------------------------- Slap ------------------------------
function ulx.slap( calling_ply, target_plys, dmg )
local affected_plys = {}

for i=1, #target_plys do
local v = target_plys[ i ]
if v:IsFrozen() then
ULib.tsayError( calling_ply, v:Nick() .. " is frozen!", true )
else
ULib.slap( v, dmg )
table.insert( affected_plys, v )
end
end

ulx.fancyLogAdmin( calling_ply, "#A slapped #T with #i damage", affected_plys, dmg )
end

local slap = ulx.command( CATEGORY_NAME, "ulx slap", ulx.slap, "!slap" )
slap:addParam{ type=ULib.cmds.PlayersArg }
slap:addParam{ type=ULib.cmds.NumArg, min=0, default=0, hint="damage", ULib.cmds.optional, ULib.cmds.round }
slap:defaultAccess( ULib.ACCESS_ADMIN )
slap:help( "Slaps target(s) with given damage." )

--- End code ---

notice how at the top where the function is defined, there are variable names passed into it. calling_ply, target_plys and dmg

In the code calling_ply is the one who RAN the command.. so if you wanted to check if that player was a superadmin.. you would use calling_ply:IsSuperAdmin() not ply:IsSuperAdmin().. that's because the player object passed into the function is calling_ply

the target_plys variable is a table of player objects.. further in the code you'll notice how we're iterating through that table with a for loop. In that case.. the player being checked in the loop is 'v'.



I know this is a bit confusing if you're not used to programming, but I think it is pretty straight forward.

let me know if you have any more questions.

Darkblizzard:
Oh no, I understand how that works, and actually made some code myself using that. What I don't understand is where to get "ply" when working in the xgui client? The xgui.open function does not have "ply", and it appears like all the functions are rather custom which makes me nervous about breaking any of the code within.

MrPresident:
XGUI is clientside.. so ply would just be LocalPlayer()

Darkblizzard:
Thanks for the help!

Navigation

[0] Message Index

[*] Previous page

Go to full version