Jump to content

In pairs inventory table


Slimwaffle
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

 

  • Thanks 1

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

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

 

  • Like 1

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

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...