SA-MP Forums Archive
Is there an more efficient way to do this? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Is there an more efficient way to do this? (/showthread.php?tid=609442)



Is there an more efficient way to do this? - xTURBOx - 12.06.2016

PHP код:
DestroyObject(ResprayHangar[1]);
    
DestroyObject(ResprayHangar[2]);
    
DestroyObject(ResprayHangar[3]); 
^^ title says it all

PS: i am hoping for something like loop through out the array and destroy the objects?
PPS:
PHP код:
//global variables
new ResprayHangar[3]; 



Re: Is there an more efficient way to do this? - lolumadd_ - 12.06.2016

Код:
DestroyHangar()
{
	for(new i=0; i < sizeof ResprayHangar; i++) // loop through all indexes of respray hangar
	{
		if(IsValidObject(ResprayHangar[i])) // check if the object was created...maybe not all variable indexes contain object id's
		{
			DestroyObject(ResprayHangar[i]); // destroy the object
		}
	}	
}



Re: Is there an more efficient way to do this? - SickAttack - 12.06.2016

Quote:
Originally Posted by lolumadd_
Посмотреть сообщение
Код:
DestroyHangar()
{
	for(new i=0; i < sizeof ResprayHangar; i++) // loop through all indexes of respray hangar
	{
		if(IsValidObject(ResprayHangar[i])) // check if the object was created...maybe not all variable indexes contain object id's
		{
			DestroyObject(ResprayHangar[i]); // destroy the object
		}
	}	
}
"more efficient way to do this"

Laying it out directly is faster than a loop.

pawn Код:
DestroyObject(ResprayHangar[1]);
DestroyObject(ResprayHangar[2]);
DestroyObject(ResprayHangar[3]);



Re: Is there an more efficient way to do this? - xTURBOx - 12.06.2016

ok thanks,


Re: Is there an more efficient way to do this? - lolumadd_ - 12.06.2016

Code functionality is more important than saving 1 nano second of processing speed.


Re: Is there an more efficient way to do this? - xTURBOx - 12.06.2016

PHP код:
 if(checkpointid == CP[1] || CP[2] || CP[3] || CP[4]) 
So this is the "efficient" way to do this?


Re: Is there an more efficient way to do this? - lolumadd_ - 12.06.2016

Quote:
Originally Posted by xTURBOx
Посмотреть сообщение
PHP код:
 if(checkpointid == CP[1] || CP[2] || CP[3] || CP[4]) 
So this is the "efficient" way to do this?
Well, that's not the right syntax for an if statement with multiple "or" operators. Here are your options:

Код:
if(checkpointid == CP[1] || checkpointid == CP[2] || checkpointid == CP[3] || checkpointid == CP[4])
or

Код:
for(new i=0; i <= 4; i++)
{
	if(checkpointid == CP[i])
	{
		// do code for entering CP
		break; // break out of the loop
	}
}
Honestly, at your level of experience, I wouldn't worry too much about efficiency. There are many ways you could optimize your code in these situations but it probably wouldn't be much use to you. Rather, just learn how to get stuff functioning.