Cassandra Posted April 4, 2012 Share Posted April 4, 2012 (edited) I know there is an article about it on wiki but a thread is better. You can post below some useful functions but please don't post untested code and don't go offtopic. Detects if a player is in a range of point. function isPlayerInRangeOfPoint(player, range, x, y, z) local plx, ply, plz = getElementPosition(player) return ((x-plx)^2+(y-ply)^2+(z-plz)^2)^0.5<=range end Gets the chance in percentage. function percentageChance(value) if value >= math.random(1, 100) then return true else return false end end Trim the extra spaces of a string. function str.Trim(str) str = string.gsub(str, '^%s+', '') str = string.gsub(str, '%s+$', '') return str end Repeat a string concerning security. function str.Repeat(str, num) if num ~= nil and tonumber(num) ~= nil and tonumber(num) > 0 then return string.rep(str, num) else return '' end end Pulls back the maximum index for the table specified and does not needs to be in sequential or contain none Numbers. function tableMax(thetable) if type(thetable) == "table" then local retNum = 0; for Key,Values in pairs(thetable) do if type(Key) == "number" and Key > retNum then retNum = Key end end return retNum else return end end Checks a table to see if the key is the defined value(thevalue) in lower case. function tableLowCheck(thetable, thevalue) if type(thetable) == "table" then local returnValues = {} local nextValueNum = 1 for Key, Value in pairs(thetable) do if strlower(Key) == strlower(thevalue) then returnValues[nextValueNum] = Key nextValueNum = nextValueNum + 1 end end return unpack(returnValues) else return end end Checks a table to see if the key is the defined value(thevalue) in upper case. function tableUpCheck(thetable, thevalue) if type(thetable) == "table" then local returnValues = {} local nextValueNum = 1 for Key, Value in pairs(thetable) do if strupper(Key) == strupper(thevalue) then returnValues[nextValueNum] = Key nextValueNum = nextValueNum + 1 end end return unpack(returnValues) else return end end Copies a table unlikely pointing to it. function tableCopy(thetable) local dest = {} if type(thetable) == "table" then for Key,Value in pairs(thetable) do if type(Key) == "table" then Key = CopyTable(Key) end if type(Value) == "table" then Value = CopyTable(Value) end dest[Key] = Value end else dest = thetable end return dest end Edited April 5, 2012 by Guest Link to comment
Renkon Posted April 4, 2012 Share Posted April 4, 2012 Would you mind telling us what do those functions do? Link to comment
drk Posted April 5, 2012 Share Posted April 5, 2012 (edited) Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? I created a simple XML Create file function, it's more easy than creating file and saving it. Here: local xml = { } function xml:Create ( uPath, uNode ) setmetatable ( { value1 = uPath, value2 = uNode }, xml ) if ( not uPath or not uNode or type ( uPath ) ~= 'string' or type ( uNode ) ~= 'string' ) then return end local file = xmlCreateFile ( uPath, uNode ) if ( file ) then xmlSaveFile ( file ) return file else return end end If you think that it's not useful, tell me. Edited April 5, 2012 by Guest Link to comment
Cassandra Posted April 5, 2012 Author Share Posted April 5, 2012 Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? I created a simple XML Create file function, it's more easy than creating file and saving it. Here: local xml = { } function xml:Create ( path, node ) if ( type ( path ) ~= 'string' or type ( node ) ~= 'string' ) then return end local file = xmlCreateFile ( tostring ( path ), tostring ( node ) ) if ( file ) then xmlSaveFile ( file ) return file else return end end If you think that it's not useful, tell me. For security reasons. Nice function you got there but I still prefer the default xml functions. Link to comment
drk Posted April 5, 2012 Share Posted April 5, 2012 (edited) I created this too: local xml = { } function xml:GetNodeValue ( uLoaded, uNode ) setmetatable ( { value1 = uLoaded, value2 = uNode }, xml ) if ( not uLoaded or not uNode or type ( uLoaded ) ~= 'string' or type ( uNode ) ~= 'string' ) then return end local uChild = xmlFindChild ( uLoaded, uNode, 0 ) if ( uChild ) then return xmlNodeGetValue ( uChild ) else return end end Example: -- Do this way: local value = xml:GetNodeValue ( xmlLoadFile ( 'myFile.xml' ), 'country' ) return value -- Instead of local child = xmlFindChild ( xmlLoadFile ( 'myFile.xml' ), 'country', 0 ) if ( child ) then local value = xmlNodeGetValue ( child ) return value else return end Maybe I can make some new functions but I need think about that. Maybe I release my DX Text / DX Rectangle library here too. Edited April 5, 2012 by Guest Link to comment
Cassandra Posted April 5, 2012 Author Share Posted April 5, 2012 I created this too: local xml = { } function xml:GetNodeValue ( uLoaded, uNode ) if ( not uLoaded or not uNode or type ( uLoaded ) ~= 'string' or type ( uNode ) ~= 'string' ) then return end local uChild = xmlFindChild ( uLoaded, uNode, 0 ) if ( uChild ) then return xmlNodeGetValue ( uChild ) else return end end Example: -- Do this way: local value = xml:GetNodeValue ( xmlLoadFile ( 'myFile.xml' ), 'country' ) return value -- Instead of local child = xmlFindChild ( xmlLoadFile ( 'myFile.xml' ), 'country', 0 ) if ( child ) then local value = xmlNodeGetValue ( child ) return value else return end I see you are caring intensively for the security. Link to comment
Castillo Posted April 5, 2012 Share Posted April 5, 2012 @Draken: You really created the function: "xml:GetNodeValue" ? because you've used "uLoaded, uNode", that's Kenix style. Link to comment
drk Posted April 5, 2012 Share Posted April 5, 2012 Yes, I like him style Sry, i will not use anymore. Link to comment
Castillo Posted April 5, 2012 Share Posted April 5, 2012 Oh, I was just asking, you should be a little more creative and do your own style . Let's stop doing off-topic. Link to comment
Cassandra Posted April 5, 2012 Author Share Posted April 5, 2012 Would you mind telling us what do those functions do? Apologize for that. I've updated the thread. Link to comment
Cassandra Posted April 5, 2012 Author Share Posted April 5, 2012 Added few table functions at the main post which are useful and efficient. And also, please post the function including the credit if it's not yours. Link to comment
Kenix Posted April 5, 2012 Share Posted April 5, 2012 Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? I created a simple XML Create file function, it's more easy than creating file and saving it. Here: local xml = { } function xml:Create ( uPath, uNode ) if ( not uPath or not uNode or type ( uPath ) ~= 'string' or type ( uNode ) ~= 'string' ) then return end local file = xmlCreateFile ( uPath, uNode ) if ( file ) then xmlSaveFile ( file ) return file else return end end If you think that it's not useful, tell me. Where setmetatable? Yes, I like him style Sry, i will not use anymore. Why? You not like prefixes? u = userdata t = table n = number s = string ... Link to comment
drk Posted April 5, 2012 Share Posted April 5, 2012 Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? I created a simple XML Create file function, it's more easy than creating file and saving it. Here: local xml = { } function xml:Create ( uPath, uNode ) if ( not uPath or not uNode or type ( uPath ) ~= 'string' or type ( uNode ) ~= 'string' ) then return end local file = xmlCreateFile ( uPath, uNode ) if ( file ) then xmlSaveFile ( file ) return file else return end end If you think that it's not useful, tell me. Where setmetatable? Yes, I like him style Sry, i will not use anymore. Why? You not like prefixes? u = userdata t = table n = number s = string ... I ever forget something Link to comment
Aibo Posted April 5, 2012 Share Posted April 5, 2012 first function is redundant and wrong, so i didnt read the rest. Link to comment
Cassandra Posted April 5, 2012 Author Share Posted April 5, 2012 first function is redundant and wrong, so i didnt read the rest. Do you have a better way? Link to comment
BinSlayer1 Posted April 5, 2012 Share Posted April 5, 2012 first function is redundant and wrong, so i didnt read the rest. Do you have a better way? I do. Use the string manipulation library pre-included. lua-users.org/wiki/StringLibraryTutorial Scroll down to the bottom of this page. I guess what Aibo meant to say was that there's no use in creating an "useful function" for something achievable with a built-in function. Also, your 'useful function' is wrong because it doesn't use the OOP reference correctly Link to comment
Cassandra Posted April 5, 2012 Author Share Posted April 5, 2012 first function is redundant and wrong, so i didnt read the rest. Do you have a better way? I do. Use the string manipulation library pre-included. lua-users.org/wiki/StringLibraryTutorial Scroll down to the bottom of this page. I guess what Aibo meant to say was that there's no use in creating an "useful function" for something achievable with a built-in function. Also, your 'useful function' is wrong because it doesn't use the OOP reference correctly Yeah, I saw that. And please don't go offtopic. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now