Help with a random color script

Hello everyone!

I have created a function that changes the colors of an item randomly. It checks the color channels the item have then in a while loop it creates copy, changing one channel at the same time.

The function works fine, but don’t know why produces a big lag spike when the item passed in is a torso. Tested with cloaks and helms and no lag is generated. I though that the issue was the creating a copy to change each color ( 6 copies in total per torso, altho depending on the cloak it will copy 6 too…) but it works inside a random shop system that creates 80+ diferent items in each store and only have lag spikes when modifying torsos >.<

The script is as follows:

object RandomColor(object oItem)
{
    int iCount = 0;
    int LEATHER_1 = 0;
    int LEATHER_2 = 0;
    int CLOTH_1 = 0;
    int CLOTH_2 = 0;
    int METAL_1 = 0;
    int METAL_2 = 0;

    if(GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_LEATHER1) != 1)
        LEATHER_1 = TRUE;

    if(GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_LEATHER2)!= 1)
        LEATHER_2 = TRUE;

    if(GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_CLOTH1)!= 1)
        CLOTH_1 = TRUE;

    if(GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_CLOTH2)!= 1)
        CLOTH_2 = TRUE;

    if(GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_METAL1)!= 1)
        METAL_1 = TRUE;

    if(GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_METAL2)!= 1)
        METAL_2 = TRUE;


	while(iCount < 6)
	{
		switch(iCount)
		{
			case 0:
			{
				if(LEATHER_1 == TRUE)
				{
					oItem = CopyItemAndModify(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,iCount,Random(176));
				}
				break;

			}
			case 1:
			{
				if(LEATHER_2 == TRUE)
				{
					oItem = CopyItemAndModify(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,iCount,Random(176));
				}
				break;

			}
			case 2:
			{
				if(CLOTH_1 == TRUE)
				{
					oItem = CopyItemAndModify(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,iCount,Random(176));
				}
				break;

			}
			case 3:
			{
				if(CLOTH_2 == TRUE)
				{
					oItem = CopyItemAndModify(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,iCount,Random(176));
				}
				break;

			}
			case 4:
			{
				if(METAL_1 == TRUE)
				{
					oItem = CopyItemAndModify(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,iCount,Random(176));
				}
				break;

			}
			case 5:
			{
				if(METAL_2 == TRUE)
				{
					oItem = CopyItemAndModify(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,iCount,Random(176));
				}
				break;

			}
		}
		iCount++;
    }

    return oItem;
}

Any help or opinion is apreciated^^

I don’t know where the lags come from (CopyItemAndModify() does not create lags even if you call it a hundred times within one script) but…

Instead of

int LEATHER_1 = 0;
if(GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_LEATHER1) != 1)
        LEATHER_1 = TRUE;

I would write

int LEATHER_1 = (GetItemAppearance(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,ITEM_APPR_ARMOR_COLOR_LEATHER1) != 1);

Also CopyItemAndModify() creates a copy of the item but it does NOT destroy the original item.

So you should write

object oNew = CopyItemAndModify(oItem,ITEM_APPR_TYPE_ARMOR_COLOR,iCount,Random(176));
if (GetIsObjectValid(oNew))
{
   DestroyObject(oItem);
   oItem = oNew;
}

Otherwise you end up with lots of items somewhere…

So if there’s a lag it has to be somewhere else. Where are the items created? Perhaps there’s a OnAcquire item script causing the lag?

Oh I will change the variable declaration, thanks that is more direct.

The items are created in temporal empty stores that keeps all junk copies, when the randomcolor function is completed, the item is copied to the actual store. The temporal store is destroyed when the shops are created, thus destroying all items inside. (Since DestroyObject occurs when the script ends, if im not mistaken)

I though the OnAcquire event was only for players, I’m unable to see it in the properties of the temporal store :confused: if that can be the issue I would like to know how to check that.

thanks in advance!

You can btw make your script much more simple:

object RandomColor(object oItem)
{
    int iCount;
    for (iCount=0; iCount<ITEM_APPR_ARMOR_NUM_COLORS; iCount++)
    {
        if (GetItemAppearance(oItem, ITEM_APPR_TYPE_ARMOR_COLOR, iCount) != 1)
        {
            object oNew = CopyItemAndModify(oItem, ITEM_APPR_TYPE_ARMOR_COLOR, iCount, Random(176));
            DestroyObject(oItem);
            oItem = oNew;
        }
    }
    return oItem;
}

In your case you could leave the DestroyObject() out but I woudn’t do that.

Since you’re using a store have you tried using a simple container instead? Maybe the lag is caused by the store somehow?

Thanks^^

About trying another container… not really. The system create a fake store next to the actual store, then produces the junk copies created with this script and then delete the fake store.

Is there any diference in copying items in a store on in another inventory? O.o I guess i should have to test.

I don’t know but Bioware made a container especially for item modifications. You can access/create the container with IPGetIPWorkContainer() (include x2_inc_craft). The container is invisible and if it does not exist yet it’s created in the current area.

Oh wow, didnt know that a thing like that existed… This container is always there? Do I need to clean it? or items in it disappear in time?

thanks!

When you call IPGetIPWorkContainer() for the first time the container is created.

And no, (afaik) items in it do not disappear in time. At least in my scripts I always destroy the items myself.