AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Rank in a for loop (https://forums.alliedmods.net/showthread.php?t=132498)

tm. 07-15-2010 14:00

Rank in a for loop
 
I don't know how to explain this, so:

Code:
new iPlayers[32], iNum     get_players(iPlayers, iNum, "ch")         for(new i; i < iNum; i++)     {         //from here no idea ?         if(g_something[iPlayers][0] > g_something[iPlayer][1])?         ...........................................         console_print(id, "1. Best player, 2.Second, 3.....     }

Mxnn 07-15-2010 16:13

Re: Rank in a for loop
 
PHP Code:

new maxplayers get_maxplayers()
for (new 
1i<=maxplayersi++) 
     for (new 
j<=maxplayersi++)
            if (
g_something[i] < g_something[j]) {
                      new 
aux g_something[i]
                      
g_something[i] = g_something[j]
                      
g_something[j] = aux          
            
}

//When you have all the players in order, you show them
for (new 1i<=maxplayersi++) {
     new 
name[32]
     
get_user_name(g_something[i], name31)
     
console_print(id"%d. %s"iname)


I use 2 loops for more prolixity, but you can show the first player before the first loop ("i") ends

Bugsy 07-15-2010 16:31

Re: Rank in a for loop
 
Explain where/how info is stored that determines a players rank. Are players already in order of ranks in that array.

Mxnn, you should not declare variables within a loop.

grimvh2 07-15-2010 18:18

Re: Rank in a for loop
 
Quote:

Originally Posted by Bugsy (Post 1240532)

Mxnn, you should not declare variables within a loop.

Why? just asking :p

ot_207 07-15-2010 18:21

Re: Rank in a for loop
 
Quote:

Originally Posted by grimvh2 (Post 1240696)
Why? just asking :p

Because it is inefficient.
By adding new to a for loop you basically delete and realloc memory for every cycle.
It is just a use of CPU for something that can be avoided or something useless.
Ex:
PHP Code:

for (new i=0;i<500;i++)
{
  new 
x;


Means:

PHP Code:

// 500 times alloc and reloc
for (new i=0;i<500;i++)
{
  
alloc memory x;
// code
  
delete memory x;



grimvh2 07-15-2010 20:07

Re: Rank in a for loop
 
Quote:

Originally Posted by ot_207 (Post 1240704)
Because it is inefficient.
By adding new to a for loop you basically delete and realloc memory for every cycle.
It is just a use of CPU for something that can be avoided or something useless.
Ex:
PHP Code:

for (new i=0;i<500;i++)
{
  new 
x;


Means:

PHP Code:

// 500 times alloc and reloc
for (new i=0;i<500;i++)
{
  
alloc memory x;
// code
  
delete memory x;



oh ofc, i misunderstood hes sentence, yes im aware of that :p

Mxnn 07-16-2010 00:41

Re: Rank in a for loop
 
I edited my post.
Thanks for the tip Bugsy. I do it so because i have seen it in some plugins

Bugsy 07-16-2010 00:49

Re: Rank in a for loop
 
Quote:

Originally Posted by Mxnn (Post 1240514)
PHP Code:

new maxplayers get_maxplayers()
new 
ij
for (1i<=maxplayersi++) 
     for (
j<=maxplayersi++)
            if (
g_something[i] < g_something[j]) {
                      new 
aux g_something[i]
                      
g_something[i] = g_something[j]
                      
g_something[j] = aux          
            
}

//When you have all the players in order, you show them
for (1i<=maxplayersi++) {
     new 
name[32]
     
get_user_name(g_something[i], name31)
     
console_print(id"%d. %s"iname)



I think you misunderstood what we are telling you. It is ok to use new in the for-loop line to create your index variable. Where you do not want to use new is inside of the loop code block.
PHP Code:

new maxplayers get_maxplayers()
new 
aux
for ( new 1i<=maxplayersi++) 
     for ( new 
j<=maxplayersi++)
            if (
g_something[i] < g_something[j]) {
                      
//move outside of loop new aux = g_something[i]
                      
aux g_something[i]
                      
g_something[i] = g_something[j]
                      
g_something[j] = aux          
            
}

//When you have all the players in order, you show them
new name32 ]
for (new 
1i<=maxplayersi++) {
     
//move outside of loop new name[32] 
     
get_user_name(g_something[i], name31)
     
console_print(id"%d. %s"iname)



tm. 07-16-2010 00:57

Re: Rank in a for loop
 
Quote:

Originally Posted by Bugsy (Post 1240532)
Explain where/how info is stored that determines a players rank. Are players already in order of ranks in that array.

I don't use it anywhere at this thime, but I might need to. It was a general question. The info's are just global arrays that could store integers, floats etc, and no, they are not in any particular order. It's not necessary to be in a loop, I just need a way to rank some values and I thought in a for loop could be done.

@Mxnn: Thanks, I'll give it a try later, I can't test anything right now and they are talking about this -> http://wiki.alliedmods.net/Optimizin...ables_in_Loops.

Mxnn 07-16-2010 02:02

Re: Rank in a for loop
 
Ohhh, i missunderstood, sorry!

@tm. My post is an example. I don't know what class of data you want to put in order.


All times are GMT -4. The time now is 07:10.

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