Inventory
#1

Hi, I'm trying to create an inventory system right now, it works well but the GiveItem function doesn't work. Like if I give myself an item then the name is blank, but everything else works (count, etc).
Enum:
Код:
enum inventoryInfo
{
	ItemName[32],
	ItemCount
}
new InventoryInfo[MAX_PLAYERS][25][inventoryInfo];
GiveItem function:
Код:
GiveItem(playerid, item[], amount)
{
	printf("%s", item); // <---- I checked if the item is blank here, but it's not. :P
	
	for (new i; i < 25; i++) //this part should check if player already has the item and just add the amount to the item's count
		if (!strcmp(InventoryInfo[playerid][i][ItemName], item)) return InventoryInfo[playerid][i][ItemCount] += amount;
		
	for (new i; i < 25; i++) if (!InventoryInfo[playerid][i][ItemCount]) //if player doesn't have the item already then it should find a free slot and give him the item
	{
		format(InventoryInfo[playerid][i][ItemName], 32, item);
		InventoryInfo[playerid][i][ItemCount] = amount;
		return 1;
	}
	return 0;
}
ShowInventory function (works like it's supposed to)
Код:
ShowInventory(playerid)
{
	new inventory[1250], item[50];

	for (new i; i < 25; i++)
	{
		if (InventoryInfo[playerid][i][ItemCount])
		{
			if (InventoryInfo[playerid][i][ItemCount] == 1) format(item, sizeof(item), "{FFFFFF}%d. %s\n", i + 1, InventoryInfo[playerid][i][ItemName]);
			else format(item, sizeof(item), "{FFFFFF}%d. %s (%d)\n", i + 1, InventoryInfo[playerid][i][ItemName], InventoryInfo[playerid][i][ItemCount]);
		}
		else format(item, sizeof(item), "{999999}%d. Empty Slot\n", i + 1);

		strcat(inventory, item);
	}
	ShowPlayerDialog(playerid, DIALOG_INVENTORY, DIALOG_STYLE_LIST, "Inventory", inventory, "Select", "Cancel");
	return 1;
}
This is what it looks like in-game: https://gyazo.com/54524c83e6ea7a5ebf2b792f71d399d6
As you can see the inventory system works itself, but GiveItem not. I gave myself a Cellphone by setting ItemName and ItemCount manually in a command. What's the problem?
Reply
#2

Are you saving the inventory data? If so, is the names saving? If you're not saving the data, please try using printf("%s", the variable); and tell me what it returns.
Reply
#3

It's only obvious, because your code has no logic to it.
Reply
#4

Sooo WTF is this? DDDDDDD
Reply
#5

As I told you, the GiveItem function works itself, but the item's name is blank. That's something with the format function.
Reply
#6

Anyone? I updated the post with some explainations.
Reply
#7

pawn Код:
GiveItem(playerid, item[], amount)
{
    for(new i; i < 25; i++)
    {
        if(!strcmp(InventoryInfo[playerid][i][ItemName], item))
        {
            return InventoryInfo[playerid][i][ItemCount] += amount;
        }
    }
       
    for(new i; i < 25; i++)
    {
        if(!InventoryInfo[playerid][i][ItemCount])
        {
            format(InventoryInfo[playerid][i][ItemName], 32, item);
            return InventoryInfo[playerid][i][ItemCount] = amount;
        }
    }
    return 0;
}
If it doesn't give you the item, then "InventoryInfo[playerid][i][ItemCount]" is not 0.
Reply
#8

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
pawn Код:
GiveItem(playerid, item[], amount)
{
    for(new i; i < 25; i++)
    {
        if(!strcmp(InventoryInfo[playerid][i][ItemName], item))
        {
            return InventoryInfo[playerid][i][ItemCount] += amount;
        }
    }
       
    for(new i; i < 25; i++)
    {
        if(!InventoryInfo[playerid][i][ItemCount])
        {
            format(InventoryInfo[playerid][i][ItemName], 32, item);
            return InventoryInfo[playerid][i][ItemCount] = amount;
        }
    }
    return 0;
}
If it doesn't give you the item, then "InventoryInfo[playerid][i][ItemCount]" is not 0.
It does give me the item, but the item's name is always blank.
Reply
#9

Oh, yeah....

format(InventoryInfo[playerid][i][ItemName], 32, "%s", item);

Or

strcat(InventoryInfo[playerid][i][ItemName], item);

If you use strcat, equal the string to end of string "e.g. string[0] = EOS;" when removing the item.
Reply
#10

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Oh, yeah....

format(InventoryInfo[playerid][i][ItemName], 32, "%s", item);

Or

strcat(InventoryInfo[playerid][i][ItemName], item);

If you use strcat, equal the string to end of string "e.g. string[0] = EOS;" when removing the item.
None of them work.
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)