1
\$\begingroup\$

When you pick an item in the inventory and drop it to obj_item_ground, this item is send to the ground. That's fine. I want to add a delay to this item to destroy it, but if I drop 2 items to the ground, only the last is destroy.
Question : How can I create one timer by item dropped ?

Step :
obj_inventory : on Create event

 instance_create(0, 0, obj_item_ground);

obj_item_ground : on Create event

initialTime = 5;
time = 5;
ground = -1;

on Alarm[0] event; if timer is less than 0, object is destroy.

if (time > 0) {
    time -= 1;
    alarm[0] = 30;
} else {
    with (ground) {
        instance_destroy();
    }
}

on Left Pressed event :

if (mouseItem != -1) {
    scr_item_ground_timer();
}

mouseItem is item I drag to the obj_item_ground.

scr_item_ground_timer() :

Here I create the instance of object dropped to the ground and start the alarm[0] timer.

scr_inventory_item_drop(mouseItem);

for (i = 0; i < mouseQuantity; i++) {
 ground = instance_create(obj_player.x + random_range(-30, 4), obj_player.y + random_range(-12, 0), mouseItem);
}
alarm[0] = 30;
mouseItem = -1;

scr_inventory_item_drop is just to clear the array which contains the object dropped.

\$\endgroup\$
  • \$\begingroup\$ Why are you doing with (ground) instance_destroy();? This means the ground will be destroyed, not the object that is the owner of the timer. \$\endgroup\$ – DH. Nov 3 '16 at 10:41
  • \$\begingroup\$ ground is the item's instance_create (in scr_item_ground_timer) and it currently destroy the object on the ground when the timer is 0. It's a bad practice ? Maybe this var is badly named. Var "ground" is the object dropped. I just need to put unique timer to each "ground" dropped to the ground. \$\endgroup\$ – guish Nov 3 '16 at 10:46
  • \$\begingroup\$ Yeah, you should let the responsibility of destroying items dropped on the ground for themselves, not delegate it to someone else. I'll write an answer detailing more soon. \$\endgroup\$ – DH. Nov 3 '16 at 11:02
0
\$\begingroup\$

As you noticed, if you handle the destruction of items dropped to the ground in the inventor object, you won't be able to handle multiple items.

Make sure to let the responsibility of destroying the items on ground for themselves, that's it, each object will have it's own alarm. It's not a bad design, and it doesn't cause lag as well because it's just a single counter that gets decreased every step.

So, for every possible mouse_item I recommend that you create a parent object, let's call it obj_droppable. In This obj_droppable you should set the alarm.

obj_droppable Create:

alarm[0] = 60; //or whatever time you need for it to stay on ground

obj_droppable Alarm 0:

instance_destroy();
\$\endgroup\$
  • \$\begingroup\$ Ok, so I can remove the Create and Alarm[0] events in obj_item_ground ? And put your new object to obj_item parent ? \$\endgroup\$ – guish Nov 3 '16 at 11:56
  • \$\begingroup\$ @guish Exactly. \$\endgroup\$ – DH. Nov 3 '16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.