Question about IDs
#1

Okay I don't know much about it, but I wanted to know about something that has to do with IDs.
Bare with me here.

With unique IDs it's simple counting:
A = 1
B = 2
C = 3
D = 4
F = 5
G = 6

But if I wanted to combine ID's, I'd do:
A = 1
B = 2
C = 4
D = 8
E = 16
F = 32
G = 64
... etc.

And now I can A + B = 3. 3 will be A and B.

This is useful for such systems like a item system, that saves into a database.

new PlayerItem = 7; // Player has Item A, Item B, and Item C.

But my problem is, how do I check if player has Item B if PlayerItem is set to 7?
Reply
#2

Make a item type definition include for example.....

This is from my DayZ script it defines items.

pawn Код:
#define ITEM_TYPE_NONE 0
#define ITEM_TYPE_MEDKIT 1
#define ITEM_TYPE_BLOODBAG 1
#define ITEM_TYPE_PAINKILLERS 1

enum ItemSpawnData {
    Float:ItemSpawnOffsetX,
    Float:ItemSpawnOffsetY,
    Float:ItemSpawnOffsetZ,
    Float:ItemSpawnRX,
    Float:ItemSpawnRY,
    Float:ItemSpawnRZ,
    ItemModelid,
    ItemTexture,
    ItemNewColor,
    ItemVW,
    ItemInt,
    ItemType
}

enum LOOTDATA { LootType, LootAmount, LootRarity }

#define         RARITY_LEVEL_2          2
#define         RARITY_LEVEL_3          3
#define         RARITY_LEVEL_4          4
#define         RARITY_LEVEL_5          5
#define         RARITY_LEVEL_6          6
#define         RARITY_LEVEL_7          7
#define         RARITY_LEVEL_8          8
#define         RARITY_LEVEL_9          9

stock const ItemSpawnInfo[][ItemSpawnData] = {

    // Items
    { 0.000000, 0.000000, -1.039999, 0.000000, 0.000000, 0.000000, 1575, 291, 0, 0, 0, ITEM_TYPE_MEDKIT },
    { 0.000000, 0.000000, -0.970000, 0.000000, 0.000000, 0.000000, 1580, 213, 0, 0, 0, ITEM_TYPE_BLOODBAG },
    { 0.000000, 0.000000, -0.850000, 0.000000, 0.000000, 0.000000, 2709, 299, 0, 0, 0, ITEM_TYPE_PAINKILLERS }
};
Now you can reference item types by using the #define names example....
pawn Код:
if(PlayerItems[playerid][ItemType][ItemSlot] == ITEM_TYPE_MEDKIT) { SetPlayerHealth(playerid, 100.0); }
Reply
#3

I do not think you got what I was trying to say.
Reply
#4

Then explain it better, I don't see why you would want to combine items like your proposing anyways doesn't make sense to me.
Reply
#5

Alright.

Imagine a Database structure. (Skipping details.)

ID INT;
PlayerName VARCHAR;
PlayerWeapon1 INT;
PlayerWeapon2 INT;
PlayerWeapon3 INT;

Instead of that, so there isn't many rows, I can have:

ID INT;
PlayerName VARCHAR;
PlayerWeapon INT;

Create new combinations IDs such as:
Weapon 1 = 1
Weapon 2 = 2
Weapon 3 = 4

Then to save Weapon 1, Weapon 2, Weapon 3 I save it into the database as ID 7 (1 + 2 + 4 = 7).

But when I load the ID of the weapon, I want to give them the correct weapon, so I need to check if Weapon 2 is in that variable.

I've seen this done before I'm not really sure where, I understand it, it's like binary.
Reply
#6

There are a few tutorials on bitflags on this forum.
https://sampwiki.blast.hk/wiki/Binary#Binary_operators
Reply
#7

Can you give me example how bits work?

I think it'll be like:

Код:
new PlayerItem = 7;

if(PlayerItem & 0x2) {
 // true?
}
Код:
new PlayerItem = 5;

if(PlayerItem & 0x2) {
 // false?
}
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)