• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua FOR LOOP (Display text inside a foor loop one time)

waqmaz

Member
Joined
Jun 17, 2015
Messages
203
Reaction score
11
I know that it should be asked for example using stackoverflow, but it can be a curiosity for many otland users.
Code:
for i=1,5 do
   doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'Text 5 times.')
end
To avoid this problem I always do something like this:
Code:
local i = 0
for i=1,5 do
   i = i + 1
   if i == 1 then
      doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'Text 1 time.')
   end
end
But I do not like to do it that way, because the code looks ugly.
My question is: is there any other way I can display the text only one time? Thanks.
 
I know that it should be asked for example using stackoverflow, but it can be a curiosity for many otland users.
Code:
for i=1,5 do
   doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'Text 5 times.')
end
To avoid this problem I always do something like this:
Code:
local i = 0
for i=1,5 do
   i = i + 1
   if i == 1 then
      doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, 'Text 1 time.')
   end
end
But I do not like to do it that way, because the code looks ugly.
My question is: is there any other way I can display the text only one time? Thanks.
if you are going to do it the second way you dont need the local i
Code:
for i = 1, 5 do
    if i == 1 then
        ...
    end
    ...
end
 
why break?
most likely they have something else needed along with this loop.

The only way is the way opening post uses loop (even though the current one never works )

I obviously have no idea what he's trying to accomplish with what he posted. And the code he posted is basically functioning as a "break."

But I digress. Hopefully you are able to assist him. :p
 
I obviously have no idea what he's trying to accomplish with what he posted. And the code he posted is basically functioning as a "break."

But I digress. Hopefully you are able to assist him. :p
its not functioning as break, he has a loop, he wants to repeat something, but only once in entire loop session they want to send message.

break however would break out of the loop, closing the entire session, not just stopping the message.

The only way is
Actually there is another one.
Code:
local singleMessageSent = false

for k, v in pairs(loop) do
    if not singleMessageSent then
        sendMessage()
        singleMessageSent = true
    end
    ...
end
 
its not functioning as break, he has a loop, he wants to repeat something, but only once in entire loop session they want to send message.

break however would break out of the loop, closing the entire session, not just stopping the message.


Actually there is another one.
Code:
local singleMessageSent = false

for k, v in pairs(loop) do
    if not singleMessageSent then
        sendMessage()
        singleMessageSent = true
    end
    ...
end
Or It doesn't have to be inside the loop and it will be sent once :O :oops::oops:
 
I feel like @tetra20 is the only one who is confused as I am as to why he wants to do it the way he's wanting to. Why not call the function outside of the for loop if you only want it called once? Completely defeats the purpose of the loop.
 
I feel like @tetra20 is the only one who is confused as I am as to why he wants to do it the way he's wanting to. Why not call the function outside of the for loop if you only want it called once? Completely defeats the purpose of the loop.
There are reasons for it. Even I do singletons inside a loop.

Or It doesn't have to be inside the loop and it will be sent once :O
But he specifically asked ways how to do it inside a loop.
 
There are reasons for it. Even I do singletons inside a loop
I know there are reasons, but his example isn't a good one.

@waqmaz I personally suggest using a Boolean flag like @whitevo showed in his last example. In your example, you're increasing the i variable by 1 inside the for loop which effectively jumps an iteration and it will never fall into your if statement (unless Lua is different from every other language I've used; I don't do much Lua coding).
 
I know there are reasons, but his example isn't a good one.

@waqmaz I personally suggest using a Boolean flag like @whitevo showed in his last example. In your example, you're increasing the i variable by 1 inside the for loop which effectively jumps an iteration and it will never fall into your if statement (unless Lua is different from every other language I've used; I don't do much Lua coding).
It will never fall into his if statement cause i is going to be (2, 3, 4, 5, 6), adding stuff to the variable being looped does not change the for index in lua.

The for index is stored in a hidden local called "(for index)"

Code:
function changeLocalVariable(var, n)
    local idx = 1
    while true do
        local name, value = debug.getlocal(2, idx)
        if not name then
            break
        elseif name == var then
            debug.setlocal(2, idx, n)
            return value
        end
        idx = idx + 1
    end
    return false
end

for i = 1, 10 do
    if i == 3 then
        changeLocalVariable("(for index)", 9)
    end
    print(i)
end

This prints 1, 2, 3, 10.

There are also "(for limit)" and "(for step)".

https://github.com/LuaDist/lua/blob/master/src/lparser.c#L1309-L1328
 
Back
Top