- Joined
- Jun 26, 2020
- Messages
- 1,921
Hello, I'm trying to make an Item drop system, but when the main function is called the game freezes, I don't know what's wrong.
Lua:
local instances = {}
---Makes the creep have a chance of drop an item when it dies.
---If the chances is an empty table, then it will be assume that
---all the items will have the same chance.
---
---The optional boolean is to make the creep don't drop items again
---in case you wanna resurrect it
---@param creep unit
---@param items integer[]
---@param chances real[]
---@param once? boolean
function AddItemDrop(creep, items, chances, once)
if #items == 0 then
error("You didn't add items", 2)
end
if #chances > 0 and #items ~= #chances then
error("The number of items don't matches with the number or chances", 2)
end
local sum = 0
for _, v in ipairs(chances) do
sum = sum + v
end
if sum > 100 then
error("The sum of the chances are bigger than 100", 2)
end
if #chances == 0 then
local max = #items
local chance = 100/max
for i = 1, max do
chances[i] = chance
end
end
sum = 0
for i = 1, #chances do
sum = sum + chances[i]
chances[i] = sum
end
chances[0] = 0
local new = {
items = items,
chances = chances,
once = once
}
if not instances[creep] then
instances[creep] = {}
end
table.insert(instances[creep], new)
end