GMS 2 Can't stop creating infinite instances help!

okasion

Member
I have this code on the obj_boss and obj_bossGhost:

Code:
CREATE:
//variables to move in loop
coordx = room_width/2;
coordy = room_height/2;
r = 100;
theta = 0;
theta_speed = 2;
boss_alpha = 1;
yukari_instances = 1;
Code:
STEP:
//circle movement
theta += theta_speed;
if (theta >= 360) theta -= 360;
x = coordx + lengthdir_x(r, theta);
y = coordy + lengthdir_y(r, theta);

//ghosting
//FROM HERE ONLY ON obj_boss, obj_bossGhost does NOT CREATE COPIES
if (image_alpha != 0){
    image_alpha = clamp(image_alpha - 0.01, 0, 1);   
}
if (image_alpha = 0)
{   
    instance_destroy();   
    if (yukari_instances < 4){
    instance_create_layer(x + random_range(50,50),y + random_range(50,50),"Instances",obj_boss);           
    instance_create_layer(x + random_range(50,50),y + random_range(50,50),"Instances",obj_bossGhost);
    yukari_instances++;
    }
    
    //image_alpha = clamp(image_alpha + 0.01, 0, 1);
}
I have an obj_bossGhost which is the same as obj_boss except a fake copy with no collision events.
I need to count how mant obj_boss and obj_bossGhost I create, but if I use a variable on create of the object, it obviously resets every time it creates another itself, I have uploaded a video so you guys can see my problem:

This is the first thread I make asking for help, I only ask for help when I really don't know what to do anymore after hours of different methods. Sorry if missed something.
Thanks in advance.
 
Instead of having the yukari_instances variable inside the boss object itself (which you have already said, can spawn a copy of itself and that "resets" (or rather, creates a new version of) the yukari_instances variable, place the yukari_instances variable inside of a controller object. This controller object should be what counts and spawns the boss instance and the ghost instances. This means whenever a new boss is created, the yukari_instances variable won't be reset to 0.
 

TsukaYuriko

šŸŒ 
Forum Staff
Moderator
This doesn't seem like a code issue to me. Yukari is just too powerful to have her powers limited by you. ;P


Jokes aside, random idea... if the clones don't have any collisions or anything attached to them, would it be possible to just draw them from within the main boss object? This wouldn't change the main Yukari's collision mask in any way, but (hopefully) still create the desired effect.

Instead of destroying and re-creating the boss every time alpha reaches 0, an alternative spin on @RefresherTowel's suggestion that doesn't rely on having a separate controller would be to merely set a new position for the main boss (and reset its alpha). This won't reset the counter and therefore shouldn't cause an endless loop of Yukaris.
 

okasion

Member
I tried to use @Catastrophe's solution, but the instance_number function wouldn't work correctly for some reason, really weird. I ended up using @RefresherTowel's suggestion, and was able to create 4 "phantom" objects that would change color because of the sprite frames I added - all in all, it worked, but my first idea was to do what you sugges @TsukaYuriko, the problem is (and I used the function clamp as a last resort to set my numeric limits) this code:
Code:
if (image_alpha != 0){
    image_alpha = clamp(image_alpha - 0.01, 0, 1);   
}
if (image_alpha != 1){
    image_alpha = clamp(image_alpha + 0.01, 0, 1);
}
beautifully "fades out" the object, but it fades in just a little, so little in fact that I had to look for various seconds closely at my monitor to check that, indeed, there was *some* fade in. I don't know why the numbers change so much, I tried all type of different numeric values in the second part, and it's something very strange.

 

Catastrophe

Member
You made sure to use instance_number(obj_boss) and not instance_count(obj_boss) right? You can usually debug a problem like that with using show_message on the result.

Notably, if obj_boss_ghost is parented to obj_boss it'll count them too, though, I think. So you'd have to do like

instance_number(obj_boss) -instance_number(obj_boss_ghost)

Regarding image alpha, those two lines of code will cancel each other out. If you want a fade in effect you'll need something like my code for fadein/fadeout


if (fadeIn) {
image_alpha += .005;
if (image_alpha > .6) { <<< change to 1 probably
fadeIn = false
}
} else {
image_alpha -= .005;

if (image_alpha < 0.01) {
instance_destroy(); << change to image_alpha = 0
}
}

But take out the else if you just want it to fade in.

I think your code also has the signs switch xD
 
Last edited:

okasion

Member
You made sure to use instance_number(obj_boss) and not instance_count(obj_boss) right? You can usually debug a problem like that with using show_message on the result.

Notably, if obj_boss_ghost is parented to obj_boss it'll count them too, though, I think. So you'd have to do like

instance_number(obj_boss) -instance_number(obj_boss_ghost)

Regarding image alpha, those two lines of code will cancel each other out. If you want a fade in effect you'll need something like my code for fadein/fadeout


if (fadeIn) {
image_alpha += .005;
if (image_alpha > .6) { <<< change to 1 probably
fadeIn = false
}
} else {
image_alpha -= .005;

if (image_alpha < 0.01) {
instance_destroy(); << change to image_alpha = 0
}
}

But take out the else if you just want it to fade in.

I think your code also has the signs switch xD
I can't make your code work friend.
Code:
//CREATE event of obj_bossGhost
fadeIn = true;

//STEP event of obj_bossGhost
if (fadeIn) {
image_alpha += .005;
if (image_alpha > .6) { // change to 1 probably
fadeIn = false
}
} else {
image_alpha -= .005;
if (image_alpha < 0.01) {
fadeIn = true;
instance_destroy(); // change to image_alpha = 0
}
}
It does the same I achieved with my code, it fades out beautifully but it never returns
I tried commenting instance_destroy and adding image_alpha = 0 as you said; nope.
Could you be more specific please? it just doesn't work... thanks in advance.
 

okasion

Member
This is the code:
Code:
//CREATE obj_bossGhost
fadeIn = true;

//STEP obj_bossGhost

if (fadeIn = true) {
image_alpha += .002;
if (image_alpha > .6) { // change to 1 probably
fadeIn = false
}
} else {
image_alpha -= .005;
if (image_alpha < 0.01) {
fadeIn = true;
//instance_destroy(); // change to image_alpha = 0
}
}
I made it work!!! thanks @Catastrophe

Thanks everyone
 
Top