1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Can't stop creating infinite instances help!

Discussion in 'Programming' started by okasion, Nov 6, 2019.

  1. okasion

    okasion Member

    Joined:
    Oct 11, 2019
    Posts:
    6
    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.
     
  2. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,216
    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.
     
    okasion likes this.
  3. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    106
    Kinda skimmed this since it's answered, but you can also use instance_number(obj_boss)
     
  4. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,707
    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 likes this.
  5. okasion

    okasion Member

    Joined:
    Oct 11, 2019
    Posts:
    6
    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.

     
  6. Catastrophe

    Catastrophe Member

    Joined:
    Sep 22, 2019
    Posts:
    106
    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: Nov 7, 2019
    okasion likes this.
  7. okasion

    okasion Member

    Joined:
    Oct 11, 2019
    Posts:
    6
    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.
     
  8. okasion

    okasion Member

    Joined:
    Oct 11, 2019
    Posts:
    6
    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
     
    Catastrophe likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice