• UtilX -- Extending GLua -- Need community input
    371 replies, posted
I actually can't wait for this to be done :D.
[QUOTE=Deco Da Man;16621244]Pretty much everything from [url=http://penlight.luaforge.net/]Penlight[/url].[/QUOTE] I don't get what's dumb about this suggestion.
so was happenin
[lua]local ChatCommands = { } if SERVER then hook.Add( "PlayerSay", "UtilxChatCommandHook", function( ply, text, toall ) if string.Left( text, 1 ) != "/" then return end local args = string.Explode( " ", text ) local command = string.sub( args[ 1 ], 2 ) table.remove( args, 1 ) if ChatCommands[ command ] != nil then ChatCommands[ command ]( ply, args, toall ) end end) end if CLIENT then hook.Add( "OnPlayerChat", "UtilxChatCommandHook", function( ply, text, toall ) if string.Left( text, 1 ) != "/" or ply != LocalPlayer() then return end local args = string.Explode( " ", text ) local command = string.sub( args[ 1 ], 2 ) table.remove( args, 1 ) if ChatCommands[ command ] != nil then ChatCommands[ command ]( args, toall ) end end) end function utilx.AddChatCommand( command, func ) if type( command ) != "string" or type( func ) != "function" then return false end ChatCommands[ command ] = func return true end function utilx.RemoveChatCommand( command ) if ChatCommands[ command ] then ChatCommands[ command ] = nil return true end return false end[/lua] Usage: [lua]utilx.AddChatCommand( "kill", function( ply, args, toall ) ply:Kill() if toall == true then ply:ConCommand( "say"..table.concat( args, " " ) end end) utilx.RemoveChatCommand( "blah" ) [/lua]
You guys are way late: gmodplus already does this. Huge number of new functions: [url]http://code.google.com/p/gmodplus/[/url] It even takes functions directly from the Source SDK, and converts them to Lua equivalents.
[QUOTE=Pecius;16674211][lua][/lua] Usage: [lua][/lua][/QUOTE] You should use PCall, prevents the script from breaking : [lua]local ChatCommands = { } if SERVER then hook.Add( "PlayerSay", "UtilxChatCommandHook", function( ply, text, toall ) if string.Left( text, 1 ) != "/" then return end local args = string.Explode( " ", text ) local command = string.sub( args[ 1 ], 2 ) table.remove( args, 1 ) if ChatCommands[ command ] then pcall(ChatCommands[ command ]( ply, args, toall )) end end) end if CLIENT then hook.Add( "OnPlayerChat", "UtilxChatCommandHook", function( ply, text, toall ) if string.Left( text, 1 ) != "/" or ply != LocalPlayer() then return end local args = string.Explode( " ", text ) local command = string.sub( args[ 1 ], 2 ) table.remove( args, 1 ) if ChatCommands[ command ] then pcall(ChatCommands[ command ]( args, toall )) end end) end function utilx.AddChatCommand( command, func ) if type( command ) != "string" or type( func ) != "function" then return false end ChatCommands[ command ] = func return true end function utilx.RemoveChatCommand( command ) if ChatCommands[ command ] then ChatCommands[ command ] = nil return true end return false end[/lua]
[QUOTE=CptFuzzies;16674316]You guys are way late: gmodplus already does this. Huge number of new functions: [url]http://code.google.com/p/gmodplus/[/url] It even takes functions directly from the Source SDK, and converts them to Lua equivalents.[/QUOTE] utilx would still be nice.
Needs more regular expressions: [lua]function getCommand( msg ) return string.match( msg, "%w+" ) end function getArguments( msg ) local args = { } local i = 1 for v in string.gmatch( msg, "%S+" ) do if i > 1 then table.insert( args, v ) end i = i + 1 end return args end[/lua]
NotAlt, the problem is, that gmodplus was designed to improve upon Garry's Mod where Garry had not wrote or improved functionality himself, as a result, in the project, utilx extentions were brought back.
[QUOTE=CptFuzzies;16680297]NotAlt, the problem is, that gmodplus was designed to improve upon Garry's Mod where Garry had not wrote or improved functionality himself, as a result, in the project, utilx extentions were brought back.[/QUOTE] While there is a lot of stuff that's useful in GM+, it's got content in it that makes it different. It overwrites base files and functions, adds an admin weapon, a couple of convars, models, materials, maps, a sound, and many other things. There's just too much going on in GM+ to consider it in the same vein as this. They're two different things, honestly. GM+ is a more or less lite enhancement pack while this would be aimed more towards coders directly.
[QUOTE=Kogitsune;16680861]While there is a lot of stuff that's useful in GM+, it's got content in it that makes it different. It overwrites base files and functions, adds an admin weapon, a couple of convars, models, materials, maps, a sound, and many other things. There's just too much going on in GM+ to consider it in the same vein as this. They're two different things, honestly. GM+ is a more or less lite enhancement pack while this would be aimed more towards coders directly.[/QUOTE] Exactly, thanks.
[lua] function utilx.DrawScaledBox( RoundBy, PercentageX, PercentageY, PercentageWidth, PercentageHeight, BoxColor) -- Doing some checks if PercentageX == 0 or PercentageY == 0 then print("Percentages can't be 0!") return end -- Some simple math to decide how big the box will be ScrHe = ScrH() ScrWe = ScrW() BoxHeight = (ScrHe*PercentageHeight)/100 BoxWidth = (ScrWe*PercentageWidth)/100 -- -- Some simple math to decide where the box will be placed if PercantageY != 0 then BoxY = (ScrHe*PercentageX)/100 else BoxY = 0 end if PercentageX != 0 then BoxX = (ScrWe*PercentageY)/100 else BoxX = 0 end -- draw.RoundedBox( RoundBy, BoxY, BoxX, BoxHeight, BoxWidth, BoxColor) end [/lua] I was surprised to find that it took decimals. I'm glad, though. :D
[b]utilx.DTextDraw - better than DLabel![/b] [lua]function utilx.DTextDraw(text,font,x,y,parent,col) if not col then col = color_white end local pan = vgui.Create("DPanel",parent) pan:SetSize(parent:GetWide(),parent:GetTall()) pan:SetPos(0,0) pan.Paint = function() draw.DrawText(text,font,x,y,col) end end[/lua] I prefer this as draw.DrawText allows more functionality than DLabels.
[QUOTE=Entoros;16684212][b]utilx.DTextDraw - better than DLabel![/b] :code: I prefer this as draw.DrawText allows more functionality than DLabels.[/QUOTE] You might want to return the label object so that a person can remove it later.
[QUOTE=iRzilla;16691412][lua] function utilx.math.isbetween(num, a, b) if(num > a && num < b) return true else return false end end [/lua][/QUOTE] [lua] function utilx.math.isbetween(num, a, b) return num > a and num < b end [/lua] There is no point to that if statement. Use the power of Lua!
[QUOTE=iRzilla;16691412][lua] function utilx.math.isbetween(num, a, b) if(num > a && num < b) return true else return false end end [/lua][/QUOTE] I do not see the point in having a function for this. You have to type more characters to use the function than to type out the comparison. [lua] utilx.math.isbetween(num, 1, 100) num > 1 and num < 100 [/lua]
[QUOTE=ers35.;16692553]I do not see the point in having a function for this. You have to type more characters to use the function than to type out the comparison. [lua] utilx.math.isbetween(num, 1, 100) num > 1 and num < 100 [/lua][/QUOTE] I concur.
It would be a much more useful function if it accepted varargs. [lua] function utilx.InBetween( num, ... ) return num > math.min( ... ) and num < math.max( ... ) end [/lua]
No offense, but that function doesn't seem to make much sense. I think it would be better to have a variable number of values to check whether they are in between the min and max. [editline]09:07PM[/editline] [lua]function equal( ... ) for i = 2, #arg do if ( arg[i] != arg[1] ) then return false end return true end[/lua]
[QUOTE=Overv;16696889]No offense, but that function doesn't seem to make much sense. I think it would be better to have a variable number of values to check whether they are in between the min and max.[/QUOTE] I can't think of a case off the top of my head where it would be honestly useful, but the same can be said about variadic usage of math.min and math.max - you aren't going to find a lot of times where you're going to need more than two parameters. Besides, it does check a variable number of variables :buddy:. [quote=Overv] [lua]function equal( ... ) for i = 2, #arg do if ( arg[i] != arg[1] ) then return false end return true end[/lua][/QUOTE] [lua] function equal( ... ) return math.min( ... ) == math.max( ... ) end [/lua] Besides, arg is depreciated last I checked and you should avoid it's usage ( instead arg = { ... } ), but it doesn't matter that much since we aren't likely to get a Lua version update anywhere in the near future. Hey, what do you know - there's one of those uses I was talking about :v:.
[lua]function utilx.Average(...) local ret, num = 0 for _, num in ipairs(arg) do ret = ret + num end return ret / #arg, ret end[/lua] Returns the average and sum of a variable number of numbers.
Your version can only compare numbers [img]http://d2k5.com/sa_emots/emot-eng101.gif[/img] Fixed some mistakes, this one is tested and works perfectly: [lua]function equal( ... ) for i = 2, #arg do if ( arg[i] != arg[1] ) then return false end end return true end[/lua]
[QUOTE=Overv;16697468]Your version can only compare numbers [img]http://d2k5.com/sa_emots/emot-eng101.gif[/img] Fixed some mistakes, this one is tested and works perfectly: [lua]function equal( ... ) for i = 2, #arg do if ( arg[i] != arg[1] ) then return false end end return true end[/lua][/QUOTE] I could alternately use InSet if I wanted to compare mixed datatypes. InSet( value, others... ) But you are right.
I don't know how it would work, but maybe a way to round up to the nearest odd/even number? I tried making a function to do that, but I had no idea how to go about doing it =[
[QUOTE=Skapocalypse;16702711]I don't know how it would work, but maybe a way to round up to the nearest odd/even number? I tried making a function to do that, but I had no idea how to go about doing it =[[/QUOTE] Like this? It will round up to the nearest even number. [lua] local function round( x ) x = math.ceil( x ) if ( x % 2 ~= 0 ) then return x + 1 end return x end [/lua]
-snip-
[lua]function utilx.SafeConcat( tbl, sep ) local tmp, i = {}, 0 for i=1, #tbl do table.insert( tmp, tostring( tbl[ i ] ) ) end return table.concat( tmp, sep ) end[/lua]
[lua]local meta = FindMetaTable( "Player" ); if( SERVER ) then utilx.PlayerVars = { } end function utilx.CreatePlayerVariable( name, type, default ) if( SERVER ) then if( default ~= nil ) then PlayerVars[name] = default; end meta["SetPlayer" .. name] = function( self, val ) if( val == nil ) then return; end if( not self:GetTable().Vars ) then self:GetTable().Vars = { } end if( self:GetTable().Vars[name] ~= val ) then local update = true; if( meta["OnUpdatePlayer" .. name] ) then update = meta["OnUpdatePlayer" .. name]( self, self:GetTable().Vars[name], val ) or true; end if( update ) then self:GetTable().Vars[name] = val; umsg.Start( "R" .. name, self ); umsg[type]( val ); umsg.End(); end end end meta["GetPlayer" .. name] = function( self ) if( not self:GetTable().Vars ) then self:GetTable().Vars = { } end return self:GetTable().Vars[name]; end end if( CLIENT ) then if( not ClientVars ) then ClientVars = { } end if( default ~= nil ) then ClientVars[name] = default; end local function setvar( msg ) local val = msg["Read" .. type]( msg ); if( _G["OnUpdatePlayer" .. name] ) then _G["OnUpdatePlayer" .. name]( ClientVars[name], val ); end ClientVars[name] = val; end usermessage.Hook( "R" .. name, setvar ); end end[/lua] I don't know how useful this is to anyone? But I like it for myself. You just do CreatePlayerVariable( "Name", "Type", default ) in a shared file. So CreatePlayerVariable( "Money", "Short", 100 ) makes a variable of type short thats set to 100. It also creates functions for it, GetPlayerMoney() and SetPlayerMoney( amt ) for the server, and then you can use ClientVars["Money"] on the client to get their Money. I find it useful, hopefully you guys do too :) [editline]03:46PM[/editline] Fucking Lua tags....
You just recreated networked values, well almost.
[QUOTE=kahn;16721065][lua]stuff[/lua] I don't know how useful this is to anyone? But I like it for myself. You just do CreatePlayerVariable( "Name", "Type", default ) in a shared file. So CreatePlayerVariable( "Money", "Short", 100 ) makes a variable of type short thats set to 100. It also creates functions for it, GetPlayerMoney() and SetPlayerMoney( amt ) for the server, and then you can use ClientVars["Money"] on the client to get their Money. I find it useful, hopefully you guys do too :) [editline]03:46PM[/editline] Fucking Lua tags....[/QUOTE] Ever heard of [url=http://wiki.garrysmod.com/?title=G.AccessorFuncNW]AccessorFuncNW[/url]?
Sorry, you need to Log In to post a reply to this thread.