Raised This Month: $ Target: $400
 0% 

Acessing structs inside arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ZzZombo
Member
Join Date: May 2012
Location: Ravenholm
Old 05-16-2012 , 06:50   Acessing structs inside arrays
Reply With Quote #1

Hello there! How to access a field of a struct inside an array? Example struct and array:
PHP Code:
struct PlayerStat
{
    
Kills,Deaths,
};

new 
PlayerStat:PlayerStats[MAXPLAYERS]; 
Thanks in advance.
ZzZombo is offline
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 05-16-2012 , 07:18   Re: Acessing structs inside arrays
Reply With Quote #2

struct is for internal usage, you can't interact with them from SourcePawn.
For struct-like functionality, you can use enumerator-indexed arrays.
__________________

Last edited by asherkin; 05-16-2012 at 07:35.
asherkin is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 05-16-2012 , 13:38   Re: Acessing structs inside arrays
Reply With Quote #3

I'll be crazy...

Is this right way add another "tag" inside other enum ??
Don't know of these.
Code:
enum PlayerStat
{
    Kills,
    Deaths,
    PlayerWeapon:lastwpn // Store last weapon ID
}
PHP Code:
// weapon names
new String:weapons[][] =
{
    
"none",    "knife",    "flashbang",    "smokegrenade",    "hegrenade",    "galil",    "ak47",    "scout",    "sg552",    "awp",    "g3sg1",    "famas",    "m4a1",
    
"aug",    "sg550",    "glock",    "usp",    "p228",    "deagle",    "elite",    "fiveseven",    "m3",    "xm1014",    "mac10",    "tmp",
    
"mp5navy",    "ump45",    "p90",    "m249"
}

// weapon ID's
enum PlayerWeapon
{
    
none,    knife,    flashbang,    smokegrenade,    hegrenade,    galil,    ak47,    scout,    sg552,    awp,    g3sg1,    famas,    m4a1,    aug,    sg550,    glock,    usp,
    
p228,    deagle,    elite,    fiveseven,    m3,    xm1014,    mac10,    tmp,    mp5navy,    ump45,    p90,    m249
}

enum PlayerStat
{
    
Kills,
    
Deaths,
    
PlayerWeapon:lastwpn // Store last weapon ID
}

new 
g_stats[MAXPLAYERS+1][PlayerStat];
new 
g_statsweapon[MAXPLAYERS+1][PlayerWeapon];

public 
OnPluginStart()
{
    
HookEventEx("player_death"player_death);
}

public 
OnClientConnected(client)
{
    
// Clear stats
    
new a;
    for(
0sizeof(g_stats[]); a++)
    {
        
g_stats[client][a] = 0;
    }

    for(
0sizeof(g_statsweapon[]); a++)
    {
        
g_statsweapon[client][a] = 0;
    }
}


public 
player_death(Handle:event, const String:name[], bool:dontBroadcast)
{
    
// There always victim
    
new victim GetClientOfUserId(GetEventInt(event"userid"));
    
g_stats[victim][Deaths] += 1;
    
PrintToChat(victim"Kills %i, Deaths %i"g_stats[victim][Kills], g_stats[victim][Deaths]);

    
// Attacker could be world or victim itself
    
new attacker GetClientOfUserId(GetEventInt(event"attacker"));
    if(
attacker != && attacker != victim)
    {
        
g_stats[attacker][Kills] += 1;

        
g_stats[attacker][lastwpn] = PlayerWeapon:0// Set weapon "none" just in case when weapon not found.

        
new String:weapon[30];
        
GetEventString(event"weapon"weaponsizeof(weapon));
        for(new 
0sizeof(weapons); a++)
        {
            if(
StrContains(weaponweapons[a], false) != -1)
            {
                
g_stats[attacker][lastwpn] = PlayerWeapon:a;
                break;
            }
        }

        
// new variable b with right tag PlayerWeapon:
        
new PlayerWeapon:g_stats[attacker][lastwpn];
        
g_statsweapon[attacker][b] += 1;

        
PrintToChat(attacker"Kills %i, Deaths %i, this weapon %s = kills %i",
                    
g_stats[attacker][Kills],
                    
g_stats[attacker][Deaths],
                    
weapons[b],
                    
g_statsweapon[attacker][b]
                    );
    }

Bacardi is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 05-17-2012 , 11:27   Re: Acessing structs inside arrays
Reply With Quote #4

Quote:
Originally Posted by Bacardi View Post
I'll be crazy...

Is this right way add another "tag" inside other enum ??
See this post about enums.
__________________
Not currently working on SourceMod plugin development.

Last edited by Powerlord; 05-17-2012 at 11:29. Reason: Removed the huge code block and stuff from the quote
Powerlord is offline
ZzZombo
Member
Join Date: May 2012
Location: Ravenholm
Old 05-19-2012 , 00:47   Re: Acessing structs inside arrays
Reply With Quote #5

Btw, why this syntax I mentioned in my first post is valid?
ZzZombo 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 05:18.


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