AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Prevent double looping (https://forums.alliedmods.net/showthread.php?t=309172)

edon1337 07-16-2018 07:20

Prevent double looping
 
Hi,

How can I prevent using a for() loop inside another for() loop, lol.
This is my current code
PHP Code:

public OnNewRound( )
{
    new 
Float:fOrigin], WeaponEntity;
    
    for( new 
i=1sizeof g_szWeaponsi++ )
    {
        if( 
SsGetOriginfOrigin ) )
        {
            if( 
g_szWeapons][ ] != EOS )
            {
                new 
WeaponBoxEntity OrpheuCallHandleCreateNamedEntityFuncIntClassNameString ) ;
                if( ! 
pev_validWeaponBoxEntity ) )
                {
                    continue;
                }
                
                
engfuncEngFunc_SetOriginWeaponBoxEntityfOrigin );
                
ExecuteHamHam_SpawnWeaponBoxEntity );
                
                
WeaponEntity create_entityg_szWeapons] );
                
                if( ! 
pev_validWeaponEntity ) )
                {
                    
remove_entityWeaponBoxEntity );
                }

                
ExecuteHamHam_SpawnWeaponEntity );
                new 
WeaponID cs_get_weapon_idWeaponEntity );
                
                
OrpheuCallHandlePackWeaponFuncWeaponBoxEntityWeaponEntity );
                if( 
WeaponBoxModelsWeaponID ][ ] != EOS 
                { 
                    
engfuncEngFunc_SetModelWeaponBoxEntityWeaponBoxModelsWeaponID ] );
                } 

                
set_pdata_intWeaponBoxEntitym_pfnThinkCWeaponBoxKill_Address);
            }
        }
    }
    return 
PLUGIN_CONTINUE;


Here's the simpler version of the code:
PHP Code:

public OnNewRound( )
{
    new 
Float:fOrigin], WeaponEntity;
    
    for( new 
i=1sizeof g_szWeaponsi++ )
    {
        if( 
SsGetOriginfOrigin ) )
        {
            
// weapon is created here
        
}
    }
}
return 
PLUGIN_CONTINUE;


Now I need to add this inside that code
PHP Code:

new iArraySize ArraySizeg_aWeaponData );

for( new 
iiArraySizei++ )
{
    
ArrayGetArrayg_aWeaponDataig_iWeapons );


So I can retrieve the data from my array.

Thanks!!

klippy 07-16-2018 09:03

Re: Prevent double looping
 
Just do it if you have to, there's nothing bad with that.

edon1337 07-16-2018 10:22

Re: Prevent double looping
 
Quote:

Originally Posted by KliPPy (Post 2603986)
Just do it if you have to, there's nothing bad with that.

I think it is bad, because every time the first loop is activated, it activates the 2nd loop, even if it has already finished. And it will not let the 2nd loop continue, it will be stuck.
Quote:

1st loop has 20 entries, so basically it will loop 20 times
2nd loop has 10 entries, so basically it needs to loop 10 times.
1st loop will keep looping and activating 2nd loop until it reaches 20th entry.

Real time example:

-> i = 0;
-> x = 0;

-> i = 1;
-> x = 0;

-> i = 2;
-> x = 0;
I had this problem once that's why I'm avoiding double looping

BUT, I realized I don't need the 2nd loop so I'm okay for now.

EFFx 07-16-2018 10:48

Re: Prevent double looping
 
Just a point: If the 1st loop hits g_szWeapons number, then no more weapons should be spawned, so it's ok to break the 2nd loop.

CrAzY MaN 07-16-2018 10:49

Re: Prevent double looping
 
Use if else with a bool, or just use this!

edon1337 07-16-2018 11:10

Re: Prevent double looping
 
Quote:

Originally Posted by EFFx (Post 2604000)
Just a point: If the 1st loop hits g_szWeapons number, then no more weapons should be spawned, so it's ok to break the 2nd loop.

I don't even need the 2nd loop anymore as I mentioned, but you're right.

Quote:

Originally Posted by CrAzY MaN (Post 2604002)
Use if else with a bool, or just use this!

What?

klippy 07-16-2018 11:33

Re: Prevent double looping
 
I don't even understand what you mean by "double looping", nested loops are a normal thing.

edon1337 07-16-2018 11:45

Re: Prevent double looping
 
Quote:

Originally Posted by KliPPy (Post 2604012)
I don't even understand what you mean by "double looping", nested loops are a normal thing.

Having a loop inside another one, basically a nested loop as you said.

klippy 07-16-2018 12:13

Re: Prevent double looping
 
Quote:

Originally Posted by edon1337 (Post 2604017)
Having a loop inside another one, basically a nested loop as you said.

Yes, and what's the problem with that? You can nest loops as much as you want.

edon1337 07-16-2018 12:15

Re: Prevent double looping
 
Quote:

Originally Posted by KliPPy (Post 2604022)
Yes, and what's the problem with that? You can nest loops as much as you want.

Quote:

Originally Posted by edon1337 (Post 2603996)
I think it is bad, because every time the first loop is activated, it activates the 2nd loop, even if it has already finished. And it will not let the 2nd loop continue, it will be stuck.

I had this problem once that's why I'm avoiding double looping

Here's the time I used a nested loop and had problems https://forums.alliedmods.net/showpo...9&postcount=19


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

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