Ulysses

General => Developers Corner => Topic started by: MGFear on December 29, 2016, 07:32:26 AM

Title: Combining two ULX commands into one!
Post 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:

Code: [Select]
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" )
Title: Re: Combining two ULX commands into one!
Post by: JamminR on December 29, 2016, 10:26:50 AM
There are multiple ways, but normally the simplest way to call another lua function is to just call the name of the function directly.
Code: [Select]
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,
Code: [Select]
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. :)
Title: Re: Combining two ULX commands into one!
Post by: MGFear on December 29, 2016, 10:55:04 AM
-----

fml.. I still dont know how to make this work

Code: [Select]
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?
Title: Re: Combining two ULX commands into one!
Post by: roastchicken on December 29, 2016, 12:12:34 PM
fml.. I still dont know how to make this work

Code: [Select]
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:


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.