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

Looping an enum


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Michael Shoe Maker
Senior Member
Join Date: Apr 2016
Old 09-29-2016 , 11:42   Looping an enum
Reply With Quote #1

Is it possible to loop through an enum?

PHP Code:
ArrayList ValidRewards = new ArrayList();
            for(
int i2i++)
            {
                if(
Package[CoinRarity][i] != null)
                {
                    
ValidRewards.Push(i);
                }
            } 
PHP Code:
#define MAX_RARITY        6

enum PackageStructure
{
    
ArrayList:Credits null,
    
ArrayList:Weapons null
}

new 
Package[MAX_RARITY][PackageStructure]; 

Last edited by Michael Shoe Maker; 09-30-2016 at 16:32.
Michael Shoe Maker is offline
Michael Shoe Maker
Senior Member
Join Date: Apr 2016
Old 10-01-2016 , 13:00   Re: Looping an enum
Reply With Quote #2

I would hate to have to do something like this:
PHP Code:
#define MAX_RARTIES        4
#define PACKAGE_MAX        2
#define CREDITS            0
#define WEAPONS            1

ArrayList Packages[MAX_RARTIES][PACKAGE_MAX] = {{null, ...}, ...}; 
Michael Shoe Maker is offline
Mitchell
~lick~
Join Date: Mar 2010
Old 10-01-2016 , 13:27   Re: Looping an enum
Reply With Quote #3

Quote:
Originally Posted by Michael Shoe Maker View Post
I would hate to have to do something like this:
PHP Code:
#define MAX_RARTIES        4
#define PACKAGE_MAX        2
#define CREDITS            0
#define WEAPONS            1

ArrayList Packages[MAX_RARTIES][PACKAGE_MAX] = {{null, ...}, ...}; 
Why? This is easier to understand (imo). Also you don't need to initialize the arrays to null when they are already null.
Mitchell is offline
Michael Shoe Maker
Senior Member
Join Date: Apr 2016
Old 10-01-2016 , 17:17   Re: Looping an enum
Reply With Quote #4

Quote:
Originally Posted by Mitchell View Post
Why? This is easier to understand (imo). Also you don't need to initialize the arrays to null when they are already null.
Meh, I ended up with this (not what I would like though):
PHP Code:
kv.GetString("Weapons"sBuffersizeof(sBuffer), "");
            if(
strlen(sBuffer) > 0)
            {
                
Packages[iIndex][PACKAGE_WEAPONS] = new ArrayList(32);
                
                
int Strings ExplodeString(sBuffer" "sBuffer2Dsizeof(sBuffer2D), sizeof(sBuffer2D[]));
                for(
int iStringsi++)
                {
                    
Packages[iIndex][PACKAGE_WEAPONS].PushString(sBuffer2D[i]);
                }
            }

ArrayList ValidPackages = new ArrayList();
            for(
int iPACKAGE_MAXi++)
            {
                if(
Packages[CoinRarity][i] != null)
                {
                    
ValidPackages.Push(i);
                }
            } 
Michael Shoe Maker is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 10-01-2016 , 18:17   Re: Looping an enum
Reply With Quote #5

You can use enum structs for that, but you can't give them default values like you did.

PHP Code:
enum PackageStructure
{
    
ArrayList:Credits null,
    
ArrayList:Weapons null

should just be

PHP Code:
enum PackageStructure
{
    
ArrayList:Credits,
    
ArrayList:Weapons
}

new 
Package[MAX_RARITY][PackageStructure];

ArrayList ValidRewards = new ArrayList();
for(
int iview_as<int>(PackageStructure); i++)
{
    if(
Package[CoinRarity][i])
    {
        
ValidRewards.Push(i);
    }

__________________
Peace-Maker is offline
dustinandband
Senior Member
Join Date: May 2015
Old 03-27-2018 , 16:33   Re: Looping an enum
Reply With Quote #6

Quote:
Originally Posted by Peace-Maker View Post
You can use enum structs for that, but you can't give them default values like you did.

PHP Code:
enum PackageStructure
{
    
ArrayList:Credits null,
    
ArrayList:Weapons null

should just be

PHP Code:
enum PackageStructure
{
    
ArrayList:Credits,
    
ArrayList:Weapons
}

new 
Package[MAX_RARITY][PackageStructure];

ArrayList ValidRewards = new ArrayList();
for(
int iview_as<int>(PackageStructure); i++)
{
    if(
Package[CoinRarity][i])
    {
        
ValidRewards.Push(i);
    }


When referencing an enumeration list within a loop, is putting "_:" followed by the enum name the same as "view_as<int>" by default ?


Example:

PHP Code:
enum SIClasses
{
        
SMOKER_CLASS=1,
        
BOOMER_CLASS,
        
HUNTER_CLASS,
        
SPITTER_CLASS,
        
JOCKEY_CLASS,
        
CHARGER_CLASS,
        
WITCH_CLASS,
        
TANK_CLASS,
        
NOTINFECTED_CLASS
}

// another function
{
    for (new 
1view_as<int>(SIClasses); i++)
    {
        
Format(buffersizeof(buffer), "z_%s_health"SINames[i]);
        
PrintToChat(client"%s"buffer);
    }

Original plugin writes the loop using "_:SIClasses"

PHP Code:
    decl String:buffer[17];
    for (new 
1_:SIClassesi++) 
Plugin for reference~
https://github.com/Attano/L4D2-Compe...pting/1v1pm.sp
dustinandband is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 03-28-2018 , 01:24   Re: Looping an enum
Reply With Quote #7

Quote:
Originally Posted by dustinandband View Post
When referencing an enumeration list within a loop, is putting "_:" followed by the enum name the same as "view_as<int>" by default ?
There should be no difference between _:foo and view_as<int>(foo) besides being old and new syntax. It has nothing to do with enums.

Edit: Back to the original question, you can't do something like in the previous post or P-M's in general if your enum has an array in it (as you might use if you're trying to emulate structs). Those examples of iterating will only work if you know the starting value (as you can see, P-M's loop starts at 0 and dustinandband's at 1; they hardcoded the starting index to match their enums) and each enum member's value goes up by one. Well, I guess you could know they're all arrays of the same size, but such a case would be unusual.

Last edited by Fyren; 03-28-2018 at 01:29.
Fyren is offline
Reply



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 03:05.


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