Adding Specific Colours To The 'set Random Color' Node

Discussion in 'How Can I...?' started by ZakM, Apr 24, 2020.

  1. ZakM

    ZakM Boxer

    Joined:
    Apr 21, 2020
    Messages:
    14
    Likes Received:
    1
    Hi,

    Im trying to add these 3 RGB colours in the Set Random Color node so that the object can only change into these 3 colours:

    (45,201,55)
    (231,180,22)
    (204,50,50)

    I tried to add it like this, but that obviously didn't work lol.


    this.entity().setColor(

    Math.random() * 45 + 201 + 55,

    Math.random() * 231 + 180 + 22,

    Math.random() * 204 + 50 + 50

    );

    Or if there's an easier way please do let me know.

    Thanks
     
  2. Codifie

    Codifie Avid Boxer

    Joined:
    Apr 17, 2018
    Messages:
    364
    Likes Received:
    190
    You could do this several different ways using code. I would recommend setting your colors as a variable first, have a random function, then a set color method. But let me give you a no code method that I think is most useful to others.
    Use the Random 5 node, connect it to a Set Color node. If you connect it to the objects Start node the color will change each time the scene is started/restarted, or you could attach it to a If Collide and trigger it that way.
     
    ZakM likes this.
  3. ZakM

    ZakM Boxer

    Joined:
    Apr 21, 2020
    Messages:
    14
    Likes Received:
    1
    Yep, Literally, working on the random node as you sent the message, didn't realise it existed.

    Thank you:)
     
  4. Codifie

    Codifie Avid Boxer

    Joined:
    Apr 17, 2018
    Messages:
    364
    Likes Received:
    190
    Let me share a script here that I use. This script is written for a level based game, I want specific objects in the level to change to a specific color pattern based on several factors, so this script will change the color of any object it is dropped into and connected to the Start node of the object.
    This is about as simple as can be as far as setting the color of an object based on a variable/parameter.
    In this code the Case: 1. Case: 2 .... those are the level numbers, and each level is a world. So the level variable defines the color chosen.
    Keep in mind, I have several other scripts going on in different places that help all of this work together, I just want to show you an example of code that shows you how to set the color of an object easily based on a variable.
    Hope this helps

    Code:
    let totalLevels
    function init(){
    totalLevels = this.attribute('TotalLevels');
    Settings.totalLevels = totalLevels;
    
    
    }
    function start() {        
    let level;
    
    
    if(Settings.levelID == undefined){
    Settings.levelID = 1;
    }
    level = Settings.levelID;
    
    switch (level) {
    
        case 1:
            this.entity().setColor(72, 50, 36);
            break;
    
        case 2:
            this.entity().setColor(109, 111, 0);
            break;
           
        case 3:
            this.entity().setColor(246, 77, 84); 
            break;
           
        case 4:
            this.entity().setColor(83, 71, 255);
            break;
           
        case 5:
            this.entity().setColor(117, 255, 0);
            break;
           
        case 6:
            this.entity().setColor(248, 107, 0);
            break;
           
        case 7:
            this.entity().setColor(189, 67, 255); 
            break;
           
        case 8:
            this.entity().setColor(206, 0, 10);  
            break;
           
        case 9:
            this.entity().setColor(255, 255, 71);  
            break;
           
        case 10:
            this.entity().setColor(89, 192, 255);
            break;
           
        case 11:
            this.entity().setColor(248, 107, 0);
            break;
           
        case 12:
            this.entity().setColor(244,0, 106);
            break;
           
        case 13:
            this.entity().setColor(96, 255, 196);
            break;
           
        case 14:
            this.entity().setColor(94, 255, 190);
            break;
           
        case 15:
            this.entity().setColor(72, 50, 36);
            break;
           
        case 16:
            this.entity().setColor(109, 111, 0);
            break;
           
        case 17:
            this.entity().setColor(109, 111, 0);
            break;
           
        case 18:
            this.entity().setColor(246, 77, 84); 
            break;
           
        case 19:
            this.entity().setColor(83, 71, 255);
            break;
           
        case 20:
            this.entity().setColor(117, 255, 0);
            break;
           
        case 21:
            this.entity().setColor(248, 107, 0);
            break;
           
        case 22:
            this.entity().setColor(189, 67, 255); 
            break;
           
        case 23:
            this.entity().setColor(206, 0, 10);  
            break;
           
        case 24:
            this.entity().setColor(255, 255, 71);  
            break;
           
        case 25:
            this.entity().setColor(89, 192, 255);
            break;
           
        case 26:
            this.entity().setColor(248, 107, 0);
            break;
           
        case 27:
            this.entity().setColor(244,0, 106);
            break;
           
        case 28:
            this.entity().setColor(96, 255, 196);
            break;
           
        case 29:
            this.entity().setColor(94, 255, 190);
            break;
           
        case 30:
            this.entity().setColor(72, 50, 36);
            break;
           
           
            }
    
    
    }
    
    
    function update(dt){
    }
    function signal(name, value){
    
    
    }
       
    
     
    ZakM likes this.
  5. ZakM

    ZakM Boxer

    Joined:
    Apr 21, 2020
    Messages:
    14
    Likes Received:
    1
    Ah right, what you mentions is similar to what i want to achieve.

    So say that the object has changed into that random colour, is it possible then to make it so that if the object doesn't land on the colour that its changed to the game ends, without code? Or does that have to be done with code?
     
  6. Codifie

    Codifie Avid Boxer

    Joined:
    Apr 17, 2018
    Messages:
    364
    Likes Received:
    190
    ok, that's a little trickier. the only thing that comes to mind is using the Save Variable on your character and save the color, and Load Variable in your object to load the characters color and some how compare the two.
    Maybe someone else may have a no Code solution for this idea?
     

Share This Page