Raised This Month: $12 Target: $400
 3% 

Solved Prevent double looping


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 07:20   Prevent double looping
Reply With Quote #1

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!!
__________________

Last edited by edon1337; 07-16-2018 at 13:23.
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-16-2018 , 09:03   Re: Prevent double looping
Reply With Quote #2

Just do it if you have to, there's nothing bad with that.
__________________
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 10:22   Re: Prevent double looping
Reply With Quote #3

Quote:
Originally Posted by KliPPy View Post
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.
__________________

Last edited by edon1337; 07-16-2018 at 10:23.
edon1337 is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 07-16-2018 , 10:48   Re: Prevent double looping
Reply With Quote #4

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.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo
EFFx is offline
CrAzY MaN
Senior Member
Join Date: Mar 2017
Location: India
Old 07-16-2018 , 10:49   Re: Prevent double looping
Reply With Quote #5

Use if else with a bool, or just use this!
__________________
CrAzY MaN is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 11:10   Re: Prevent double looping
Reply With Quote #6

Quote:
Originally Posted by EFFx View Post
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 View Post
Use if else with a bool, or just use this!
What?
__________________
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-16-2018 , 11:33   Re: Prevent double looping
Reply With Quote #7

I don't even understand what you mean by "double looping", nested loops are a normal thing.
__________________
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 11:45   Re: Prevent double looping
Reply With Quote #8

Quote:
Originally Posted by KliPPy View Post
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.
__________________

Last edited by edon1337; 07-16-2018 at 11:46.
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-16-2018 , 12:13   Re: Prevent double looping
Reply With Quote #9

Quote:
Originally Posted by edon1337 View Post
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.
__________________
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 07-16-2018 , 12:15   Re: Prevent double looping
Reply With Quote #10

Quote:
Originally Posted by KliPPy View Post
Yes, and what's the problem with that? You can nest loops as much as you want.
Quote:
Originally Posted by edon1337 View Post
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
__________________

Last edited by edon1337; 07-16-2018 at 12:35.
edon1337 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 21:39.


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