Item system help
#1

I followed a tutorial on how to make items that you can pickup/drop from the ground, but I've run into an issue.

The maximum amount of items is 100, so when I want to control an individual item, I have to run it through a loop, which ends up creating 100 items in one place. I'm not sure how to fix it.

pawn Код:
stock CreateItem(model, amount, type, Float:X, Float:Y, Float:Z, Float:RX, Float:RY, Float:RZ, world, interior)
{
    if(model != 0)
    {
        for(new i=0; i<MAX_ITEMS; i++)
        {
            if(Item[i][ItemPosX] == 0 && Item[i][ItemPosY] == 0 && Item[i][ItemPosZ] == 0)
            {
                Item[i][ItemModel] = model;
                Item[i][ItemAmount] = amount;
                Item[i][ItemType] = type;
                Item[i][ItemPosX] = X;
                Item[i][ItemPosY] = Y;
                Item[i][ItemPosZ] = Z;
                Item[i][ItemRotX] = RX;
                Item[i][ItemRotY] = RY;
                Item[i][ItemRotZ] = RZ;
                Item[i][ItemVWorld] = world;
                Item[i][ItemInterior] = interior;
                LoadItemVisual();
                //ItemLabel[i] = Create3DTextLabel("ItemName", COLOR_BLUE, X, Y, Z, 5, world, 0);
                DropObject[i] = CreateObject(model, X, Y, Z, RX, RY, RZ, world);
            }
        }
    }
    return 1;
}

stock DropItem(model, amount, type, Float:X, Float:Y, Float:Z, world, interior)
{
    if(model != 0)
    {
        for(new i=0; i<MAX_ITEMS; i++)
        {
            if(Item[i][ItemPosX] == 0 && Item[i][ItemPosY] == 0 && Item[i][ItemPosZ] == 0)
            {
                Item[i][ItemModel] = model;
                Item[i][ItemAmount] = amount;
                Item[i][ItemType] = type;
                Item[i][ItemPosX] = X;
                Item[i][ItemPosY] = Y;
                Item[i][ItemPosZ] = Z;
                Item[i][ItemVWorld] = world;
                Item[i][ItemInterior] = interior;
                LoadItemVisual();
                //ItemLabel[i] = Create3DTextLabel("ItemName", COLOR_BLUE, X, Y, Z, 5, world, 0);
                DropObject[i] = CreateObject(model, X, Y, Z-1, 0, 0, 0, world);
            }
        }
    }
    return 1;
}

stock LoadItems()
{
    CreateItem(2806, 1, 1, 346.54266, 2507.15649, 15.47086,   0.00000, 0.00000, 0.00000, 0, 0);
    CreateItem(2806, 1, 2, 351.54187, 2507.21704, 15.47086,   0.00000, 0.00000, 0.00000, 0, 0);
    CreateItem(2806, 1, 1, 351.48746, 2499.85474, 15.47086,   0.00000, 0.00000, 0.00000, 0, 0);
    CreateItem(2806, 1, 2, 345.39008, 2499.46338, 15.47086,   0.00000, 0.00000, 0.00000, 0, 0);
    return 1;
}
I suspect it has something to do with creating an item as it will create 100 items at the first X,Y,Z coords, but like I said, I have no clue what's wrong or how to fix it.
Reply
#2

If it creates 100 items in the same spot, then you need to break the loop once it creates the first item.

Simply put
pawn Код:
break;
After you create the object.

So:
pawn Код:
DropObject[i] = CreateObject(model, X, Y, Z, RX, RY, RZ, world);
break;
Reply
#3

this method

PHP код:
stock CreateItem 
is being executed 100 times, because of the loop, and you're creating items with it. So, just do it without the For loop:

PHP код:

new 0;

stock CreateItem(modelamounttypeFloat:XFloat:YFloat:ZFloat:RXFloat:RYFloat:RZworldinterior)
{
    if(
model != 0)
    {
       
            
            {
                
Item[i][ItemModel] = model;
                
Item[i][ItemAmount] = amount;
                
Item[i][ItemType] = type;
                
Item[i][ItemPosX] = X;
                
Item[i][ItemPosY] = Y;
                
Item[i][ItemPosZ] = Z;
                
Item[i][ItemRotX] = RX;
                   
Item[i][ItemRotY] = RY;
                
Item[i][ItemRotZ] = RZ;
                
Item[i][ItemVWorld] = world;
                
Item[i][ItemInterior] = interior;
                
LoadItemVisual();
                
//ItemLabel[i] = Create3DTextLabel("ItemName", COLOR_BLUE, X, Y, Z, 5, world, 0);
                
DropObject[i] = CreateObject(modelXYZRXRYRZworld);
                           
                           
i++;
            }
        
    }
    return 
1;

Reply
#4

Quote:
Originally Posted by EiresJason
Посмотреть сообщение
If it creates 100 items in the same spot, then you need to break the loop once it creates the first item.

Simply put
pawn Код:
break;
After you create the object.

So:
pawn Код:
DropObject[i] = CreateObject(model, X, Y, Z, RX, RY, RZ, world);
break;
It seems I overlook things like this and the fix is relatively simple :P, thanks for the help.
Reply
#5

Rule of thumb when designing dynamic systems.

- The return value for the creation function should be the INDEX in which the item was created.
- All slots full? return -1

Furthermore......

Код:
if(Item[i][ItemPosX] == 0 && Item[i][ItemPosY] == 0 && Item[i][ItemPosZ] == 0)
That is very silly to do and another rule of thumb never ever use == when checking floats!
What you should do is this.

Define your item types
Код:
#define ITEM_TYPE_NONE 0
#define ITEM_TYPE_DEAGLE 1
#define ITEM_TYPE_SPAS 2
No need for silly checks now it's nice and simple.
Код:
if(Item[i][ItemType] == ITEM_TYPE_NONE)
Other things don't create multiple functions that does the same thing as another function!
Код:
stock DropItem(model, amount, type, Float:X, Float:Y, Float:Z, world, interior)
Just call the create function when needed instead from this function.

This belongs in the enum not it's own variable!
Код:
DropObject[i] = CreateObject(model, X, Y, Z, RX, RY, RZ, world);
You should also return the index the object was created on after this line.
Код:
return i;
Reply
#6

Quote:
Originally Posted by Pottus
Посмотреть сообщение
Rule of thumb when designing dynamic systems.

- The return value for the creation function should be the INDEX in which the item was created.
- All slots full? return -1

Furthermore......

Код:
if(Item[i][ItemPosX] == 0 && Item[i][ItemPosY] == 0 && Item[i][ItemPosZ] == 0)
That is very silly to do and another rule of thumb never ever use == when checking floats!
What you should do is this.

Define your item types
Код:
#define ITEM_TYPE_NONE 0
#define ITEM_TYPE_DEAGLE 1
#define ITEM_TYPE_SPAS 2
No need for silly checks now it's nice and simple.
Код:
if(Item[i][ItemType] == ITEM_TYPE_NONE)
Other things don't create multiple functions that does the same thing as another function!
Код:
stock DropItem(model, amount, type, Float:X, Float:Y, Float:Z, world, interior)
Just call the create function when needed instead from this function.

This belongs in the enum not it's own variable!
Код:
DropObject[i] = CreateObject(model, X, Y, Z, RX, RY, RZ, world);
You should also return the index the object was created on after this line.
Код:
return i;
Could you explain this with a little more detail? I understand some of it, but stuff like the 'return i;' doesn't make sense to me.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)