C_Sharper
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 6405 |
|
|
| 25 Oct 2016 11:29 AM |
So say we have a data model containing different models with numbervalues in them called "RequiredLevel"
Like
WeaponInfo:
-Pistol (Required level = 1) -Shotgun (Required level = 5)
And so on.
However I totally forgot when I rendered buttons for every weapon you can buy, it's strange to have a common weapon appear, then a lvl 100 required weapon right after that. I need to figure out how to add every weapon to a table according to their required level value. From smallest to largest. Trying to figure this out :p
Thanks for reading!
|
|
|
| Report Abuse |
|
|
|
| 25 Oct 2016 11:33 AM |
I would transfer them into a table:
local Weapons = {}
for i,v in pairs (WeaponInfo:GetChildren()) do Weapons[WeaponLevel.Value] = v end
and then I would use table.sort:
table.sort(Weapons)
|
|
|
| Report Abuse |
|
|
C_Sharper
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 6405 |
|
|
| 25 Oct 2016 11:35 AM |
| Oh brainfart! Totally forgot about the possibility of dictionaries. Thank you! |
|
|
| Report Abuse |
|
|
C_Sharper
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 6405 |
|
|
| 25 Oct 2016 01:51 PM |
Erm, that doesn't work :/
This is what happens when I print everything in the table:
Number = key, and the gun = value.
1 Pistol 45 Ray Gun 3 Shotgun
Should be
1 Pistol 3 Shotgun 45 Ray Gun |
|
|
| Report Abuse |
|
|
C_Sharper
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 6405 |
|
|
| 25 Oct 2016 01:59 PM |
Meh, I figured a method, kinda inefficient though.
I sum up all the values of the keys, then use a for loop to run from 0 - X amount of times, gathering all the weapons from 0 - X.
Definitely not the best way. But it works. |
|
|
| Report Abuse |
|
|
|
| 25 Oct 2016 02:00 PM |
Is that what you're table looked like before?
I've never used sort before but I'm guessing this may work:
local Table = {} Table = table.sort(Table)
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 25 Oct 2016 02:03 PM |
local weapons = { { "SMG", 10 } { "Pistol", 3 } }
table.sort(weapons, function(v1, v2) return v1[2] < v2[2] end) |
|
|
| Report Abuse |
|
|
C_Sharper
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 6405 |
|
|
| 25 Oct 2016 02:03 PM |
Thanks for replying Skull.
I used table.sort and it didn't work when I tried it.
|
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 25 Oct 2016 02:03 PM |
Weapons = WeaponHolder:GetChildren() table.sort(Weapons, function(a,b) return a.RequiredLevel.Value < b.RequiredLevel.Value end)
|
|
|
| Report Abuse |
|
|
C_Sharper
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 6405 |
|
|
| 25 Oct 2016 02:04 PM |
| Thanks cnt, I'll try that! |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 25 Oct 2016 02:05 PM |
| If yours is set up the way chimmihc put it, use hise |
|
|
| Report Abuse |
|
|
C_Sharper
|
  |
| Joined: 03 Oct 2011 |
| Total Posts: 6405 |
|
|
| 25 Oct 2016 02:06 PM |
Lol I can't keep up with the replies.
Thank you chimm :)
And while I'm waiting for the reply cooldown to finish I'll just say thank you to anyone whom posts while I'm typing this. :P |
|
|
| Report Abuse |
|
|