GML Assign new random variable

Discussion in 'Programming' started by Montigordev, Oct 11, 2019.

Tags:
  1. Montigordev

    Montigordev Member

    Joined:
    Oct 11, 2019
    Posts:
    5
    Hello, i make a game for mobile devices and faced a problem. After the player hits cube with the same color, player should change its color to random. I am trying to do it with random_range, but it does not work, looks like gamemaker does not understand that i want to assign new variable through random range. Here is the code in the collision event. The player stays with the same color. Thank you in advance.


    if(instance_exists(obj_player))
    {
    if (obj_player.sprite_index == spr_player_yellow)
    {
    color = random_range(0,3)
    switch (color)
    {
    case 0:
    obj_player.sprite_index = spr_player_blue
    case 1:
    obj_player.sprite_index = spr_player_red
    case 2:
    obj_player.sprite_index = spr_player_green
    case 3:
    obj_player.sprite_index = spr_player_yellow
    }
    instance_destroy(self);
    }
    else
    {
    instance_destroy(obj_player)
    }
    }
     
  2. samspade

    samspade Member

    Joined:
    Feb 26, 2017
    Posts:
    2,019
    Try irandom_range or round the results of random_range. Or use choose(). A switch statement requires things to be exactly equal to its cases and random gives you decimals not whole numbers.
     
  3. Montigordev

    Montigordev Member

    Joined:
    Oct 11, 2019
    Posts:
    5
    Thank you for the answer, but unfortunately rounding random range result does not work, it gives absolutely the same result. Irandom always picks case 3. I tried many times, don't think that it is random.
     
  4. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,889
    When you test it out, it's not random. You need to call the randomize() function at the start of your game when testing it (it will be random once you export it to an executable, though).

    switch irandom(3)
     
  5. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    Or even easier for your case:

    obj_player.sprite_index = choose(spr_player_red,spr_player_yellow,spr_player_green,spr_player_blue)
     
  6. Montigordev

    Montigordev Member

    Joined:
    Oct 11, 2019
    Posts:
    5
    Thank you very much, works perfectly.
     
  7. Bros Before Giraffes

    Bros Before Giraffes Member

    Joined:
    Jun 24, 2016
    Posts:
    30
    I just wanted to say that you forgot to put breaks in your cases which is why it always picked case 3.

    Code:
    switch (color)
    {
    case 0:
    obj_player.sprite_index = spr_player_blue; break;
    case 1:
    obj_player.sprite_index = spr_player_red; break;
    case 2:
    obj_player.sprite_index = spr_player_green; break;
    case 3:
    obj_player.sprite_index = spr_player_yellow; break;
    }
     
    TheouAegis 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