AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Loops (https://forums.alliedmods.net/showthread.php?t=10647)

v3x 02-25-2005 20:48

Loops
 
How would I make something loop 5 times with for()?

Edit: Is this right?
Code:
 for (i=1; i<=5; i++) {    something[i] }

XxAvalanchexX 02-25-2005 21:54

Yep, just make sure to declare i

v3x 02-25-2005 21:56

Heh, knew that. Thanks :).

Only ones I've ever worked with are while() loops using MySQL..

BlueRaja 02-26-2005 00:28

uhh..but remember that arrays go from 0 to n-1...
That is, to access all the elements of array something[]...
Code:
new something[5] for (new i=0; i<5; i++)     something[i]

Peli 02-26-2005 00:40

Sorry to steal his attention but , how would you loop a task? For example , one to take away 5 hp every second for 10 seconds , thanks.

v3x 02-26-2005 01:08

Quote:

Originally Posted by Peli
Sorry to steal his attention but , how would you loop a task? For example , one to take away 5 hp every second for 10 seconds , thanks.

Like rings of regeneration on WC3, hehe..

Anyhow, I'd like to know that too :).

Peli 02-26-2005 01:33

Yeah , because I'm working on a plugin that needs that... but I tried looking into the WC3 code... one of them just linked me to a piece of code , another was so huge I couldn't even find the functions I was looking for...

v3x 02-26-2005 01:35

OOO, actually...take a look @ xeroblood's AIO menu :).

Poison..
Code:
public poison_loop( p_aCmdArgs[] ) {     new vID = p_aCmdArgs[0]     if( is_user_alive(vID) && (g_nHasEffect[vID-1] & AIO_POISON) )     {         new userHealth = get_user_health( vID )         new nDmgAmount = get_cvar_num( "sv_aio_poison_damage" )         new nNewHP = (userHealth - nDmgAmount)         set_user_health( vID, nNewHP )         if( nNewHP < 1 )         {             message_begin( MSG_BROADCAST, g_nMsgDeath, {0,0,0}, 0 )             write_byte( vID )             write_byte( vID )             write_byte( 0 )             write_string( "infection" )             message_end()         }         new korigin[3]         get_user_origin( vID, korigin )         message_begin( MSG_ONE, g_nMsgDamage, {0,0,0}, vID )         write_byte( 30 )             // dmg_save         write_byte( 30 )             // dmg_take         write_long( 1 << 16 )        // visibleDamageBits         write_coord( korigin[0] )    // damageOrigin.x         write_coord( korigin[1] )    // damageOrigin.y         write_coord( korigin[2] )    // damageOrigin.z         message_end()         emit_sound( vID, CHAN_AUTO, g_szSndPoison, 0.6, ATTN_NORM, 0, PITCH_HIGH )         set_task( 1.0, "poison_loop", 0, p_aCmdArgs, 2 )     }

xeroblood 02-26-2005 12:14

Yes, that method will work, and is called Recursion (when a functon calls itself).

Here is a simpler example:

Code:
#include <amxmodx> // variable to keep track of each players loop times new g_iLoops[33] = 0 public plugin_init() {     register_clcmd( "do_slap_loop", "SlapLoop" ) } public SlapLoop( id ) {     g_iLoops[id] = 10     new victimID[2]     victimID[0] = id     set_task( 1.0, "MyLoopFunc", 0, victimID, 1 )     return PLUGIN_HANDLED } public MyLoopFunc( aCmdArgs[] ) {     new victimID = aCmdArgs[0]     // do stuff here...     user_slap( victimID, 5 ) // Slap victim with 5 hp     // Minus 1 from loops     --g_iLoops[victimID]     // Call self again in 1 second..     if( g_iLoops[victimID] > 0 )         set_task( 1.0, "MyLoopFunc", 0, aCmdArgs, 2 )     return }

v3x 02-26-2005 12:24

Nice :).


All times are GMT -4. The time now is 14:02.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.