samp.directory
← Skrypty i biblioteki

Mergevos/pawn-rbits

Support 16, 8, 4, 2 and 1-bit arrays!

Biblioteka★ 2zaktualizowano lis 2020Brak licencjiPawnsampctl
Otwórz na GitHubie

To repozytorium nie ma pliku licencji. Jest publiczne do odczytu, ale samo to nie daje pozwolenia na ponowne użycie.

Zainstaluj za pomocą sampctl

sampctl package install Mergevos/pawn-rbits

Uruchom to w folderze swojego projektu. sampctl pobiera pakiet i to, od czego zależy.

Wypróbuj w kompilatorze Pawn →

Dodaje jej pliki include, razem z bibliotekami, od których zależy, do kompilatora Pawn w twojej przeglądarce, gotowe do dołączenia w skrypcie.

Używany przez 1 pakiet

README.md

pawn-rbits

Ryder's rBits include because it doesn't have a Github repo.

Installation

Simply install to your project:

sampctl package install Mergevos/pawn-rbits

Include in your code and begin using the library:

#include <rbits>

Usage

Integer Limits

BitLimits
Bit10-1 (boolean)
Bit20-3
Bit40-15
Bit80-255 (char)
Bit160-65535

Declaration

To declare a Bit Array, use the following code.

new BitX: array<MAX_SIZE>;

Take into account that you must change X with a Bit Array type you want to create.
Thus, let's create Bit 4 nibble with a size of 32:

new
    Bit4: b4_Nibble <32>
;

Pretty easy, huh?

Setting and getting (originally written by RyDeR)

You can use the BitX_Set/Get function for this. For example:

Bit4_Set(b4_Nibble, 0, 4);

This will set b4_Nibble at index 0 to a value of 4.

Retrieving the value is as simple as:

Bit4_Get(b4_Nibble, 0);

This will return the value in b4_Nibble at index 0 which will be 4 in this case.

Examples (originally written by RyDeR)

Here are some simple examples of what I mean by "saving lots of memory". I often see people doing things like this:

#include <a_samp>
    
new
    bool: g_PlayerSpawned[MAX_PLAYERS]
;

public OnPlayerSpawn(playerid) {
    g_PlayerSpawned[playerid] = true;
}

public OnPlayerDeath(playerid) {
    g_PlayerSpawned[playerid] = false;
}

public OnPlayerDisconnect(playerid, reason) {
    g_PlayerSpawned[playerid] = false;
}

stock IsPlayerSpawned(playerid) {
    return g_PlayerSpawned[playerid];
}

The .amx size after compile is 726 bytes. Nothing much, but as you can see we're using 32-bit variables for only 0 and 1, so that's a waste of a lot memory. The most relevant bit type in this case would be 1-bit:

#include <a_samp>
#include <rBits>

new
    Bit1: g_PlayerSpawned <MAX_PLAYERS>
;

public OnPlayerSpawn(playerid) {
    Bit1_Set(g_PlayerSpawned, playerid, true);
}

public OnPlayerDeath(playerid) {
    Bit1_Set(g_PlayerSpawned, playerid, false);
}

public OnPlayerDisconnect(playerid, reason) {
    Bit1_Set(g_PlayerSpawned, playerid, false);
}

stock IsPlayerSpawned(playerid) {
    return Bit1_Get(g_PlayerSpawned, playerid);
}

The .amx size of this after compile is only 473 bytes. Probably not a big deal right now, but imagine with larger arrays or just a couple of more arrays like this.

This is just analog all the rest of the bit-types, just make sure you pick the right bit-type for your value.

FAQ (originally written by RyDeR)

  • How can I use this with enum?
  • You can just sort per bit type. For example for 1-bit arrays:
enum e_Bit1_Data {
    e_bSpawned,
    e_bIsDeath,
    e_bInDM,
    // ...
};

new
    g_Bit1_Data[e_Bit1_Data] <MAX_PLAYERS>
;

Bit1_Set(g_Bit1_Data[e_bSpawned], playerid, true);
Bit1_Get(g_Bit1_Data[e_bSpawned], playerid);
  • How to increase/decrease the value?
  • Since x++ is equal to x = x + 1 we can simply use that technique:
Bit4_Set(b4_Nibble, 0, Bit4_Get(b4_Nibble, 0) + 1);

Testing

To test, simply run the package and let y_testing do its job:

sampctl package run

Ta strona jest zbudowana z tego, co GitHub publikuje o repozytorium. samp.directory nie hostuje kodu. Zgłoś problem z tym wpisem

Mergevos/pawn-rbits: Biblioteka SA-MP | samp.directory