bane_
Howardianism High-Priest
- Joined
- Nov 27, 2013
- Messages
- 1,559
I use this mini-function to get random values from tables:
It's been working quite well for me, but now that I'm doing some AI-tuning (holy crap, how this **** is hard) I'm getting this error:
It happens when it gets a nil interval (as explicitly told by the log
), BUT I wanna know why this is the first time I'm getting those.
I was using table.insert to put in IDs from workers, like this:
So I suspect the problem is that the ID from the Worker will not be contiguously, leaving small breaches between one number and the other. I imagine using CraneAvWorkers[iUnitID] (with iUnitID defined, obviously) will result in the same eventual errors.
Any ideas how to fix this?
Code:
function tablerandom(tbl)
local keys = {}
for k in pairs(tbl) do
table.insert(keys, k)
end
local randIndex = math.random(#keys) -- this is line 23
local randKey = keys[randIndex]
return tbl[randKey]
end
It's been working quite well for me, but now that I'm doing some AI-tuning (holy crap, how this **** is hard) I'm getting this error:
[32015.632] Runtime Error: C:\Users\bane_\Documents\My Games\Sid Meier's Civilization 5\MODS\Crane Clan (v 1)\Lua\Lib\DomotaiUtils.lua:23: bad argument #1 to 'random' (interval is empty)
It happens when it gets a nil interval (as explicitly told by the log

I was using table.insert to put in IDs from workers, like this:
Spoiler :
Code:
for pUnit in pPlayer:Units() do
if pUnit:GetUnitType() == GameInfoTypes["UNIT_KAKITA_ARTISAN"] and pUnit:GetPlot():GetOwner() == iPlayer then
table.insert(CraneAvWorkers, pUnit:GetID())
bCraneAvWorkers = true
print("A Crane worker was inserted into the 'CraneAvWorkers' table")
elseif pUnit:GetUnitType() == GameInfoTypes["UNIT_KAKITA_ARTISAN"] and not pUnit:IsBusy() and pUnit:GetPlot():GetOwner() ~= iPlayer and pUnit:GetPlot():GetOwner() >= 0 then
local pPlot = pUnit:GetPlot()
local iUnit = pUnit:GetID()
LookForSuitableGardens(iUnit, pPlot, false)
print("Looking for suitable place for a garden with boolean as 'false'.")
end
end
So I suspect the problem is that the ID from the Worker will not be contiguously, leaving small breaches between one number and the other. I imagine using CraneAvWorkers[iUnitID] (with iUnitID defined, obviously) will result in the same eventual errors.
Any ideas how to fix this?