Hey. This is a revised version of my old mapArea iterator. This simplifies looping through areas.
My old iterator was a bit more confusing for developers who might have wanted to edit the iterator function itself. This time it's using coroutines so the function should be very easily customizable.
The old iterator was using stackpos, this one isn't. I assume it's irrelevant these days? It also fetched the creature on the tile however, which this one doesn't. But I plan to add it. I wanted some input from you guys how I should approach this. Just return top thing as second value or should we have an option to get e.g. TOP_CREATURE or TOP_ITEM or an option to allow it to loop through each item on every tile (without the necessity of a second loop)? Or a callback? Possibility to reverse loop?
I feel like this function never was much appreciated back when I made this, which confuses me. I fail to understand why anyone would like to create 3 loops in place of a single one, which also might save you a few more lines checking for a player etc...
Enough talk, here's the code, place this wherever you place your functions normally:
Simple example usage:
COMPARED to current way of looping:
If you are intending to use this function, don't forget to come back and look for updates in short. In the meantime, I would like some input on how I should be adding the thing, creature etc...
My old iterator was a bit more confusing for developers who might have wanted to edit the iterator function itself. This time it's using coroutines so the function should be very easily customizable.
The old iterator was using stackpos, this one isn't. I assume it's irrelevant these days? It also fetched the creature on the tile however, which this one doesn't. But I plan to add it. I wanted some input from you guys how I should approach this. Just return top thing as second value or should we have an option to get e.g. TOP_CREATURE or TOP_ITEM or an option to allow it to loop through each item on every tile (without the necessity of a second loop)? Or a callback? Possibility to reverse loop?
I feel like this function never was much appreciated back when I made this, which confuses me. I fail to understand why anyone would like to create 3 loops in place of a single one, which also might save you a few more lines checking for a player etc...
Enough talk, here's the code, place this wherever you place your functions normally:
Code:
do
local function mapAreaGen(fromPos, toPos)
for x = fromPos.x, toPos.x do
for y = fromPos.y, toPos.y do
for z = fromPos.z, toPos.z do
coroutine.yield(Position(x, y, z))
end
end
end
end
function mapArea(fromPos, toPos)
local co = coroutine.create(function () mapAreaGen(fromPos, toPos) end)
return function()
local _, pos = coroutine.resume(co)
return pos
end
end
end
Simple example usage:
Code:
local fromPos = Position(...)
local toPos = Position(...)
for position in mapArea(fromPos, toPos) do
-- position:sendMagicEffect(CONST_ME_MAGIC_BLUE)
end
COMPARED to current way of looping:
Code:
local fromPos = Position(...)
local toPos = Position(...)
for x = fromPos.x, toPos.x do
for y = fromPos.y, toPos.y do
for z = fromPos.z, toPos.z do
local position = Position(x, y, z)
-- position:sendMagicEffect(CONST_ME_MAGIC_BLUE)
end
end
end
If you are intending to use this function, don't forget to come back and look for updates in short. In the meantime, I would like some input on how I should be adding the thing, creature etc...