ULX

Author Topic: Combining two ULX commands into one!  (Read 2230 times)

0 Members and 3 Guests are viewing this topic.

Offline MGFear

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Combining two ULX commands into one!
« 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" )

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: Combining two ULX commands into one!
« Reply #1 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 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 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. :)
« Last Edit: December 29, 2016, 10:40:30 AM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MGFear

  • Newbie
  • *
  • Posts: 3
  • Karma: 0
Re: Combining two ULX commands into one!
« Reply #2 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?

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: Combining two ULX commands into one!
« Reply #3 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:

  • 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.
  • DrawMaterialOverlay is not a method of Player or Entity, so you can't use the colon syntax with it.
  • 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 ULib.clientRPC( plys, fn, ... ).
Alternatively you could use the Net Library, but that's significantly more complicated.
« Last Edit: December 29, 2016, 12:16:31 PM by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.