• UtilX -- Extending GLua -- Need community input
    371 replies, posted
Sorry if I'm missing something, but couldn't you do something like this? [lua]function DoMath(pl,cmd,args) local str = "" for _,v in ipairs(args) do str = str .. v end RunString(str) end concommand.Add("math",DoMath)[/lua] Is RunString not an option?
[QUOTE=Entoros;17739948]Sorry if I'm missing something, but couldn't you do something like this? [lua]function DoMath(pl,cmd,args) local str = "" for _,v in ipairs(args) do str = str .. v end RunString(str) end concommand.Add("math",DoMath)[/lua] Is RunString not an option?[/QUOTE] Optimized version: [lua]function DoMath( pl, cmd, args ) RunString( table.concat( args, " " ) ) end concommand.Add( "math", DoMath )[/lua]
But RunString is easy to exploit.
[QUOTE=Entoros;17739948]Sorry if I'm missing something, but couldn't you do something like this? [lua]function DoMath(pl,cmd,args) local str = "" for _,v in ipairs(args) do str = str .. v end RunString(str) end concommand.Add("math",DoMath)[/lua] Is RunString not an option?[/QUOTE] math "for k,v in pairs(player.GetAll()) do v:Ban(0) end" :downs:
[QUOTE=Overv;17740377]But RunString is easy to exploit.[/QUOTE] You could runstring it on the client calling the command and then send the result back to the server. Depends on how you're going to do it though.
Using RunString defeats the purpose of having such a function.
[QUOTE=Overv;17740377]But RunString is easy to exploit.[/QUOTE] Or you can just filter out all letters. Some stuff wouldn't be possible then, but you could have things like 35+144/54*4^2
[QUOTE=Dlaor;17741255]Or you can just filter out all letters. Some stuff wouldn't be possible then, but you could have things like 35+144/54*4^2[/QUOTE] But then there's no variables and trig functions, so a function would be much better.
[QUOTE=raBBish;17740400]math "for k,v in pairs(player.GetAll()) do v:Ban(0) end" :downs:[/QUOTE] ...But only a fool would run math functions serverside. RunString would be completely harmless and do everything you were looking for.
Have fun: [url]http://codepad.org/SE60D81Z[/url] (usage at bottom)
[QUOTE=Deco Da Man;17753580]Have fun: [url]http://codepad.org/SE60D81Z[/url] (usage at bottom)[/QUOTE] That's some horribly hard to read code. [Lua]math.calcfuncs[k] = type(v) == "function" and v or function() return v end [/Lua]
[QUOTE=Jawalt;17755611]That's some horribly hard to read code. [Lua]math.calcfuncs[k] = type(v) == "function" and v or function() return v end [/Lua][/QUOTE] I find that really easy to read. I can't understand why everyone finds it hard to read... I do use a white on black colour scheme, if that matters. I'm also short-sighted (I use my glasses). Maybe I just have superior eyesight :D Anyway.. I like the style, and I'm gonna keep using it. [b]Edit:[/b] Not as good as [url=http://codepad.org/ucuMCslP]this[/url]!
Might have something to do with the fact that you defined a variable, performed a comparison, and returned all in the same line.
[QUOTE=Levybreak;17756211]Might have something to do with the fact that you defined a variable, performed a comparison, and returned all in the same line.[/QUOTE] No, he performed a comparison and declared a function in a variable declaration. The equivalent would [lua]if type(v) == "function" then math.calcfuncs[k] = v else math.calcfuncs[k] = function() return v end end[/lua] Of the bizarre style Deco uses, that's the one of the least bizarre examples you could have used.
Funny thing is, it all gets converted into pretty much the same [url=http://en.wikipedia.org/wiki/Assembly_language]Assembly[/url] code in the end, so coding styles don't matter to much to a computer :P
[QUOTE=Gbps;17778821]Funny thing is, it all gets converted into pretty much the same [url=http://en.wikipedia.org/wiki/Assembly_language]Assembly[/url] code in the end, so coding styles don't matter to much to a computer :P[/QUOTE] Most scripted languages don't, but Lua does get compiled at runtime I think.
[lua] local meta = _R.Entity function meta:TranslateBoneToPhysBone(bone) for i=0, self:GetPhysicsObjectCount()-1 do if (self:TranslatePhysBoneToBone(i) == bone) then return i end end return -1 end [/lua] Sorry if this was already posted.
[QUOTE=Deco Da Man;17756136] [b]Edit:[/b] Not as good as [url=http://codepad.org/ucuMCslP]this[/url]![/QUOTE] Thanks to examples like this I've began using this "style" too. I find it also easy to read and when you get the logic it's really convenient compared to messing with billions of if clauses. One place where I used to use this: [LUA] -- Snip -- function(name, pass, steam, ip) Msg(Format( "User %s joining with%s id %s from %s", name, (pass != "") and (" password "..pass.." and") or "", steam, ip )) -- Snip -- end [/LUA]
Pretty self explanatory. Rounds a number to the highest whole division of the number (This is not rounded up) [lua]-- utilx.RoundTo -- Use -- Round a number to a specified number -- utilx.RoundTo(int number to round to , int number to round) -- Example: print(utilx.RoundTo(5 , 27)) : 25 function utilx.RoundTo(a , b) return tonumber(math.floor(b / a) * a) end[/lua] It's untested but is so amazingly simple, if I made a mistake in that I might as well give up coding. Can also build upon it: [lua]-- utilx.NumberIsEven -- Use -- Returns true if a number is even, false if not. -- utilx.NumberIsEven(int number) -- Example: if utilx.NumberIsEven(2) then print("Even") else print("Odd") end : "Even" function utilx.NumberIsEven(num) return (utilx.RoundTo(2 , num) == num) end[/lua] [lua]-- The cleaner version of utilx.NumberIsEven function utilx.NumberIsEven(num) return (num % 2 == 0) end[/lua]
[QUOTE=Levybreak;17756211]Might have something to do with the fact that you defined a variable, performed a comparison, and returned all in the same line.[/QUOTE] he doesn't return something in that line he assigns a function that returns something, however I find that easy to read as well. It helps if you're used to seeing the "a?b:c" operator in other languages. "a and b or c" is the closest Lua equivalent of that operator. [editline]03:12AM[/editline] [QUOTE=thomasfn;17791176]Most scripted languages don't, but Lua does get compiled at runtime I think.[/QUOTE] not into asm though. try luac -l
Started this a year or more ago, sadly it has almost no documentation (and some programming articles aren't even defined on the wiki) [url]http://dan.asteriskgaming.com/wiki/LibAsterisk[/url] UtilX kind of makes it limited to a library. :/
That's pretty awesome, should be in GMod by default.
i'm not sure if this has been done before or not: [php] function AllEntsInClass( class ) for k, v in pairs(ents.FindByClass(class)) do return v end end [/php] this would be useful for finding all of one type of entity, like this: [php] local ENT = GetMetaTable("Entity") function ENT:GetOwner() return AllEntsInClass(ent:GetClass()).owner end [/php] i just thought i'd try to pitch in...
[QUOTE=ZenX2;17873262]i'm not sure if this has been done before or not: [php] function AllEntsInClass( class ) for k, v in pairs(ents.FindByClass(class)) do return v end end [/php] this would be useful for finding all of one type of entity, like this: [php] local ENT = GetMetaTable("Entity") function ENT:GetOwner() return AllEntsInClass(ent:GetClass()).owner end [/php] i just thought i'd try to pitch in...[/QUOTE] What? Neither of those would do anything useful what so ever. The first one would return the first entity found by ents.FindByClass() and the second one would throw an error about 'ent' not being defined. what were you trying to do? [editline]08:30AM[/editline] Also GetMetaTable isn't even a function.
And why are you overwriting Entity.GetOwner?
Here's something I needed once. [lua]function CapitalizeFirstLetter( str ) str = string.Explode( "", str ) str[1] = string.upper( str[1] ) return string.Implode( "", str ) end[/lua] If anyone has a better way of doing it, tell me, because I don't think my version is very optimized.
[QUOTE=Dlaor;17879707]Here's something I needed once. [lua]function CapitalizeFirstLetter( str ) str = string.Explode( "", str ) str[1] = string.upper( str[1] ) return string.Implode( "", str ) end[/lua] If anyone has a better way of doing it, tell me, because I don't think my version is very optimized.[/QUOTE] [lua]function CapitaliseFirstLetter(str) return string.gsub(str, "%w", string.upper, 1) end[/lua]
[QUOTE=Deco Da Man;17879745][lua]function CapitaliseFirstLetter(str) return string.gsub(str, "%w", string.upper, 1) end[/lua][/QUOTE] Jesus christ, you can do EVERYTHING with regular expressions it seems.
[QUOTE=Dlaor;17879876]code[/QUOTE] [lua] function util.AOrAn(s) return string.match(s, "^h?[AaEeIiOoUu]") and "an" or "a" end [/lua] [editline]01:28AM[/editline] Stop snipping stuff as I reply with a snipped version of your reply! Makes me appear like I'm quoting you then posting random stuff :S [QUOTE=Dlaor;17879876]Jesus christ, you can do EVERYTHING with regular expressions it seems.[/QUOTE] Hehe. They are awesome.
[QUOTE=Deco Da Man;17879944][lua] function util.AOrAn(s) return string.match(s, "^h?[AaEeIiOoUu]") and "an" or "a" end [/lua][/QUOTE] An hatstand? [editline]06:33PM[/editline] An horse? An house? An headcrab? An hose? Why is there an H there, I can't think of any words that begin with h that use 'an' [editline]06:37PM[/editline] An hole, an hand, an homing missile. [editline]06:37PM[/editline] You ain't nothing but an hound dog.
Sorry, you need to Log In to post a reply to this thread.