Any better way to do this?
#1

I am making an inventory system and I plan on having alot of items, my way of defining their name is kinda awful if you ask me, it will take alot of space but I can't think of a better way, this is how I define item names.

If inventory empty
PHP код:
if(PlayerInfo[playerid][pItem1] == 0) { test[0] = "EMPTY"; }
    if(
PlayerInfo[playerid][pItem2] == 0) { test[1] = "EMPTY"; }
    if(
PlayerInfo[playerid][pItem3] == 0) { test[2] = "EMPTY"; }
    if(
PlayerInfo[playerid][pItem4] == 0) { test[3] = "EMPTY"; }
    if(
PlayerInfo[playerid][pItem5] == 0) { test[4] = "EMPTY"; }
    if(
PlayerInfo[playerid][pItem6] == 0) { test[5] = "EMPTY"; }
    if(
PlayerInfo[playerid][pItem7] == 0) { test[6] = "EMPTY"; } 
If player has an apple
PHP код:
if(PlayerInfo[playerid][pItem1] == 1) { test[0] = "APPLE"; }
    if(
PlayerInfo[playerid][pItem2] == 1) { test[1] = "APPLE"; }
    if(
PlayerInfo[playerid][pItem3] == 1) { test[2] = "APPLE"; }
    if(
PlayerInfo[playerid][pItem4] == 1) { test[3] = "APPLE"; }
    if(
PlayerInfo[playerid][pItem5] == 1) { test[4] = "APPLE"; }
    if(
PlayerInfo[playerid][pItem6] == 1) { test[5] = "APPLE"; }
    if(
PlayerInfo[playerid][pItem7] == 1) { test[6] = "APPLE"; } 
and so on..
Reply
#2

When you see stuff like "Something[pItem1], Something[pItem2]" you can be certain that you can simplify it to an array. So, instead of
pawn Код:
enum PlayerInfoOrSomeShit {
    //(...)
    pItem1,
    pItem2,
    //(...)
}
Just do
pawn Код:
#define MAX_INV_ITEMS 7

enum PlayerInfoOrSomeShit {
    //(...)
    pItem[MAX_INV_ITEMS],
    //(...)
}
Why define? So later in your code instead of
pawn Код:
if(PlayerInfo[playerid][pItem1] == 1) { test[0] = "APPLE"; }
    if(PlayerInfo[playerid][pItem2] == 1) { test[1] = "APPLE"; }
    if(PlayerInfo[playerid][pItem3] == 1) { test[2] = "APPLE"; }
    if(PlayerInfo[playerid][pItem4] == 1) { test[3] = "APPLE"; }
    if(PlayerInfo[playerid][pItem5] == 1) { test[4] = "APPLE"; }
    if(PlayerInfo[playerid][pItem6] == 1) { test[5] = "APPLE"; }
    if(PlayerInfo[playerid][pItem7] == 1) { test[6] = "APPLE"; }
You can do simply
pawn Код:
for(new i = 0; i != MAX_INV_ITEMS; ++i) {
    if(PlayerInfo[playerid][pItem][i] == 1) {
        test[i] = "APPLE";
    }
}
You can go further:
pawn Код:
static const InventoryItems[][32] = {
    "Empty",
    "Loaf of bread"  
};

for(new i = 0; i != MAX_INV_ITEMS; ++i) {
    for(new j = 0; j != sizeof(InventoryItems); ++j) {
        if(PlayerInfo[playerid][pItem][i] == j) {
            strcpy(test[i], InventoryItems[j], 32);
        }
    }
}
Reply
#3

Nevermind - someone already showed it , didnt refresh.
Reply
#4

Thanks, will test it out
Reply
#5

Or you can use switch for that
Reply
#6

Uh.. but how can I save it?

Example, slot1 was pItem1, slot2 was pItem2 and if pItem1 was higher than 0 which means that has an item already then it will switch to pItem 2 and store the info in the mysql database.

How can I store more items in the database if I use pItem[i]?


And also how to load?

Something like this?

PHP код:
cache_get_value_int(0"Item1"PlayerInfo[playerid][pItem[1]]);
                
cache_get_value_int(0"Item2"PlayerInfo[playerid][pItem[2]]);
                
cache_get_value_int(0"Item3"PlayerInfo[playerid][pItem[3]]);
                
cache_get_value_int(0"Item4"PlayerInfo[playerid][pItem[4]]);
                
cache_get_value_int(0"Item5"PlayerInfo[playerid][pItem[5]]);
                
cache_get_value_int(0"Item6"PlayerInfo[playerid][pItem[6]]);
                
cache_get_value_int(0"Item7"PlayerInfo[playerid][pItem[7]]); 
Reply
#7

Why not using switch instead....That would be a lot easier and more organized...
PS:Wait let me get on PC..
Reply
#8

Quote:
Originally Posted by princejeet1510
Посмотреть сообщение
Why not using switch instead....That would be a lot easier and more organized...
Can you show me an example please?
Reply
#9

Still not on PC but I guess it would similar I didn't tested it... I hope it's not right way but for an example just
PHP код:
switch(PlayerInfo[playerid][pItem])
{
   Case 
1test[0] = "APPLE"
   Case 
2test[1] = "APPLE";
   
Bla bla bla.....

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)