Rand
by Anthony Ratcliffe · in Torque Game Builder · 07/28/2008 (9:55 pm) · 7 replies
I was wondering if there is a get random function in torque like c# i wrote a small c# prog to explain wat i mean
int rand = randNum.Next(1, 3);
if (rand == 1)
{
// play song 1
}
else if (rand == 2)
{
play song 2
}
else if (rand == 3)
{
play song 3
}
just wondeing how that converts in torque
int rand = randNum.Next(1, 3);
if (rand == 1)
{
// play song 1
}
else if (rand == 2)
{
play song 2
}
else if (rand == 3)
{
play song 3
}
just wondeing how that converts in torque
About the author
#2
%randNum = getRandom();
//play song 1
}
{
%randNum = getRandom(%x);
//play song 2
}
like that?
08/02/2008 (8:05 pm)
{%randNum = getRandom();
//play song 1
}
{
%randNum = getRandom(%x);
//play song 2
}
like that?
#3
If you don't specify any arguments, it will return a float (lots of numbers after the decimal place) between 0 and 1.
If you specify only one argument, it will return an integer (whole number, no decimals) between 0 and the number passed in as the argument, including 0 and the number.
If you specify two numbers, it will return an integer between (and including) those two numbers.
I just picked numbers arbitrarily. The truth is that you can pass any integer as an argument. Perhaps it would be better explained like so:
So if you had 5 songs that you wanted to randomly pick from, you could do something like this:
I hope that explains it.
And by the way, put your example code inside code blocks on the forum to format them like code, like this:
[ code ]
// code goes here
[ /code ]
Without spaces between the square brackets.
08/03/2008 (1:56 pm)
No.If you don't specify any arguments, it will return a float (lots of numbers after the decimal place) between 0 and 1.
If you specify only one argument, it will return an integer (whole number, no decimals) between 0 and the number passed in as the argument, including 0 and the number.
If you specify two numbers, it will return an integer between (and including) those two numbers.
// this generates a float between 0.0 and 1.0 // for example, this might generate the numer 0.98357835467 %randNum = getRandom(); // this generates an integer between 0 and 5 // for example, this might generate the number 4 %randNum = getRandom(5); // this generates an integer between 4 and 10 // for example, this might generate the number 8 %randNum = getRandom(4,10);
I just picked numbers arbitrarily. The truth is that you can pass any integer as an argument. Perhaps it would be better explained like so:
// float between 0 and 1 %randNum = getRandom(); // integer between 0 and %max %randNum = getRandom(%max); // integer between %min and %max %randNum = getRandom(%min, %max);
So if you had 5 songs that you wanted to randomly pick from, you could do something like this:
// assuming you don't want to have a song 0
%songNum = getRandom(1,5);
// call a function that will play the song
playSong(%songNum);
// now you need a function that will work with that command
function playSong(%songNum)
{
// code here that will play the song
}I hope that explains it.
And by the way, put your example code inside code blocks on the forum to format them like code, like this:
[ code ]
// code goes here
[ /code ]
Without spaces between the square brackets.
#4
is that what you mean i had trouble understanding songNUM
08/03/2008 (2:37 pm)
// in audio.cs
new AudioProfile(backgroundMusic)
{
filename = "~/data/audio/MainTheme.ogg";
description = "AudioLooping";
preload = false;
};%songNum = getRandom(1,3);
playSong(%songNum);
function playSong(%songNum)
{
if %songNum ==1
{
$backgroundMusic = alxPlay(backgroundMusic);
}
else if songNum ==2
{
// use another song defined in audio.cs
}
}is that what you mean i had trouble understanding songNUM
#5
Alternatively, you could use a switch() statement, but your way will work fine too.
08/03/2008 (2:42 pm)
That is spot on Anthony.Alternatively, you could use a switch() statement, but your way will work fine too.
#6
08/03/2008 (2:43 pm)
Thanks m8
#7
The method I was thinking of would be something more along the lines of having songs defined in audio.cs with the names of song1, song2, song3, song4, and song5. Then you could define the playSong() function in a manner similar to this:
So in that code, if %songNum was 3, then %songName would be "song3"
This method saves a lot of time from having to iterate through each possible value of %songNum using switch() or if() statements, especially if you have lots of music, but has the drawback of making it hard to remember exactly what music "song3" is since it doesn't have a descriptive name.
That's just one example of how it could be done. It's not necessarily the right or best way. Between yourself, Phillip, and I, we've each already come up with out own ways to do it. So just pick what works for you and makes sense to you.
Oh, and nice work on putting your examples in code blocks. :-)
08/03/2008 (2:55 pm)
Yes that works. Sorry I wasn't more specific in my example on how to use %sonNum.The method I was thinking of would be something more along the lines of having songs defined in audio.cs with the names of song1, song2, song3, song4, and song5. Then you could define the playSong() function in a manner similar to this:
function playSong(%songNum)
{
%songName = "song" @ %songNum;
$backgroundMusic = alxPlay(%songName);
}So in that code, if %songNum was 3, then %songName would be "song3"
This method saves a lot of time from having to iterate through each possible value of %songNum using switch() or if() statements, especially if you have lots of music, but has the drawback of making it hard to remember exactly what music "song3" is since it doesn't have a descriptive name.
That's just one example of how it could be done. It's not necessarily the right or best way. Between yourself, Phillip, and I, we've each already come up with out own ways to do it. So just pick what works for you and makes sense to you.
Oh, and nice work on putting your examples in code blocks. :-)
Associate Phillip O'Shea
Violent Tulip