Jump to content

A better way to write this function


Recommended Posts

Hello Guys/Gals, Is there a way to make this function dynamic? By dynamic, I mean that it will automatically get the lowest members a team has and return it without providing the names and members of all the teams.

  
local Clans =  
{ 
    { "Sanity", { 0, 0, 255 }, {150, 250, 14} }, 
    { "Blodz", { 255, 0, 0 }, {50, 300, 15} }, 
    { "Gringo", { 0, 255, 0 }, {350, 10, 16} } 
} 
  
function clanForMember() 
  
    local clancount = {} 
    clancount[1] = clanCountMembers("Sanity") 
    clancount[2] = clanCountMembers("Blodz") 
    clancount[3] = clanCountMembers("Gringo") 
     
    if(clancount[1] < clancount[2] and clancount[3]) then return clanGet("Sanity") 
    elseif(clancount[2] < clancount[1] and clancount[3]) then return clanGet("Blodz") 
    elseif(clancount[3] < clancount[1] and clancount[2]) then return clanGet("Gringo") 
    else 
     
        local clanrnd = math.random(1, 3) 
         
        return clanGet(Clans[clanrnd][1]) 
     
    end 
  
end 
  

Link to comment

Sure, it can:

local fTableInsert, fTableSort = table.insert, table.sort; 
local clans = 
{ 
    { "Sanity",     { 0, 0, 255 },      { 150, 250, 14 } }, 
    { "Blodz",      { 255, 0, 0 },      { 50, 300, 15 }  }, 
    { "Gringo",     { 0, 255, 0 },      { 350, 10, 16 }  } 
}; 
  
clanForMember = function() 
    local clanMembers = {} 
     
    for i,v in ipairs ( _clans ) do 
        fTableInsert ( clanMembers, { v[1], clanCountMembers ( v[ 1 ] ) } ); 
    end 
    fTableSort ( clanMembers, function ( a, b ) return a[1] < b[1] end ); 
     
    return clanGet ( clanMembers[1][1] ); 
end 

Edited by Guest
Link to comment
Sure, it can:
local clans = 
{ 
    { "Sanity",     { 0, 0, 255 },      { 150, 250, 14 } }, 
    { "Blodz",      { 255, 0, 0 },      { 50, 300, 15 }  }, 
    { "Gringo",     { 0, 255, 0 },      { 350, 10, 16 }  } 
}; 
  
clanForMember = function() 
    local _clans = clans; 
    local fTableSort = table.sort; 
    local fTableInsert = table.insert; 
    local clanMembers = {} 
     
    for i,v in ipairs ( _clans ) do 
        fTableInsert ( clanMembers, { v[1], clanCountMembers ( v[ 1 ] ) } ); 
    end 
     
    fTableSort ( clanMembers, function ( a, b ) return a[1] < b[1] end ); 
     
    return clanGet ( clanMembers[1][1] ); 
end 
     

What's the point of the following references?

  
    local _clans = clans; 
    local fTableSort = table.sort; 
    local fTableInsert = table.insert; 

Link to comment
Do you mind explaining in what form will it help then? Because most websites disagree with you (or perhaps I didn't understand correctly when to use that?).

Why would referencing a local table make it faster?

You can save a little when referencing Lua library functions (math, table etc), but why would you do it inside a single function's scope? (i.e do it every time you call the function instead of a single reference when the script loads)

Link to comment
Sure, it can:
local fTableInsert, fTableSort = table.insert, table.sort; 
local clans = 
{ 
    { "Sanity",     { 0, 0, 255 },      { 150, 250, 14 } }, 
    { "Blodz",      { 255, 0, 0 },      { 50, 300, 15 }  }, 
    { "Gringo",     { 0, 255, 0 },      { 350, 10, 16 }  } 
}; 
  
clanForMember = function() 
    local clanMembers = {} 
     
    for i,v in ipairs ( _clans ) do 
        fTableInsert ( clanMembers, { v[1], clanCountMembers ( v[ 1 ] ) } ); 
    end 
    fTableSort ( clanMembers, function ( a, b ) return a[1] < b[1] end ); 
     
    return clanGet ( clanMembers[1][1] ); 
end 

Thank you!

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...