ITBV
|
  |
| Joined: 27 Dec 2013 |
| Total Posts: 954 |
|
|
| 15 Nov 2017 12:13 PM |
So I have a table like this;
materials = { ["mat1"] = { ["rarity"] = 1, }, ["mat2"] = { ["rarity"] = 1, }, ["mat3"] = { ["rarity"] = 2, }, }
What I'm trying to do is go through the table and get a random material, but only get that material if the rarity is 2 (This is just an example).
I'm confused how I would be able to do this, if anyone could give me some assistance that would be helpful. |
|
|
| Report Abuse |
|
|
|
| 15 Nov 2017 12:15 PM |
I would do it like this:
Loop through the table, save materials with a rarity of 2 to a second (temporary) table. Then choose a random one from that table. |
|
|
| Report Abuse |
|
|
ITBV
|
  |
| Joined: 27 Dec 2013 |
| Total Posts: 954 |
|
|
| 15 Nov 2017 12:16 PM |
| Ah that could work cheers. |
|
|
| Report Abuse |
|
|
|
| 15 Nov 2017 01:53 PM |
an alternate way would be to use repeat in a way such as this
local a = {}
for x = 0, 9, 1 do a[#a + 1] = x; end --[[Had to do this to get passed the stupid filter not allowing a table of numbers.]]
local selected; repeat selected = a[math.random(1, #a)] until (selected >= 5)
print(selected)
I'm not saying you should do this instead but Im bored so here you go |
|
|
| Report Abuse |
|
|
ITBV
|
  |
| Joined: 27 Dec 2013 |
| Total Posts: 954 |
|
|
| 15 Nov 2017 02:05 PM |
| That's the way I was thinking of originally but wasn't exactly sure how to write it, Cheers! |
|
|
| Report Abuse |
|
|
helloburp
|
  |
| Joined: 26 Aug 2011 |
| Total Posts: 14376 |
|
|
| 15 Nov 2017 02:34 PM |
I was given an algorithm for this when I asked about this earlier
paste blin
/Zf9uSAb3 |
|
|
| Report Abuse |
|
|