GMS 2 Choose blend based on existing objects blends

Discussion in 'Programming' started by ProjectGamesInc, Oct 11, 2018.

Tags:
  1. ProjectGamesInc

    ProjectGamesInc Member

    Joined:
    May 22, 2018
    Posts:
    30
    Hi guys,

    I have a room with random objects, every object will have a color, marked with a number (0=red, 1=green and so on).
    The player must destroy the objects with a bullet of the same color, the bullet change color every time you shoot.
    Here we have the script

    Code:
    blend = irandom_range(0,global.colors)
    if blend = 0
    {
        global.blend = make_color_rgb(255,100,100)
        Obj_NextCol.image_blend = make_color_rgb(255,100,100)
    }
    else if blend = 1
    {
        global.blend = make_color_rgb(100,100,255)
        Obj_NextCol.image_blend = make_color_rgb(100,100,255)
    }
    else if blend = 2
    {
        global.blend = make_color_rgb(100,255,100)
        Obj_NextCol.image_blend = make_color_rgb(100,255,100)
    }
    else if blend = 3
    {
        global.blend = make_color_rgb(255,255,100)
        Obj_NextCol.image_blend = make_color_rgb(255,255,100)
    }
    

    Now, you will reach a point in the game when you will have only 3, 2 or 1 type of color in the map, since the others are gone.
    Every colored object have also a local variable called "blend" with inside the value of the color. (0=red, 1=green and so on).
    How can i make the script search and understand how many colors are left and then choose a random one from only the colors that exists?

    Thanks a lot.
     
  2. Phil Strahl

    Phil Strahl Member

    Joined:
    Jul 3, 2016
    Posts:
    331
    How about having an controller or manager object of some sort that keeps track of the colors? Then, the individual objects just get their color from a list or an array within this manager object instead of all mixing their own colors individually.
     
  3. ShineBolt

    ShineBolt Member

    Joined:
    Jul 6, 2016
    Posts:
    9
    Maybe a script like?

    while(instance_exists(obj)) // Don't use as the actual check for the beating the game
    {
    new_color = irandom_range(0, 3);
    var i;
    for (i = 0; i < instance_number(obj); i += 1)
    {
    if (instance_find(obj, i).blend == new_color) return new_color;
    }
    }
     
  4. spe

    spe Member

    Joined:
    Aug 25, 2016
    Posts:
    101
    An easier but possibly less efficient method is to loop your objects each time you change colors. eg:
    Code:
    var red, blue, green, yellow, orange, purple;
    for(var i = 0, i < instance_number(obj), i++) {
        var inst = instance_find(obj, i);
        switch (inst.blend) {
            case red :
                red=true;
                break;
            case blue:
                blue=true;
                break;
            case green:
                green=true;
                break;
            case yellow:
                yellow=true;
                break;
            case orange:
                orange=true;
                break;
            case purple:
                purple=true;
                break;
        }
    }
    That way, only colors that come out 'true' will still have instances of that color left. Just choose randomly only from the 'true' colors.
     
  5. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    126
    Sounds like global ds_list in target create event that each target adds their own blend value and saves reference for him self so it could delete it's own when destroyed.
    Bulet just chose random remaining entry in list.
     
  6. ProjectGamesInc

    ProjectGamesInc Member

    Joined:
    May 22, 2018
    Posts:
    30
    Well, i've managed to solve this by myself in some way, take a look, maybe the code isnt smooth or can be improved so if you tips your welcome ;)

    This is a script that i call everytime i need to change the color of my projectile.

    Code:
    inst = instance_number(Obj_Sphere)
    var col = []
    
    for (var i = 0; i < inst; i += 1)
    {
        target = instance_find(Obj_Sphere,i)
        if target.blend = 1 then col[1] = 1
        if target.blend = 2 then col[2] = 2
        if target.blend = 3 then col[3] = 3
        if target.blend = 4 then col[4] = 4
        if target.blend = 5 then col[5] = 5
        if target.blend = 6 then col[6] = 6
    }
    
    global.nextblend = 0
    while global.nextblend = 0
    {
        global.nextblend = col[irandom(array_length_1d(col)-1)]
    }
    
    if global.nextblend = 1
    {
        global.blend = make_color_rgb(225,80,40)
        Obj_NextCol.image_blend = make_color_rgb(225,80,40)
    }
    else if global.nextblend = 2
    {
        global.blend = make_color_rgb(70,100,230)
        Obj_NextCol.image_blend = make_color_rgb(70,100,230)
    }
    else if global.nextblend = 3
    {
        global.blend = make_color_rgb(70,220,90)
        Obj_NextCol.image_blend = make_color_rgb(70,220,90)
    }
    else if global.nextblend = 4
    {
        global.blend = make_color_rgb(250,220,80)
        Obj_NextCol.image_blend = make_color_rgb(250,220,80)
    }
    else if global.nextblend = 5
    {
        global.blend = make_color_rgb(150,150,150)
        Obj_NextCol.image_blend = make_color_rgb(150,150,150)
    }
    else if global.nextblend = 6
    {
        global.blend = make_color_rgb(150,150,150)
        Obj_NextCol.image_blend = make_color_rgb(150,150,150)
    }
    
     
  7. NeZvers

    NeZvers Member

    Joined:
    Mar 24, 2018
    Posts:
    126
    Edit: I was posting on phone and thought previous wasn't posted.
     

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