Ulysses
General => Developers Corner => Topic started by: MGFear on December 29, 2016, 07:32:26 AM
-
Hi, I made a custom command on ulx for my server and I need it combined so whenever I run the command one somebody he gets X amount of HP
here is my code:
function ulx.warlock ( calling_ply, target_plys)
target_ply:DrawMaterialOverlay( "effects/combine_binocoverlay")
ulx.fancyLogAdmin( calling_ply, "#A turned #T into the warlock!", target_ply)
end
local warlock = ulx.command( CATEGORY_NAME, "ulx warlock", ulx.warlock, "!warlock",true)
warlock:addParam{ type=ULib.cmds.PlayersArg }
warlock:defaultAccess( ULib.ACCESS_ADMIN )
warlock:help( "turn somebody into the warlock" )
-
There are multiple ways, but normally the simplest way to call another lua function is to just call the name of the function directly.
function my_function(calling_ply, target_ply)
... some_code ...
ulx.hp(calling_ply, target_ply, 200)
end
The name of the ULX hp function is 'ulx.hp()', and expects the same parameters be passed as your function (calling player, target playerS, and the amount of hp.
Here's the code for it at line 296 (as of this posting)
https://github.com/TeamUlysses/ulx/blob/master/lua/ulx/modules/sh/fun.lua#L296
You _could_ call "ulx hp" using (http://wiki.garrysmod.com/favicon.ico) RunConsoleCommand (http://wiki.garrysmod.com/page/Global/RunConsoleCommand), but that adds the additional ULX checks that are likely not necessary in your situation, and could cause complication depending on the scope of the call.
All that being said, You don't need to use ulx.hp function. SetHealth is a standard Gmod function.
It's not special. See (http://wiki.garrysmod.com/favicon.ico) Entity:SetHealth (http://wiki.garrysmod.com/page/Entity/SetHealth)
And, the command you wrote will likely not work "as is" right now, even without trying to add HP.
You're using ULib's PlayerSArg in your command setup,
warlock:addParam{ type=ULib.cmds.PlayersArg }
and your function is being passed a table, even if it contains only one player.
You then try to treat your drawmaterialoverlay as a single player entity (even the variable name target_player is single/treated as one, and no, renaming it players won't fix it.)
You have a design decision - update the function to allow multiple targets (see the 'for' loop in the ulx.hp for how to do this), or, make it a single target using type=ULib.cmds.PlayerArg (no s) in your command setup; and then just you don't even have to use ulx.hp, you can use use target_ply:SetHealth(#)
My personal recommendation/opinion, don't even call ulx hp function, just make yours accept multiple targets using a for loop, adding the overlay and sethealth on each index.
Then your challenge is figuring out how to turn off warlock. :)
-
-----
fml.. I still dont know how to make this work
function ulx.warlock ( calling_ply, target_plys)
target_plys:DrawMaterialOverlay("effects/combine_binocoverlay", -0.06)
ulx.fancyLogAdmin( calling_ply, "#A turned #T into the warlock!", target_ply)
ulx.hp(calling_ply, target_ply, 200)
end
local warlock = ulx.command( CATEGORY_NAME, "ulx warlock", ulx.warlock, "!warlock",true)
warlock:addParam{ type=ULib.cmds.PlayersArg }
warlock:defaultAccess( ULib.ACCESS_ADMIN )
warlock:help( "turn somebody into the warlock" )
to what I understood, my code should look like this now?
-
fml.. I still dont know how to make this work
function ulx.warlock ( calling_ply, target_plys)
target_plys:DrawMaterialOverlay("effects/combine_binocoverlay", -0.06)
ulx.fancyLogAdmin( calling_ply, "#A turned #T into the warlock!", target_ply)
ulx.hp(calling_ply, target_ply, 200)
end
local warlock = ulx.command( CATEGORY_NAME, "ulx warlock", ulx.warlock, "!warlock",true)
warlock:addParam{ type=ULib.cmds.PlayersArg }
warlock:defaultAccess( ULib.ACCESS_ADMIN )
warlock:help( "turn somebody into the warlock" )
to what I understood, my code should look like this now?
A few things:
- You're still using ULib.cmds.PlayersArg. If you use this, target_plys will be a table. As JamminR say, you'll either need a for loop to handle that or you need to switch to ULib.cmds.PlayerArg.
- (http://wiki.garrysmod.com/favicon.ico) DrawMaterialOverlay (http://wiki.garrysmod.com/page/Global/DrawMaterialOverlay) is not a method of Player or Entity, so you can't use the colon syntax with it.
- (http://wiki.garrysmod.com/favicon.ico) DrawMaterialOverlay (http://wiki.garrysmod.com/page/Global/DrawMaterialOverlay) is a clientside function, so running it on the server will have no effect and will probably throw an error.
To use DrawMaterialOverlay you're going to need a clientside lua file with a function. You can then call this function with (http://ulyssesmod.net/favicon.ico) ULib.clientRPC( plys, fn, ... ) (http://ulyssesmod.net/docs/files/lua/ulib/server/util-lua.html#clientRPC).
Alternatively you could use the (http://wiki.garrysmod.com/favicon.ico) Net Library (http://wiki.garrysmod.com/page/Net_Library_Usage), but that's significantly more complicated.