DrWaffler
|
  |
| Joined: 16 Sep 2011 |
| Total Posts: 4248 |
|
|
| 14 Dec 2013 12:06 PM |
I posted a similar thread yesterday, but I've nailed down the path I wish to take, and am back for more questions and discussion on the subject.
Let's say I have this table:
rarityTable = { ["Hut"] = { CutStone = 60, Grass = 10, Rock = 20, Stick = 10 }, ["AbandonedCamp"] = { CutStone = 85, Grass = 5, Rock = 5, Stick = 5 }, ["DeadBody"] = { CutStone = 40, Grass = 10, Rock = 40, Stick = 10 }, ["Table"] = { CutStone = 40, Grass = 10, Rock = 25, Stick = 25 } }
The names represent different sites where loot will spawn, and inside of them you have the different items that could spawn, and their probability of spawning out of 100 for examples sake.
What I'm wanting to do is have one function that will choose one of those randomly based on percentage chance to spawn. Keep in mind not every location will have only 4 items, and they will not always be the same name.
This is my struggle, help is appreciated.
#TheStruggleIsReal
--[Territory Conquest Developer, Valerian Studios Developer, Business Casual Narwhal Moai. ]]-- |
|
|
| Report Abuse |
|
|
crouton04
|
  |
| Joined: 07 Jul 2010 |
| Total Posts: 4459 |
|
|
| 14 Dec 2013 12:20 PM |
lol simple simple simple... if i am understanding correctly.
I'll use this example
local tab = { h = 50; g = 20; f = 30 }
function randomt(tab) tat = {} for i,v in pairs(tab) do for x = 1,v[i] do table.insert(tat, v) end end return tat[math.random(1,#tat)] end
randompick = randomt(tab)
print(randompick)
-- h has a 50% possibility to be chosen -- g has a 20 -- f has a 30
|
|
|
| Report Abuse |
|
|
DrWaffler
|
  |
| Joined: 16 Sep 2011 |
| Total Posts: 4248 |
|
|
| 14 Dec 2013 12:22 PM |
Oh wow! I slapped myself when I saw how you did that!
Thanks!
--[Territory Conquest Developer, Valerian Studios Developer, Business Casual Narwhal Moai. ]]-- |
|
|
| Report Abuse |
|
|
crouton04
|
  |
| Joined: 07 Jul 2010 |
| Total Posts: 4459 |
|
| |
|
DrWaffler
|
  |
| Joined: 16 Sep 2011 |
| Total Posts: 4248 |
|
|
| 14 Dec 2013 12:26 PM |
I caught the mistake already, it works exceptionally well.
--[Territory Conquest Developer, Valerian Studios Developer, Business Casual Narwhal Moai. ]]-- |
|
|
| Report Abuse |
|
|