dzek (varez) Posted March 31, 2010 Share Posted March 31, 2010 Hi Sometimes i hear word "table", sometimes "array", are they the same for LUA? Or this is another types? I was always not sure about it, and looping through it. Every loop on ipairs (this is looping through tables/array, right?) i've written i've copied from wiki, then modified. Now look what im trying to do: texts= { } textCount=0 function showText(text, time) textCount = textCount+1 texts[textCount]={ } texts[textCount]['text']=text texts[textCount]['time']=time end -- for example I'm adding some texts to that table/array showText("test1",50) showText("test2",33) showText("test3",99) -- then after some conditions im removing one of them texts[1]=nil -- so only texts[0] and texts[2] are not nil -- how can i loop through all texts, w/out using iteration, and checking if texts[i] is nil, or not nil ? -- for example output all texts, and times assigned to them to chatbox? Link to comment
driver2 Posted March 31, 2010 Share Posted March 31, 2010 "Table" is the datastructure of Lua that can be used as an array, list or even class. The ipairs() iterator only works with increasing indices (1,2,3,4, ..). If you want to loop through all numbers, use a numeric for loop: for i = 1,10 do .. end -- loops from 1 to 10 in steps of 1 If you want to loop through all elements of a table in no particular order, use pairs(): for k,v in pairs(table) do .. end Maybe you also want to use table.insert() and table.remove() which shifts the indices to create increasing indices. http://lua-users.org/wiki/ForTutorial http://www.lua.org/manual/5.1/manual.html#5.5 1 Link to comment
dzek (varez) Posted April 1, 2010 Author Share Posted April 1, 2010 thanks for answer and the links Link to comment
karlis Posted April 1, 2010 Share Posted April 1, 2010 btw you could use #[tablename] for getting tables sizes, not with non-needed textCount = textCount+1 in current example you could use: texts[#texts+1]={ } texts[#texts+1]['text']=text texts[#texts+1]['time']=time --or table.insert(texts,{}) table.insert(texts["text"],text) table.insert(texts["time"],time) Link to comment
dzek (varez) Posted April 1, 2010 Author Share Posted April 1, 2010 nice idea karlis but im not sure which one is better (for speed of the script), - storing one integer variable (takes a bit of memory), - or checking table size every add (takes a bit of CPU) table.insert isn't good idea for me, as after that i will need to get insert key, so again, checking the table size, etc, which will result in longer script Link to comment
karlis Posted April 1, 2010 Share Posted April 1, 2010 then just use free=#texts+1 texts[free]={} ... Link to comment
dzek (varez) Posted April 1, 2010 Author Share Posted April 1, 2010 i think local free=#texts+1 will be better, as if free will be global variable, there will be no difference between textCount, or even a little bit more CPU due to counting table, instead of adding 1 Link to comment
karlis Posted April 1, 2010 Share Posted April 1, 2010 anyway we are talking about few miliseconds, so that, how script is readable and easy to write/edit is much more important Link to comment
dzek (varez) Posted April 1, 2010 Author Share Posted April 1, 2010 anyway we are talking about few miliseconds, so that, how script is readable and easy to write/edit is much more important i know, but almost no difference in looking, and ive got a lot of scripts, and they are slowing game a bit, i'll optimize the rest (some are downloaded), and new ones i wanna make perfect.. im a programmer, so im caring about details Link to comment
Dark Dragon Posted April 2, 2010 Share Posted April 2, 2010 no matter what others say, performance does matter. Link to comment
karlis Posted April 2, 2010 Share Posted April 2, 2010 well yes it does, but now we are talking about much solutions that almost dont change it Link to comment
dzek (varez) Posted April 2, 2010 Author Share Posted April 2, 2010 well yes it does, but now we are talking about much solutions that almost dont change it 100 "almost" scripts, and the game start to lag 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