Jump to content

Regarding for loops


myonlake

Recommended Posts

Hey,

I am just wondering, as I can't remember how it was done; how to make a function that will execute after the for loop has gone through everything. Currently I am doing like the following, and it doesn't work, and yes, I know the reason, but I am not sure how I can fix it so that it doesn't trigger before for loop as went through all the data. Anything else I should use or..?

-- Loop here 
for i,v in ipairs(getElementsByType("vehicle")) do 
    if getElementData(v, "example") then 
        -- Do something 
    end 
end 
-- Function here 
 

How should I organize it, or what should I do differently? That was just an example snippet by the way.

Edited by myonlake
Link to comment

If you by "some" mean roughly 1000... :P

Anyway, I really don't see the problem. Anything you put after the for loop in your code will be executed after the for loop - no mystery there. If you want to loop and then call a function... loop, and then call that function. Am I missing something?

What exactly are you trying to do, and what is the actual problem?

Link to comment
MTA Lua doesn't exist. "MTA Lua" is just Lua, with some functions added via cpp.

I have never used coroutines, so your best bet is to experiment with lua.org's examples at http://lua.org/demo.html

What I mean with MTA Lua is Lua within MTA.

If you by "some" mean roughly 1000... :P

Anyway, I really don't see the problem. Anything you put after the for loop in your code will be executed after the for loop - no mystery there. If you want to loop and then call a function... loop, and then call that function. Am I missing something?

What exactly are you trying to do, and what is the actual problem?

I think you didn't read my post. I quite clearly said that the functions after for-loop get executed before for-loop as gone through all the data. You don't need my code to know what's the problem, because the script is working perfectly, the problem is in that part. And you can only just explain me how and what should I do.

For example, think that you're currently making a sandwich. You eat it before it's finished. I want to eat it after it's finished.

Link to comment

I read your post (several times) and I still have no idea what the problem is. If you write A(); B(), Lua will do A, then do B. The fact that this is blindingly obvious aside, you don't really manage to make it clear WHAT the problem is. You're blaming Lua for... whatever it is... and claiming it runs your code in reverse, which is obviously not possible. The error or whatever it is is in your code, and you need to share it and explain what's wrong and what you're trying to do.

Link to comment
I read your post (several times) and I still have no idea what the problem is. If you write A(); B(), Lua will do A, then do B. The fact that this is blindingly obvious aside, you don't really manage to make it clear WHAT the problem is. You're blaming Lua for... whatever it is... and claiming it runs your code in reverse, which is obviously not possible. The error or whatever it is is in your code, and you need to share it and explain what's wrong and what you're trying to do.

He explained, you just didn't understand.

He meant that functions are executed before the loop ends. That means, the loop starts and the function get executed and not the loop get started then wait to it to end and execute the function.

Link to comment

I will edit this post with an example that will show you what I am meaning. You just don't get it.

EDIT: This will only output one data value into the gridlist. The reason why it is doing it, is because the for-loop is going through all vehicles with the data "testing", and the functions after the for-loop make as many vehicles with data "testing" as many vehicles there are in the game (before the actual for-loop, the script makes 20 vehicles so you see that it doesn't work). So the problem is, how can I prevent the functions to be executed during/before the for-loop. Gosh, how had it is to understand this plain English?

addCommandHandler("tests", 
    function(cmd) 
        triggerEvent("onReloadRequested", localPlayer) 
    end 
) 
  
addEvent("onReloadRequested", true) 
addEventHandler("onReloadRequested", root, 
    function() 
        for i=1,20 do 
            local vehicle = createVehicle(406, 0, 0, 3, 0, 0, 0) 
            setElementCollisionsEnabled(vehicle, false) 
        end 
        for i,v in ipairs(getElementsByType("vehicle")) do 
            for key,value in ipairs(getElementsByType("vehicle")) do 
                if getElementData(value, "testing") then 
                    destroyElement(value) 
                end 
            end 
            local vehicle = createVehicle(406, 2493.94, -1668.34, 13.34, 0, 0, 0) 
            setElementCollisionsEnabled(vehicle, false) 
            setElementFrozen(vehicle, true) 
            setElementData(vehicle, "testing", math.random(0, 100)) 
            runBoard() 
        end 
    end 
) 
  
function runBoard() 
    if isElement(g_runboard_gridlist) then 
        destroyElement(g_runboard_gridlist) 
    end 
    g_runboard_gridlist = guiCreateGridList(49, 401, 295, 359, false) 
    guiGridListSetSelectionMode(g_runboard_gridlist, 0) 
    guiSetAlpha(g_runboard_gridlist, 0.9) 
    local column = guiGridListAddColumn(g_runboard_gridlist, "Information", 0.92) 
    for i,v in ipairs(getElementsByType("vehicle")) do 
        if getElementData(v, "testing") then 
            local row = guiGridListAddRow(g_runboard_gridlist) 
            guiGridListSetItemText(g_runboard_gridlist, row, column, getElementData(v, "testing"), false, false) 
        end 
    end 
end 

So...

             -- BEFORE the functions are executed below 
            for key,value in ipairs(getElementsByType("vehicle")) do 
                if getElementData(value, "testing") then 
                    destroyElement(value) 
                end 
            end 
            -- AFTER the for-loop has digged all the data 
            local vehicle = createVehicle(406, 2493.94, -1668.34, 13.34, 0, 0, 0) 
            setElementCollisionsEnabled(vehicle, false) 
            setElementFrozen(vehicle, true) 
            setElementData(vehicle, "testing", math.random(0, 100)) 
            runBoard() 
            -- Shows only one data value, because the vehicles created after the for-loop are deleted because for-loop is digging. 

Link to comment

I did this, and it ran some stuff only in the end of the check:

local bState = false 
local iIndex = 0 
while not bState do 
    iIndex = iIndex + 1 
    if( getElementData( getElementsByType( 'vehicle' )[iIndex], 'testing' ) ) then 
        destroyElement( getElementsByType( 'vehicle' )[iIndex] ) 
    end 
    if( iIndex == #getElementsByType( 'vehicle' ) ) then 
        bState = true 
    end 
end 
--do stuff 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...