Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func _ready():
- var A = Item.new("A", 0.1)
- var B = Item.new("B", 0.5)
- var C = Item.new("C", 0.7)
- for i in 10:
- print(drop_random_item([A, B, C]).name)
- class Item:
- var chance
- var name
- func _init(name, c):
- self.name = name
- self.chance = c
- func drop_random_item(items):
- var total_chance_sum = 0
- for i in items.size():
- total_chance_sum += items[i].chance
- var r = rand_range(0, total_chance_sum)
- var current_sum = 0
- for i in items.size():
- if (current_sum <= r && r < current_sum + items[i].chance): return items[i]
- current_sum += items[i].chance
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement