Advertisement
LMC_dd

Untitled

Oct 15th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. func _ready():
  2. var A = Item.new("A", 0.1)
  3. var B = Item.new("B", 0.5)
  4. var C = Item.new("C", 0.7)
  5. for i in 10:
  6. print(drop_random_item([A, B, C]).name)
  7.  
  8. class Item:
  9. var chance
  10. var name
  11. func _init(name, c):
  12. self.name = name
  13. self.chance = c
  14.  
  15. func drop_random_item(items):
  16. var total_chance_sum = 0
  17. for i in items.size():
  18. total_chance_sum += items[i].chance
  19. var r = rand_range(0, total_chance_sum)
  20. var current_sum = 0
  21. for i in items.size():
  22. if (current_sum <= r && r < current_sum + items[i].chance): return items[i]
  23. current_sum += items[i].chance
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement