Author Topic: [Solved] Delete an item  (Read 1617 times)

If I spawn an item using the method below, how could I delete it later?
Code: [Select]
$item = new Item()
{
datablock = "PizzaItem";
static    = "1";
position = %pos;
};
missionCleanup.add($item);

$item.delete(); does not seem to work.


edit:  Problem solved. It was not working because I had spawned multiple $items and only the latest would be deleted. To prevent multiple items from spawning, I made it delete any existing item before creating a new one:
Code: [Select]
if(isObject($item))
    $item.delete();

$item = new Item()
{
    datablock = "PizzaItem";
    static    = "1";
    position = %pos;
};
missionCleanup.add($item);
« Last Edit: July 17, 2016, 12:17:49 AM by Farad »

T+T ammo and my energy ammo for the XCOM weapon pack do this.
Calling .fadeout() on the item before .delete() may work.

edit: it is weird that it isn't working, so check for errors or something first
« Last Edit: July 16, 2016, 07:46:55 AM by Conan »

$item.delete(); should work fine. You must be doing something wrong?

You should just be able to delete it using ::delete. Are you sure the code you're executing is the same as what you've posted here?

Are you running that code with other add-ons at the same time?
Maybe something else is enabled that overwrites the delete function in a weird way that breaks it.

Another thing that could be happening is if $item is spawned twice, it'll only be the most recent I believe. The best thing to do is give it a unique ID, like $item[%client] = new item()

You could be saying global variable is item 11245 then spawning another, $item is now item 12266 or whatever, it'll only delete 12266 because that's what $item is. I'm drunk and on a beach on holiday so apologies if it's not the most clear explanation  :cookieMonster:

I would advise using a simset

create the pizzaSet like this (only do this once)
Code: [Select]
if(!isObject($pizzaSet))
    $pizzaSet = new simSet();

then whenever you create a new pizza add it to the set

Code: [Select]
%item = new Item()
{
    datablock = PizzaItem;
    static = true;
    position = %pos;
};
missionCleanup.add(%item);

Code: [Select]
$pizzaSet.add(%item);
if you want to remove all the pizzas do something like this

Code: [Select]
%i = $pizzaSet.getCount();
while(%i-->=0)
    $pizzaSet.getObject(%i);

or maybe you want to do some kind of check
like upon creating a pizza set a variable on it like so: createTime = getSimTime();
and then later delete all pizzas that have been around for longer than 18 seconds

Code: [Select]
%i = $pizzaSet.getCount();
while(%i-->=0)
{
    if(getSimTime()-(%o = $pizzaSet.getObject(%i)).createTime < 18000)
        continue;
    %o.delete();
}

or maybe after creating a pizza you want it to go away after an amount of time
then you would do something like this (upon creating it)

Code: [Select]
%delay = 18000;
%item.schedule(%delay-200,fadeOut);
%item.schedule(%delay,delete);

fadeOut is not required but it will make it look prettier when it "fades" before it gets deleted
« Last Edit: July 16, 2016, 07:00:10 PM by Swollow »

like upon creating a pizza set a variable on it like so: createTime = getSimTime();
Just to clarify - to put a variable on an object, you can do %object.variable = %whatever;
(You probably already knew this but just in case)

I feel like, in a while loop you should be using getRealTime();

I feel like, in a while loop you should be using getRealTime();
we want to remove all of the items that are over 18 seconds old on that tick
the while loop should not take more than a millisecond unless there are like 15,000 pizza items in which case it may take like 2ms
I also notice weird nonsensical jumps when using getRealTime(); that I can't explain, leading me to believe its not perfectly accurate


i think in this case using sim time will be better