Poll

Does this library/API thingy seem useful in the slightest?

Yes.
1 (25%)
No.
3 (75%)

Total Members Voted: 0

  • Print

Author Topic: My first Lua library/API/thing! (From another Lua half-noob)  (Read 7659 times)

0 Members and 1 Guest are viewing this topic.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
My first Lua library/API/thing! (From another Lua half-noob)
« on: January 25, 2014, 09:09:50 pm »
Much like syn, I am myself learning Lua at the moment... slowly but steadily. :P

Being OCD, I like to make useless functions that I could easily put inline and save space... but I like to not do that as it makes things look nicer. :P
Anyway, here's my first Lua library/API/thingy that I'll use for most of my scripts, whether they're just for fun or something I'll actually release.
Code: Lua
  1. --[[
  2. OCDLib by Short Circuit
  3. For use by OCD people/ponies who want cool functions for Lua. :P
  4. ]]--
  5.  
  6. --VERSION: 0.1
  7.  
  8. OCDLib = {}
  9.  
  10. --Pre-formatted error handler.
  11. function OCDLib.errorDetection( func )
  12.         error( "OCDLib." .. func .. " experienced an error of some type." )
  13. end
  14.  
  15. function OCDLib.makeSomeRoom( spaces )
  16.         --Checks if any specific number of spaces is needed.
  17.         --(If not, return 2.)
  18.         if spaces ~= nil then
  19.                 for i = 1, spaces do
  20.                         --\n also works... just don't feel like it. :P
  21.                         print( "" )
  22.                 end
  23.         --Again, if no specific spaces are returned, use 2 as the default.
  24.         elseif spaces == nil then
  25.                 for i = 1, 2 do
  26.                         print( "" )
  27.                 end
  28.         --If something fails somehow... use our errorDetection function! :D
  29.         else
  30.                 OCDLib.errorDetection( "makeSomeRoom" )
  31.         end
  32. end
  33.  
  34. --DOS/Windows like pause function.
  35. function OCDLib.pauseAndWait( textstr )
  36.         --If you want, define the text to be printed here.
  37.         if textstr ~= nil then
  38.                 print( textstr )
  39.         elseif textstr == nil then
  40.                 --If no text is defined, default to this.
  41.                 print( "Press Enter to continue..." )
  42.         --If something fails somehow... use our errorDetection function! :D
  43.         else
  44.                 OCDLib.errorDetection( "pauseAndWait" )
  45.         end
  46.         --Actually wait for enter. :P
  47.         io.read()
  48. end
  49.  
  50. --Psuedo true randomness generator.
  51. function OCDLib.trueRandom( min, max )
  52.         --Init the randomseed and throw away the first few numbers as they may not always be random.
  53.         math.randomseed( os.time() )
  54.         --Make a dumping ground for useless numbers.
  55.         uselessDumpingGrounds = {}
  56.         for i = 1,5 do
  57.                 --Insert the useless numbers into our dumping grounds.
  58.                 uselessDumpingGrounds[i] = math.random()
  59.         end
  60.         --Clear up the memory space used by our dumping grounds.
  61.         uselessDumpingGrounds = nil
  62.         if min and max ~= nil then
  63.                 random = math.random( min, max )
  64.         --Check for anything that might cause errors...
  65.         elseif min == nil then
  66.                 random = math.random( max )
  67.         elseif max == nil then
  68.                 random = math.random( min, 10 )
  69.         elseif min and max == nil then
  70.                 random = math.random( 10 )
  71.         --If something fails somehow... use our errorDetection function! :D
  72.         else
  73.                 OCDLib.errorDetection( "trueRandom" )
  74.         end
  75.         return random
  76. end
  77.  
  78. function OCDLib.credits( scriptname, creator, date, use )
  79.         if scriptname ~= nil then
  80.                 print( "Script name: " .. scriptname )
  81.         elseif scriptname == nil then
  82.                 return
  83.         elseif creator ~= nil then
  84.                 print( "Author: " .. creator )
  85.         elseif creator == nil then
  86.                 return
  87.         elseif date ~= nil then
  88.                 print( "Created on: " .. date )
  89.         elseif date == nil then
  90.                 return
  91.         elseif use ~= nil then
  92.                 print( "Used for: " .. use )
  93.         elseif use == nil then
  94.                 return
  95.         else
  96.                 OCDLib.errorDetection( "credits" )
  97.         end
  98. end

