Slimwaffle Posted July 13, 2018 Share Posted July 13, 2018 I am working on an inventory script. I have the thing pretty much working the way I want. I just want it to sort alphabetically using in pairs. How can I adjust the code below to still do the same things but also sort alphabetically? --Material Item List mitem ={} mitem[0] = "Wood" mitem[1] = "Steel" mitem[2] = "Concrete" mitem[3] = "Rubber" for i=0, #mitem do local selected=false GameMenu.materials:AddItem( mitem) end GameMenu.usematerial = Widget:Button("Trash",250,y,72,30,ipanel) y=y+sep Quote Link to comment Share on other sites More sharing options...
macklebee Posted July 13, 2018 Share Posted July 13, 2018 table.sort() will sort the table alphabetically. mitem ={} mitem[1] = "Wood" mitem[2] = "Steel" mitem[3] = "Concrete" mitem[4] = "Rubber" System:Print("Unsorted Table******") for k,v in ipairs(mitem) do System:Print(v) end table.sort(mitem) System:Print("Sorted Table********") for k,v in ipairs(mitem) do System:Print(v) end will result in the following output: Quote Unsorted Table****** Wood Steel Concrete Rubber Sorted Table******** Concrete Rubber Steel Wood 1 Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Josh Posted July 13, 2018 Share Posted July 13, 2018 Note that Lua arrays are 1-based. Don't start with zero. 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
macklebee Posted July 13, 2018 Share Posted July 13, 2018 12 hours ago, Josh said: Note that Lua arrays are 1-based. Don't start with zero. yep - sorry i forgot to mention that above but I was getting ready to leave for work when i posted Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
Josh Posted July 14, 2018 Share Posted July 14, 2018 Using table.insert is an easy way to avoid that confusion: https://www.leadwerks.com/learn?page=Tutorials_Lua-Scripting_Tables --Create a new table mytable={} --Insert some items into the table table.insert(mytable,"Thing 1") table.insert(mytable,"Thing 2") table.insert(mytable,"Thing 3") for i=1, #mytable do Print(mytable[i]) end 1 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
Slimwaffle Posted July 18, 2018 Author Share Posted July 18, 2018 thanks heaps. I got this working perfectly. My materials list for inventory is working great. I did post another thread though. Because I am having an issue with the scrap button. Where I can't call self (in the code for scrap function it returns nill) and values on my generic item script aren't storing to each entity and they are changing each time I add a model into the scene and using the value of the last placed model. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.