Skip to content

get the total number of players #42

@DavidBrenner3

Description

@DavidBrenner3

Hi.

I'm a C++/Delphi programmer.
I use the Open-SAMP-API dll in one of my applications.

I'm trying to get the total number of players. Some servers use it to create events at specific time intervals (more often when there are many players).

For now I'm using this function:

`function GetPlayerNameByID(id: Integer; var playername: PAnsiChar; max_len: Integer): Boolean; cdecl; external 'Open-SAMP-API.dll' name 'GetPlayerNameByID'

function GetNumberOfPlayers: Integer;
var
i: Integer;
buffer: array[0..31] of AnsiChar;
p: PAnsiChar;
begin
Result := 0;
i := 0;
while i < 420 do
begin
FillChar(buffer, Length(buffer), 0);
p := @buffer[0];
if GetPlayerNameByID(i, p, High(buffer)) then
if buffer[0] <> #0 then
Inc(Result);
Inc(i);
end;
end;`

Problems:

  1. It's very slow (it takes a few sec) but I need to display a value (which I got from it and some other variables) in real time without hogging one of the logical CPUs.
    Could be from calling SERVER_CHECK(0) 420 times when one time is enough, but I'm not sure,
  2. Since checking the first char from playername would suffice, I tried to lower the length of the buffer and the max_len parameter. But at some point the dll is crashing my app. Using a try..except doesn't help.
  3. I tried to compile the dll sources in Visual C++ 2010 but I end up with some strange compilation errors in German language.
    The gold was to add this function and then to send you the sources so others can benefit as well.

Can you help me please?
Thank you.

Merry Christmas.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions