Jumba' Posted January 11, 2010 Share Posted January 11, 2010 Hi, I've been trying to figure out a way to assign a unique id to each player, but I can't seem to get it right. I've tried everything I could think of, but I either get duplicate id's, or the old id's don't get removed, etc. This is the one I'm using right now, which doesn't work.. But I'm sure there's a way smaller/easier way to do this, but I can't figure it out. The problem with it is that it does't take out all the old id's from the 'availableID' table. I don't need the whole script already written, just instructions on how I can make it shorter (and maybe without having to use the availableID table?) and I'll try to do it myself. function Init () i = 1 while i < getMaxPlayers() do table.insert ( availableID, i, true ) i = i + 1 end for k, player in ipairs ( getElementsByType ( "player" ) ) do assignPlayerID ( player ) end end function assignPlayerID ( player ) local s_id = 1 while ( isIDAvailable ( s_id ) == false ) do s_id = s_id + 1 end setElementData ( player, "ID", s_id ) table.insert ( availableID, s_id, false ) end function isIDAvailable ( id ) if ( availableID[id] ) then return true else return false end end function onLeave () local id = getElementData ( source, "ID" ) table.insert ( availableID, id, true ) removeElementData ( source, "ID" ) end Link to comment
Jumba' Posted January 11, 2010 Author Share Posted January 11, 2010 Hi, I've been trying to figure out a way to assign a unique id to each player, but I can't seem to get it right. I've tried everything I could think of, but I either get duplicate id's, or the old id's don't get removed, etc. This is the one I'm using right now, which doesn't work.. But I'm sure there's a way smaller/easier way to do this, but I can't figure it out. The problem with it is that it does't take out all the old id's from the 'availableID' table. I don't need the whole script already written, just instructions on how I can make it shorter (and maybe without having to use the availableID table?) and I'll try to do it myself. function Init () i = 1 while i < getMaxPlayers() do table.insert ( availableID, i, true ) i = i + 1 end for k, player in ipairs ( getElementsByType ( "player" ) ) do assignPlayerID ( player ) end end function assignPlayerID ( player ) local s_id = 1 while ( isIDAvailable ( s_id ) == false ) do s_id = s_id + 1 end setElementData ( player, "ID", s_id ) table.insert ( availableID, s_id, false ) end function isIDAvailable ( id ) if ( availableID[id] ) then return true else return false end end function onLeave () local id = getElementData ( source, "ID" ) table.insert ( availableID, id, true ) removeElementData ( source, "ID" ) end Link to comment
eAi Posted January 11, 2010 Share Posted January 11, 2010 Well, function isIDAvailable ( id ) if ( availableID[id] ) then return true else return false end end could just be: function isIDAvailable ( id ) return availableID[id] end What doesn't work with the code you've given? Link to comment
eAi Posted January 11, 2010 Share Posted January 11, 2010 Well, function isIDAvailable ( id ) if ( availableID[id] ) then return true else return false end end could just be: function isIDAvailable ( id ) return availableID[id] end What doesn't work with the code you've given? Link to comment
Jumba' Posted January 11, 2010 Author Share Posted January 11, 2010 Well, function isIDAvailable ( id ) if ( availableID[id] ) then return true else return false end end could just be: function isIDAvailable ( id ) return availableID[id] end *facepalm* What doesn't work with the code you've given? The thing is, when a player disconnects I want the the script to set his id as available, but, here's an example of how the table is at this moment, with only me ingame. 1 false 2 false 3 true 4 false 5 true 6 false 7 true 8 false 9 true 10 true 11 true 12 true 13 true 14 true 15 true 16 true 17 true 18 true 19 true 20 true 21 true 22 true 23 true 24 true 25 true 26 true 27 true 28 true 29 true 30 true 31 true 32 true 33 true 34 true 35 true 36 true 37 true 38 true 39 true 1. The table gets larger 2. Some id's are set to 'false' for some reason. Link to comment
Jumba' Posted January 11, 2010 Author Share Posted January 11, 2010 Well, function isIDAvailable ( id ) if ( availableID[id] ) then return true else return false end end could just be: function isIDAvailable ( id ) return availableID[id] end *facepalm* What doesn't work with the code you've given? The thing is, when a player disconnects I want the the script to set his id as available, but, here's an example of how the table is at this moment, with only me ingame. 1 false2 false3 true4 false5 true6 false7 true8 false9 true10 true11 true12 true13 true14 true15 true16 true17 true18 true19 true20 true21 true22 true23 true24 true25 true26 true27 true28 true29 true30 true31 true32 true33 true34 true35 true36 true37 true38 true39 true 1. The table gets larger 2. Some id's are set to 'false' for some reason. Link to comment
50p Posted January 12, 2010 Share Posted January 12, 2010 Because you insert new variable into the table every time you assign new ID. You should use: -- Instead of: table.insert ( availableID, s_id, false ) -- Use this: availableID[ s_id ] = false; Link to comment
50p Posted January 12, 2010 Share Posted January 12, 2010 Because you insert new variable into the table every time you assign new ID. You should use: -- Instead of: table.insert ( availableID, s_id, false ) -- Use this: availableID[ s_id ] = false; Link to comment
Jumba' Posted January 12, 2010 Author Share Posted January 12, 2010 Because you insert new variable into the table every time you assign new ID. You should use: -- Instead of: table.insert ( availableID, s_id, false ) -- Use this: availableID[ s_id ] = false; Aaah, that explains it. Thanks! =D Link to comment
Jumba' Posted January 12, 2010 Author Share Posted January 12, 2010 Because you insert new variable into the table every time you assign new ID. You should use: -- Instead of: table.insert ( availableID, s_id, false ) -- Use this: availableID[ s_id ] = false; Aaah, that explains it. Thanks! =D 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