Returns the first key/value pair in the array. If a lastKey argument was specified then returns the next element in the array based on the key that provided. The order in which the indices are enumerated is not specified, even for numeric indices. To traverse a table in numeric order, use a numerical for loop or ipairs.
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may, however, modify existing fields. In particular, you may clear existing fields.
So print(next(module.damage)) would return the first key & value pair of the “module.damage” table.
The next function has 2 parameters the first of those is represents the table the next() function is acting upon and is required, the second which is optional represents some key of that table of which you wish to retrieve the associated value of.
Ok I tried the second index it seemed to work. Maybe next() doesn’t work with the first index since i was trying to do it with the first index. But I tried to add something in front of the [“10”] = 2500 and it still returns nil. Please help
module.damage = {...}
local orderListKeys = {}
function GetKeys(t)
for i,v in pairs(t) do
table.insert(orderListKeys,tonumber(i))
end
table.sort(orderListKeys)
print("--------------------------")
for i,v in pairs(orderListKeys) do
print(i,v)
end
print("--------------------------")
end
GetKeys(module.damage)
local currentValue = "10" --I think you mean currentLevel
local currentIndex = table.find(orderListKeys,tonumber(currentValue))
print("currentIndex",currentIndex)
local nextIndex = currentIndex + 1
print("nextIndex",nextIndex)
local nextValue = orderListKeys[nextIndex]
print("nextValue",nextValue)
print("nextDamage",module.damage[tostring(nextValue)])
If you’re still looking, you need to keep a separate array of dictionary keys, it’s important. I’ve said it before, dictionaries in Lua don’t have any order to them. Pairs and next are pretty close to the same thing by the way:
local function pairs(inputTable)
return next, inputTable
end
You can give the code I gave in my original solution a try.
module.healths = {
["100"] = 5000;
["110"] = 15000;
["120"] = 35000;
["130"] = 50000;
["145"] = 75000;
["160"] = 125000;
["185"] = 300000;
["200"] = 475000;
["225"] = 750000;
["255"] = 1050000;
["280"] = 1350000;
["320"] = 1850000;
}
module.healthsIndex = {}
for key, value in pairs(module.healths) do
table.insert(module.healthsIndex, key)
end
table.sort(module.healthsIndex, function(a, b) return module.healths[a] < module.healths[b] end) -- Sort by value rather than key
module.healthsNext = function(key)
local index = table.find(module.healthsIndex, key)
if index then
local newKey = module.healthsIndex[index+1]
if newKey then return newKey, module.healths[newKey]
else return "NaN", "MAX" end -- If there is nothing larger, return the key NaN and the value MAX
else
warn(("Unable to find key \"%s\" in module.healthsIndex"):format(key))
end
end
print(module.healthsNext("120")) --> 130, 50000