Is it possible to do this in a shorter way?
for rep = 1, 15 do
--blah different coordinate calculations
for rep = 15, 30 do
--blah different different coordinates calculations
for rep = 30,45 do
--blah different coordinates calculations
for rep = etc....... do
--blah different coordinates calculations
end
end
end
end
My first thought was to use arrays. Will this work? If yes, how would I have starter begin 0 and ender begin at 15?
for 1,15 do
it would end up being:
15,30
30,45
45,60
etc
local starter={}
local ender ={}
for rep 1,15 do
starter[rep] = 0
ender[rep] = 0
end
for reprep = 1,15 do
starter[reprep]=starter[reprep] +15
ending[reprep] = starter[reprep] +15
for rep = starter[reprep], ending[reprep] do
--blah different coordinates calculations arrays
end
end
Quote from: zutokaza on November 10, 2016, 08:21:59 AM
Is it possible to do this in a shorter way?
for rep = 1, 15 do
--blah different coordinate calculations
for rep = 15, 30 do
--blah different different coordinates calculations
for rep = 30,45 do
--blah different coordinates calculations
for rep = etc....... do
--blah different coordinates calculations
end
end
end
end
You probably noticed this: this part of code is completely wrong in many ways. You should avoid using the same variable name ("rep", in this case) for nested variables, since the new variable is not the same one: although the syntax is correct, the nested variable that has the same name ("rep") is a different variable that is hiding the other variable ("rep") of the outer scope. Besides, your inner loop will be executed 15 x 15 = 225 times aproximately instead of once, which is not what you want; so just put one loop after another and not one nested inside the other. I guess that your second and third loops should start with index 16 and 31 instead of 15 and 30.
Quote from: zutokaza on November 10, 2016, 08:21:59 AM
My first thought was to use arrays. Will this work? If yes, how would I have starter begin 0 and ender begin at 15?
Yes, you can do that. Note that loops can start and end by any numeric value. You can also add an optional step (which can be negative if you want). Check some Lua tutorial: https://www.lua.org/pil/4.3.4.html
You can do shorter
for i = 1, whatever_you_want do
if i < 15 then
-- calculation
elseif i < 30 then
-- other calculation
elseif i < 45
-- Again
end
end
This is recommanded to do so if you plan to do exponential coordination, for instance, admit that i[0; 15] have a fixed position of x + 15 where x = 10 (example)
simple way is
if i <= 15 then
table:draw(surface, 10 + (15 * i))
elseif i <= 30 then
-- Other position
table:draw(surface, 24 + (13 * i))
end
And it does the job
In the case of your example
local starter={}
local ender ={}
for i = 1, 15 do
-- First, check if i = 1, if yes, then the start value would be 0
local start = i == 1 and 0 or starter[i]
-- starter and ender share the same result apparently ?
starter[i] = start + 15
ender[i] = starter[i]
end
Now let's assume you want to increment the starter and ender array in a different way, but still exponentially
local starter={}
local ender ={}
for i = 1, 30 do
-- First, check if i = 1, if yes, then the start value would be 0
local start = i == 1 and 0 or starter[i]
local increment
if i <= 15 then
increment = 15
elseif i <= 30 then
increment = -30
end
-- starter and ender share the same result apparently ?
starter[i] = start + increment
ender[i] = starter[i]
end
Not tested but that should work
Quote from: MetalZelda on November 10, 2016, 12:54:30 PM
You can do shorter
for i = 1, whatever_you_want do
if i < 15 then
-- calculation
elseif i < 30 then
-- other calculation
elseif i < 45
-- Again
end
end
This works. Thank you MetalZelda. :D