Windows Please help (Random_range(x,y); doesn't work

PPowerrr

Member
Hey guys,
I am making a multiplayer game and I want one of the 2 players to be tagged at the start of the game.
For this I use the code:

GML:
player_tagged = random_range(1,2);

if (player_tagged = 1)
{
    with (oPlayer1)
    {
        tagged = 1;
    }
    
    with (oPlayer2)
    {
        tagged = 0;
    }
}

if (player_tagged = 2)
{
    with (oPlayer1)
    {
        tagged = 0;
    }
    
    with (oPlayer2)
    {
        tagged = 1;
    }
}
This works fine apart from the first line.
If I put
Code:
tagged_player = 1;
in player one becomes the tagged one.
If I put
Code:
tagged_player = 2;
in player two becomes the tagged one.
But if I try to randomize it with
Code:
tagged_player = random_range(1,2);
it doesn't make any one of the players tagged.

Can anybody please tell me what I am doing wrong?
It would really help me out.

Thanks in advance.
Pim
 

kburkhart84

Firehammer Games
Your problem is that random_range() returns a float value between the two numbers. You will be getting things like 1.5676466, and those will just about never be exactly 1 or 2. You want the integer version, which is irandom_range(). It will give you more exact values to work with.

I also recommend that you go away from the with() statements there too. You can just do oPlayer2.tagged = 1 instead of doing the whole with() thing. There is a slight performance hit from what I saw in another forum post too. It seems that it is only really better to use with() if you are doing something more intensive than changing a couple variables, which is all you are doing in this specific code.
 

PPowerrr

Member
Your problem is that random_range() returns a float value between the two numbers. You will be getting things like 1.5676466, and those will just about never be exactly 1 or 2. You want the integer version, which is irandom_range(). It will give you more exact values to work with.

I also recommend that you go away from the with() statements there too. You can just do oPlayer2.tagged = 1 instead of doing the whole with() thing. There is a slight performance hit from what I saw in another forum post too. It seems that it is only really better to use with() if you are doing something more intensive than changing a couple variables, which is all you are doing in this specific code.
Thanks a lot. It works now.

And thanks for the tip. I used oPlayer.tagged before but since it didn't work I changed it to something I was sure should work.
 
Top