[Help] Random Color Messages

Discussion in 'CommandHelper' started by Sketsi, Mar 19, 2019.

  1. Sketsi

    Sketsi New Member

    I am trying to create a message which will use random colors, tried creating an array and when it comes back, it's in curly brackets. Was wondering if this was possible, and if so how I might go about doing it?

    Alias:
    /randomcolor $msg = >>>
    assign(@randomcolorarray, array('a', 'b', 'c', 'd', 'e', 'f', 2, 3, 4, 5, 6, 7, 8));
    chat(colorize('&' array_rand(@randomcolorarray, 1, false) $msg)); <<<

    Sketsi: & {3} Message
    Last edited: Mar 19, 2019
  2. PseudoKnight

    PseudoKnight Well-Known Member Developer

    array_rand() returns an array, so you'd have to select an index. Here's two ways of choosing a single value from an array.
    PHP:
    @color = array_rand(@randomcolorarray, 1, false)[0];
    // or
    @color = @randomcolorarray[rand(array_size(@randomcolorarray))];
    I have actually wanted to propose a cleaner function for getting single values for some time now. I keep forgetting to do it.

    Note without dot concatenation, you have an extra space between the ampersand (&) and the color value. So the line should look like this:
    PHP:
    chat(colorize('&' . array_rand(@randomcolorarray, 1, false)[0] . $msg));
  3. Sketsi

    Sketsi New Member

    Thanks for the assistance. I managed to get it to work, yeah I realized after I posted the output on here, that I forgot the dot concatenation, and ended up fixing that bit, was mainly testing to see if I could get the curly brackets gone or not.