Check if player is online by mysql id?
#1

I'm currently messing up some codes.. trying to find a way how I can get a users playerid to send them a message IF they are logged on. Here's what I got currently (doesn't work at all):
Код:
stock GetPIDByDID(did)
{
	for(new i = 0; i <= MAX_PLAYERS; i++)
	{
		if(PlayerIsOn(i))
		{
			printf("Returned database ID %d", PlayerInfo[i][pDatabaseID]);
			return PlayerInfo[i][pDatabaseID];
		}
	}
	return INVALID_PLAYER_ID;
}
Reason I want that is because of this: VehicleInfo[vehicleid][carOwner] = DatabaseID
Код:
if(PlayerIsOn(GetPIDByDID(VehicleInfo[vehicleid][carOwner])))
				{
					format(msg, sizeof(msg), "Your %s has been rekt %d.",VehicleNames[GetVehicleModel(vehicleid)-400],VehicleInfo[vehicleid][carDestroyed]);
					SCM(GetPIDByDID(VehicleInfo[vehicleid][carOwner]), COLOR_PINK, msg);
					format(msg, sizeof(msg), "You have %d lives left.",VehicleInfo[vehicleid][carInsurances]);
					SCM(GetDIDByPID(VehicleInfo[vehicleid][carOwner]), COLOR_PINK, msg);
					PlayerInfo[GetPIDByDID(VehicleInfo[vehicleid][carOwner])][pCarKey] = 0;
					CheckOwnedVehicles(GetPIDByDID(VehicleInfo[vehicleid][carOwner]));
				}
So, when a car gets destroyed and the vehicle owner is online - I'd like to send him a message, telling him that his car is rekt. To do that I assume I will have to first check if he's online, then get his in-game playerid and retrieve it so I can send him a message.
Reply
#2

"CarOwner" holds the user ID? Simply run a player-loop and check if the "pDatabaseID" is equal to.

About this:
pawn Код:
for(new i = 0; i <= MAX_PLAYERS; i++)
If the size of PlayerInfo array is MAX_PLAYERS, it will go out of bounds. If you don't want to use foreach which is recommended, at least use player-pool which was added in 0.3.7:
pawn Код:
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
Reply
#3

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
"CarOwner" holds the user ID? Simply run a player-loop and check if the "pDatabaseID" is equal to.

About this:
pawn Код:
for(new i = 0; i <= MAX_PLAYERS; i++)
If the size of PlayerInfo array is MAX_PLAYERS, it will go out of bounds. If you don't want to use foreach which is recommended, at least use player-pool which was added in 0.3.7:
pawn Код:
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
CarOwner holds the database ID.
Reply
#4

The unique database ID per player, that's what I'm saying.

pawn Код:
if (VehicleInfo[vehicleid][carOwner]) // if it is owned by anyone
{
    for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
    {
        if (PlayerInfo[i][pDatabaseID] == VehicleInfo[vehicleid][carOwner])
        {
            // Send message to inform player - "i" holds their in-game ID.
           
            break; // stop the loop, result was found
        }
    }
}
Reply
#5

Thanks. I'd like to make it a stock though as I'll be using it elsewhere as well. I tried, but apparently it's an unreachable code. I assume it has to do with the return, but don't get why. No matter where I place it, I get that error.

Код:
stock GetIDbyVDID(vehicleid)
{
	for(new i = 0, j = GetPlayerPoolSize(); i <= j; i++)
    {
        if (PlayerInfo[i][pDatabaseID] == VehicleInfo[vehicleid][carOwner])
        {
            // Send message to inform player - "i" holds their in-game ID.
            printf("Returning %d", i);
            break; // stop the loop, result was found
            return i;
        }
    }
    return INVALID_PLAYER_ID;
}
Reply
#6

You cannot use both break and return one after another.

PHP код:
GetIDbyVDID(vehicleid)
{
    for(new 
0GetPlayerPoolSize(); <= ji++)
    {
        if (
PlayerInfo[i][pDatabaseID] == VehicleInfo[vehicleid][carOwner])
        {
            
printf("Returning %d"i);
            return 
i;
        }
    }
    return 
INVALID_PLAYER_ID;

What do you reset "CarOwner" to so we won't have to loop through players when a vehicle is not owned by anyone?

Be it 0, INVALID_PLAYER_ID or whatever, add it at the top of the function.
pawn Код:
if (VehicleInfo[vehicleid][carOwner] == /* RESET VALUE/NO OWNER */) return INVALID_PLAYER_ID;
and then usage:
pawn Код:
new p_id = GetIDbyVDID(vehicleid);
if (p_id != INVALID_PLAYER_ID) // necessary if you want to use "p_id" in arrays.
{
    // messages or what you want..
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)