Cassandra Posted March 9, 2013 Share Posted March 9, 2013 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
PaiN^ Posted March 9, 2013 Share Posted March 9, 2013 If you want it automatically, You can add it to an event, Or you can use a timer .. Link to comment
Cassandra Posted March 9, 2013 Author Share Posted March 9, 2013 If you want it automatically, You can add it to an event, Or you can use a timer .. That will be the same as you have to check the other team members. Link to comment
OGF Posted March 9, 2013 Share Posted March 9, 2013 You could try using using the standard table library function # operator '#' - to get the number of elements within a table. Link to comment
Anderl Posted March 9, 2013 Share Posted March 9, 2013 (edited) 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 March 9, 2013 by Guest Link to comment
FFS-Racing Posted March 9, 2013 Share Posted March 9, 2013 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
Anderl Posted March 9, 2013 Share Posted March 9, 2013 Faster lookups, it won't give any advantage in little things, but will help in large projects. Link to comment
FFS-Racing Posted March 9, 2013 Share Posted March 9, 2013 Faster lookups, it won't give any advantage in little things, but will help in large projects. Faster lookups? No, not in this form. Link to comment
Anderl Posted March 9, 2013 Share Posted March 9, 2013 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?). Link to comment
FFS-Racing Posted March 9, 2013 Share Posted March 9, 2013 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
Anderl Posted March 9, 2013 Share Posted March 9, 2013 Good point, I guess I got it now. Thanks for explaining. Link to comment
Cassandra Posted March 9, 2013 Author Share Posted March 9, 2013 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
Anderl Posted March 9, 2013 Share Posted March 9, 2013 By the way, in the ipairs function change "_clans" to "clans". I forgot to change it after editing the code Link to comment
Cassandra Posted March 9, 2013 Author Share Posted March 9, 2013 By the way, in the ipairs function change "_clans" to "clans". I forgot to change it after editing the code I got it already, it's fine. 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