Look at it, and bask in its OCD glory!
Look, there's even OCD functions inside of OCD functions! :D
EDIT: Also, bask in its Ulysses approved spacing style! :D
« Last Edit: January 25, 2014, 09:13:45 pm by Princess Twilight Sparkle »
bw81@ulysses-forums ~ % whoami
Homepage

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #1 on: January 26, 2014, 12:30:17 am »
I would say it's useful, but I still don't know what this is supposed to do.
Out of the Garry's Mod business.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #2 on: January 26, 2014, 06:28:05 am »
It really doesn't have just one single use...
It's a function library. Those are functions I've made in multiple scripts that I grew tired of copy/pasting the code of.
Think of the way ULib works:
ULib by itself is pretty useless, is it not?
Also, something like ULX is useless without ULib.

What I tried to do with that code was minimize file sizes and coding time, much like the way ULib does.
Again, it's a function library stored in a global table. It's useless on its own, but most of my newer scripts use its code.
bw81@ulysses-forums ~ % whoami
Homepage

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #3 on: January 26, 2014, 02:45:19 pm »
Nice, Short Circuit! What's with your recent kick modifying the random function, though? You only really have two classes of randomness, PRNG (Pseudo-Random Number Generator) and TRNG (True Random Number Generator). Since you're still depending on math.random, you're still well within PRNG.
Experiencing God's grace one day at a time.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #4 on: January 26, 2014, 03:36:45 pm »
Nice, Short Circuit! What's with your recent kick modifying the random function, though? You only really have two classes of randomness, PRNG (Pseudo-Random Number Generator) and TRNG (True Random Number Generator). Since you're still depending on math.random, you're still well within PRNG.
Eh...
I like math.random() as it doesn't take any new libraries or code to make. The reason i mod it os that, woth my experiences, the first value(s) returned by it seem the same... Could just be me, though.
Also, the seed in that isn't static- it changes every second.


bleh
bw81@ulysses-forums ~ % whoami
Homepage

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #5 on: January 26, 2014, 04:47:10 pm »
Oh, I didn't even notice that. You shouldn't change the seed except at initialization (which Garry already does). That's probably why you're getting poor results. Every time you reset the seed in the same second, you're going to get the same number back.
Experiencing God's grace one day at a time.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #6 on: January 26, 2014, 05:12:17 pm »
Oh, I didn't even notice that. You shouldn't change the seed except at initialization (which Garry already does). That's probably why you're getting poor results. Every time you reset the seed in the same second, you're going to get the same number back.
Which is why I dump the first five random values into a table that i then clear. :P

EDIT: Also, i wrote this for Lua period, not necessarily just GLua, hence there not being any GLua code... I'm still learning basic Lua syntax before i move on to something like GLua. :P
« Last Edit: January 26, 2014, 05:14:26 pm by Princess Twilight Sparkle »
bw81@ulysses-forums ~ % whoami
Homepage

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #7 on: January 26, 2014, 06:05:31 pm »
hence there not being any GLua code
ULib = 99.9% glua free.
.01%, I think, due to hacks we've fixed for Glua
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #8 on: January 26, 2014, 07:11:42 pm »
Yeah, you shouldn't graduate from lua to glua. If you can avoid using glua do so.

All glua adds are helper functions to make your life easier at the expense of learning lua wrong.

For instance, lua does not have a lot of the same string library functions that glua has. If you want string.explode you have to code that yourself, the hard way.

Offline Bytewave

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 718
  • Karma: 116
  • :)
    • My Homepage
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #9 on: January 27, 2014, 08:39:38 pm »
But what about functions that CAN'T be coded yourself? What about functions that execute code in Source itself, or something like AddConVar() (pretty sure that's the one) and AddConsoleCommand() that call code outside of Lua?
bw81@ulysses-forums ~ % whoami
Homepage

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #10 on: January 27, 2014, 11:31:23 pm »
Don't confuse GMod's API with glua.

GLua (at least as far as I understand) is just Garry's modified lua.

Offline Neku

  • Hero Member
  • *****
  • Posts: 549
  • Karma: 27
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #11 on: January 28, 2014, 12:25:37 am »
GLua is a mix of coding languages. Not gmod functions.
Out of the Garry's Mod business.

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #12 on: January 28, 2014, 05:11:28 am »
I'm still angry about Garry adding "!=" and "//". What a great way to encourage horrible coding practices.
Experiencing God's grace one day at a time.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #13 on: January 28, 2014, 08:25:25 am »
I used to use ! as a negate until I realized that it wasn't true lua. After that I just started using 'not' like you're supposed to.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: My first Lua library/API/thing! (From another Lua half-noob)
« Reply #14 on: January 28, 2014, 03:23:02 pm »
just started using 'not' like you're supposed to.
Though "!=" isn't Lua, "~=" is.
For readability sake, which MrPresident does very well, "not" is recommended following Team U's recommendations of how to program for Ulysses projects.

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

  • Print