AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   while() cpu load 100% (https://forums.alliedmods.net/showthread.php?t=92354)

xPaw 05-14-2009 07:33

while() cpu load 100%
 
I have this code
PHP Code:

#include <amxmodx>
#include <engine>

public plugin_init( ) {
    
register_plugin"Reset Buttons""1.0""xPaw" );
    
    
register_event"HLTV""evNewRound""a""1=0""2=0" );
}

public 
evNewRound( ) {
    new 
iButton    = -1;
    new 
Float:flGametime get_gametime();
    new 
Float:flNextthinkFloat:flLTime;
    
    while( ( 
iButton find_ent_by_classiButton"func_button" ) != ) ) {
        
flNextthink entity_get_floatiButtonEV_FL_nextthink );
        
flLTime entity_get_floatiButtonEV_FL_ltime );
        
        if( 
flNextthink flLTime && flNextthink flGametime ) {
            
entity_set_floatiButtonEV_FL_nextthinkflLTime );
        }
    }


When it starts new round, server just freezes and cpu usage goes to 100% :\

--kml-- 05-14-2009 09:20

Re: while() cpu load 100%
 
oo maybe change new to static?

Code:

#include <amxmodx>
#include <engine>

public plugin_init( ) {
   
register_plugin( "Reset Buttons", "1.0", "xPaw" );
   
   
register_event( "HLTV", "evNewRound", "a", "1=0", "2=0" );
}

public
evNewRound( ) {
    static
iButton    = -1;
    static
Float:flGametime = get_gametime();
    static
Float:flNextthink, Float:flLTime;
   
    while( (
iButton = find_ent_by_class( iButton, "func_button" ) != 0 ) ) {
       
flNextthink = entity_get_float( iButton, EV_FL_nextthink );
       
flLTime = entity_get_float( iButton, EV_FL_ltime );
       
        if(
flNextthink > flLTime && flNextthink > flGametime ) {
           
entity_set_float( iButton, EV_FL_nextthink, flLTime );
        }
    }


maybe will worK? xD :mrgreen:

Bugsy 05-14-2009 10:19

Re: while() cpu load 100%
 
Quote:

Originally Posted by --kml-- (Post 827273)
oo maybe change new to static?

maybe will worK? xD :mrgreen:

That is only beneficial if:
  • The function is being called very frequently. Here it is not
  • If the variable requires a lot of memory allocation . Here it does not
  • If you wish the data to remain static [stored in memory between function calls]. Here it is not necessary

Also, FYI, you cannot set a static variables value while declaring.

PHP Code:

//You cannot do:

static iVar 5;

//you CAN do:

static iVariVar 5


--kml-- 05-14-2009 10:22

Re: while() cpu load 100%
 
ooo i will add that to my referring list thx xD :mrgreen:

Arkshine 05-14-2009 10:50

Re: while() cpu load 100%
 
@xPaw :

while( ( iButton = find_ent_by_class( iButton, "func_button" ) != 0 ) ) {

->

while( ( iButton = find_ent_by_class( iButton, "func_button" ) ) != 0 ) {

xPaw 05-14-2009 11:26

Re: while() cpu load 100%
 
@arkshine: o_O i was so lame :mrgreen: thanks

--kml-- 05-14-2009 20:09

Re: while() cpu load 100%
 
oo even that mistake could make 100% cpu usage O.O added to my referring list xD

joaquimandrade 05-14-2009 20:42

Re: while() cpu load 100%
 
Quote:

Originally Posted by --kml-- (Post 827613)
oo even that mistake could make 100% cpu usage O.O added to my referring list xD

You understood why? It was a infinite loop

Bugsy 05-14-2009 22:44

Re: while() cpu load 100%
 
Misplaced closing ) I assume

Hunter-Digital 05-15-2009 08:27

Re: while() cpu load 100%
 
Yes, don't assume =) because this:
PHP Code:

while( ( iButton find_ent_by_classiButton"func_button" ) != ) ) 

it will be ALWAYS true, because what it checks is if iButton = find_ent_by_class() is not 0, and it does not matter what find_ent_by_class() returns because it checks if the assignment worked, and always does, therefore, infinite loop
the above mistake in less code:
PHP Code:

while(var = != 0

the "var = 0" can be anything that returns true, if you want the result of it, you just:
PHP Code:

while((var = 0) != 0

and the loop will stop, because var is 0 and results false for the loop


All times are GMT -4. The time now is 01:28.

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