PDA

View Full Version : Getting their favorite weapon


2KY
06/04/2012, 03:43 PM
Well, I'm trying to create a function that gets their favorite weapon and tbh, i'm stuck;

My player variables are like so:

enum E_accVars {
Passcode, Cash, RegisteredIP[ 16 ],
Kills, Deaths, Status,
Model, KillsWithWeapon [ 42 ], DeathsByWeapon [ 42 ] //KillsWithWeapon & DeathsByWeapon are the variables used in this.
}

How said variables are set:

public OnPlayerDeath(playerid, killerid, reason)
{

GivePlayerMoney( playerid, 10 ), GivePlayerMoney( killerid, -100 );
//Give them $100 if they succeed in killing someone, and take $10 if they die. This should help create an economy.

if( reason >= 0 && reason < 43 ) {
accInfo [ killerid ] [ KillsWithWeapon ] [ reason ] += 1;
accInfo [ playerid ] [ DeathsByWeapon ] [ reason ] += 1;
}

player_Spawned [ playerid ] = false;
return 1;
}

What I've got so far is:

stock GetFavoriteWeapon( playerid )
{
new
favWepID = 0;

for( new w; w < 42; w ++ )
{
if( accInfo [ playerid ] [ KillsWithWeapon ] [ w ] != 0 )
{
//This is a viable candidate for their favorite weapon - they've used it before.
if( accInfo [ playerid ] [ KillsWithWeapon ] [ w ] > favWepID )
{
favWepID = w;
}
}
}
return favWepID;
}

Could someone assist me further?

Nero_3D
06/04/2012, 04:01 PM
In OnPlayerDeath you should check if the killerid is valid


public OnPlayerDeath(playerid, killerid, reason)
{
if( killerid != INVALID_PLAYER_ID )
{
if( reason < 43 )
{
accInfo [ killerid ] [ KillsWithWeapon ] [ reason ] += 1;
}
GivePlayerMoney( killerid, 100 );
}
player_Spawned [ playerid ] = false;

if( reason < 43 )
{
accInfo [ playerid ] [ DeathsByWeapon ] [ reason ] += 1;
}
GivePlayerMoney( playerid, -10 );

return 1;
}

Just some little changes and it was done


stock GetFavoriteWeapon( playerid )
{
new
weapon,
favWeapon,
kills = accInfo [ playerid ] [ KillsWithWeapon ] [ 0 ]
;
while( ++weapon < 43 )
{
if( kills < accInfo [ playerid ] [ KillsWithWeapon ] [ weapon ] )
{
kills = accInfo [ playerid ] [ KillsWithWeapon ] [ ( favWeapon = weapon ) ];
}
}
return favWeapon;
}

2KY
06/04/2012, 04:05 PM
In OnPlayerDeath you should check if the killerid is valid


public OnPlayerDeath(playerid, killerid, reason)
{
if( killerid != INVALID_PLAYER_ID )
{
if( reason < sizeof accInfo[][] )
{
accInfo [ killerid ] [ KillsWithWeapon ] [ reason ] += 1;
}
GivePlayerMoney( killerid, 100 );
}
player_Spawned [ playerid ] = false;

if( reason < sizeof accInfo[][] )
{
accInfo [ playerid ] [ DeathsByWeapon ] [ reason ] += 1;
}
GivePlayerMoney( playerid, -10 );

return 1;
}

Just some little changes and it was done


stock GetFavoriteWeapon( playerid )
{
new
kills,
weapon,
favWeapon
;
while( ++weapon < sizeof accInfo[][] )
{
if( kills < accInfo [ playerid ] [ KillsWithWeapon ] [ weapon ] )
{
kills = accInfo [ playerid ] [ KillsWithWeapon ] [ ( favWeapon = weapon ) ];
}
}
return favWeapon;
}

Thank you very much. :D