Frictional Games Forum (read-only)

Full Version: Random variables
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi everyone, and happy coding! I'm asking if anyone of you know how to use the RandInt code. I basically know the variables and so, but as I can't name the random int, how to I make an if-else statement about that. Heres an example what im talking about:
--------
if(GetLocalVarInt("sounds") == 1
{
PlayGuiSound("sumthing", 100);
}
----------------
Now I want to make that like it generates a random int, if its 1 it plays something, if 2 if plays something else, and so on. I just don't understand how Huh
(10-30-2013, 08:21 PM)Robosprog Wrote: [ -> ]
PHP Code:
if(RandInt(18) == 8){PlaySoundAtEntity("""scare_animal_squeal.snt""Player"0.60false);}
    else if(
RandInt(1,8) == 7){PlaySoundAtEntity("""scare_human_noices.snt""Player"0.60false);}
    else if(
RandInt(1,8) == 6){PlaySoundAtEntity("""scare_male_terrified.snt""Player"0.60false);}
    else if(
RandInt(1,8) == 5){PlaySoundAtEntity("""scare_wood_creak_mix.snt""Player"0.50false);}
    else if(
RandInt(1,8) == 4){PlaySoundAtEntity("""scare_ghost.snt""Player"0.60false);}
    else if(
RandInt(1,8) == 3){PlaySoundAtEntity("""scare_walk_ghost.snt""Player"0.40false);}
    else if(
RandInt(1,8) == 2){PlaySoundAtEntity("""scare_baby_cry.snt""Player"0.60false);}
    else if(
RandInt(1,8) == 1){PlaySoundAtEntity("""scare_steps_big.snt""Player"0.40false);} 

This is how I used them, if that gives an idea of how they work? Nothing else required.


Thanks! I totally forgot else if statements..
You can totally 'name' a Random number, simply by creating a variable, then assigning to it the random number that RandInt or RandFloat gives you

Code:
int myRandomInt = RandInt(1, 8);

if (myRandomInt > 4)
{
    // do stuff
}

This will also allow you to use the super awesome Switch.. Case statements, to make a bunch of if..else if.. else if.. etc look nice

Code:
int myRandomInt = RandInt(1, 3);

switch (myRandomInt)
{
    case 1: // do stuff if random number is 1
        break;
    case 2: // do stuff if random number is 2
        break;
    case 3: // do stuff if random number is 3
        break;
}

You can read about switch..case statements here, under the 'Conditions' heading
http://www.angelcode.com/angelscript/sdk...ments.html