Product: DeleteProduct

Called to delete a product component.

Function

void DeleteProduct(product: Product);

Parameters

Name Type Description
product Product A product object to delete

Return Value

None

Remarks

DeleteProduct causes the given product to be deleted after the current event has been processed. This means that other functions that retrieve products from transports will still show this product as existing until the current event has been completed.

Consider the following example.

let gConveyor = null;
function OnSimulationStart()
{
    gConveyor = GetComponentByNameAndType("Conveyor1", "Conveyor");
    SubscribeToEvent("OnProductAdded", "Conveyor1", "Conveyor", OnProductAdded);
    SubscribeToEvent("OnProductRemoved", "Conveyor1", "Conveyor", OnProductRemoved);
}

function OnProductAdded(conveyor, product)
{
    // After 500 milliseconds, call TimerFunc with product as its second argument.
    SetTimerEx(42, 500, TimerFunc, product);
}

// Returns true if obj is in array.
function InArray(array, obj)
{
    if (array === null) return false;
    for (let i = 0; i < array.length; ++i)
    {
        if (array[i] === obj) return true;
    }
    return false;
}

function TimerFunc(timerId, product)
{
    DeleteProduct(product);
    // Product isn't deleted until this timer event is complete.
    // (That is, some time after TimerFunc returns control to Sym3.)
    let products = gConveyor.Products;
    if (InArray(products, product)) LogDebug("TimerFunc: product still present in gConveyor.Products.");
    let areaProducts = gConveyor.GetProductInArea(0, gConveyor.Length);
    if (InArray(products, product)) LogDebug("TimerFunc: product still present according to gConveyor.GetProductInArea."); 
}

function OnProductRemoved(conveyor, product)
{
    if (!InArray(conveyor.Products, product)) LogDebug("OnProductRemoved: product not in conveyor.Products.");
    if (!InArray(conveyor.GetProductInArea(0, conveyor.Length))) LogDebug("OnProductRemoved: product not returned by GetProductInArea.");
}

If you run this script in a project with a conveyor named Conveyor1, adding a product to Conveyor1 prints the following results to the message viewer (shown here from earliest message to latest):

TimerFunc: product still present in gConveyor.Products.
TimerFunc: product still present according to gConveyor.GetProductInArea.
OnProductRemoved: product not in conveyor.Products.
OnProductRemoved: product not returned by GetProductInArea.

If it’s important that a script is processed with the products removed from product querying functions like the above and DeleteProduct is being called, that code should be called from an event that is called after the event that called DeleteProduct. (Like OnProductRemoved, as above, or a simple timer function called from SetTimerEx.)