Jump to content

Useful Functions


Recommended Posts

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 by Guest
Link to comment

Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? :D

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 by Guest
Link to comment
Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? :D

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

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 by Guest
Link to comment
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
Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? :D

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? :mrgreen:

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
Can you explain me why you need str.Repeat when you have "string.rep" wich is much more easy? :D

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? :mrgreen:

Yes, I like him style Sry, i will not use anymore.

Why?

You not like prefixes?

u = userdata

t = table

n = number

s = string

...

troll-face1.jpg

I ever forget something :oops:

Link to comment
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
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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...