too many instances of one object

H

heanfry

Guest
i realized that one of my object is created more then one time.

can i make an if sentence where it checks how many instances of this object are active and destroy every instances above the one.
 
P

ph101

Guest
You could:

Code:
if instance_number(obj) > 1
with (obj)  {instance_destroy();}

But this could be a terrible idea if you have code making objects unecessarily, potentially every step and then deleting them, your game will be grinding to a halt with wasted CPU. Better to identify where and why the object is its being creating extraneously and prevent it happening.

I suggest putting show_debug_message('i'm obj being created') in the create event of the errant object so you can seei the console when its being made, por possible where the instance is created to track down the issue.

edit. just realised this will destroy them all so you would need a way to leave one object, or spawn a nother, but this approach is a really bad one anyway you shouldn't really pursue it.
 

Dupletor

Member
I don't like ph101 idea.
Try to figure out the situations in which the instances are created and why they appear more times than intended. Don't do that kind of thing, specially because this specific code will destroy both instances every time there are 2 instances. Not only it will not work properly if you create an even amount of instances, it also weakens your game mechanics by a god damn lot, and your object will most likely become useless in the future of your project.

Worst case scenario, don't destroy all instances if more than one exist, put a self destruction on the creation code of the object.

But honestly, find the source of the problem.
 
P

ph101

Guest
I don't like ph101 idea.
It wasn't my idea, I was purely explaining how it could be done, I recommended at least twice not not doing it: ("But this could be a terrible idea", "This approach is a really bad one") and suggested to find the source using debug. You just repeated what I said :p
 

TheouAegis

Member
SOMETIMES it's ok to check if instance_number is greater than a limit.

Code:
if instance_number(children) < 1
    new_child = instance_create(mom.x, dad.y, fetus.type);
else
    fetus.type = -1;
or

Code:
var new_child = instance_create(mom.x, dad.y, fetus.type);
if instance_number(children) > 1
    instance_destroy(new_child);
But then the question arises, "Why are you making more children if you don't want anymore? What can you do to prevent the creation of more children? Killing the children is not the answer!"
 
Top