lets say distance is = 30, that would mean it is larger than 0, but not large enough to be larger than 41 so it should print 1 because that is designed number for that distance. I want to make it without for i loops, nor more than 1 if statement, just using table[x} For some reason it prints nil, can somebody help out?
--
local index = 3
local color
--
local colors = {
[Color3.fromRGB(60, 45, 0)] = {
["minimum"] = 1,
["maximum"] = 3,
},
[Color3.fromRGB(0, 80, 0)] = {
["minimum"] = 4,
["maximum"] = maxIndex,
}
}
--
for possibleColor, data in colors do
if index >= data.minimum and index <= data.maximum then
color = possibleColor
end
end
--
(In your method, 55 is greater than 0 so it would be 1, but its also great than 41 so it would also be 2. It just wont work with 1 variable, so you need a dictionary with 2)
local distance = 30
local thresholds = {
{limit = 0, value = 1},
{limit = 41, value = 2},
{limit = 90, value = 3},
{limit = 260, value = 4},
}
local result
for i = #thresholds, 1, -1 do
if distance > thresholds[i].limit then
result = thresholds[i].value
break
end
end
print(result)
Data is data, logic is logic; it’s best to keep them separated. Here, you’re mixing data with logic.
yea thats what i wanted, its bigger than 41 so it will be 2 because nuimbers 41+ are asigned to “2”, also is there a way to do that with no for i,v loops? thats what im looking for
local distance = 55
local thresholds = {
{limit = 0, value = 1},
{limit = 41, value = 2},
{limit = 90, value = 3},
{limit = 260, value = 4},
}
local value = thresholds[1].value
if distance > thresholds[2].limit then value = thresholds[2].value end
if distance > thresholds[3].limit then value = thresholds[3].value end
if distance > thresholds[4].limit then value = thresholds[4].value end
print(value)
There are only two real solutions to this problem, which is doing it procedurally (using a loop, as shown before) or using a pre-computed look-up table. The alternative is to generate a table with each possible input value,
-- Setup
-- List of your desired lower limits and values as pairs
local _secDictionary = {
{0, 1},
{41, 2},
{90, 3},
{260, 4}
}
-- Sequential array containing each possible key in the range, assuming integer keys
local secDictionary = table.create(_secDictionary[#_secDictionary][1])
-- Setting keys 1 to #secDictionary in secDictionary with the corresponding value
for k, v in ipairs(_secDictionary) do
local val = v[2]
local stop = (_secDictionary[k + 1] or v)[1] -- Get the next limit
for i = v[1], stop do
secDictionary[i] = val
end
end
-- Usage
local distance = 55
-- If you have a known upper bound, you can exclude this check
if distance <= #secDictionary then
print(secDictionary[distance])
else
print(secDictionary[#secDictionary])
end
The problem is similar to weighted random distribution. You might want to look into solutions for those to see if any are relevant, too.
There is no feature in Lua to perform ranges like this. Use a function and it’ll be a one-liner regardless of complexity.