Difference between revisions of "IterElements"

From Multi Theft Auto: Wiki
(Created page with '{{Useful Function}} <lowercasetitle/> __NOTOC__ This function is useful for '''for''' loops. You don't have to type ''ipairs( getElementsByType( "player" ) )'' but simply ''iterE…')
(No difference)

Revision as of 22:04, 14 July 2010

This function is useful for for loops. You don't have to type ipairs( getElementsByType( "player" ) ) but simply iterElements( "player" ). It's very useful if you have many for loops in your code. doForAllElements has to iterate through elements every time you call 1 function per call but this iterates and what you do to the elements is up to you.


iterator iterElements( string elementType )

Required Arguments

  • elementType: Type of the elements that you want to iterate through.


function iterElements( elementType )
	local i = 0;
	local tab = getElementsByType( elementType );
	return function( )
		i = i + 1;
		if tab[ i ] then
			return i, tab[ i ];


This example will iterate through all players and vehicles. It will heal the players, give them $1000, send them a message informing them about the action and fix all vehicles when the resource starts.

addEventHandler( "onResourceStart", getResourceRootElement( ),
    function( )
        for _, plr in iterElements( "player" ) do
            setElementHealth( plr, 100 );
            givePlayerMoney( plr, 1000 );
            outputChatBox( "You've just been healed and given $1000!", plr );

        for _, veh in iterElements( "vehicle" ) do
            fixVehicle( veh );