Page 1 of 2
How do I do random?

Posted:
Mon Aug 06, 2007 12:54 am
by supa-tails
How do I do random scripting, like if you shoot an enemy, it plays a random sound effect?

Posted:
Mon Aug 06, 2007 1:03 am
by d-soldier
For that situation you would need the bullet's collision event to the badguy to have a script with:
int n;
n=rand(2);
if (n==0)
{
INSERT PLAYSOUND #1 HERE
}
if (n==1)
{
INSERT PLAYSOUND #2 HERE
}

Posted:
Mon Aug 06, 2007 1:14 am
by supa-tails
It didn't work, it said Owie1(name of the sound) wasn't found


Posted:
Mon Aug 06, 2007 1:37 am
by d-soldier
It works fine... you need to highlite the line that says "INSERT PLAYSOUND #1 HERE" and delete it, then right click and setup your playsound function there. If the file isn't found then it's a problem with your file or data folder location, not with the scripting.

Posted:
Mon Aug 06, 2007 2:28 am
by arcreamer
you mean so when the bullet is shot is plays a sound or if it hits a person it plays a sound? for that all u have to do is collision with bullet on character add sound and open ur sound.

Posted:
Mon Aug 06, 2007 5:55 am
by DilloDude
d-soldier wrote:- Code: Select all
int n;
n=rand(2);
if (n==0)
{
INSERT PLAYSOUND #1 HERE
}
if (n==1)
{
INSERT PLAYSOUND #2 HERE
}
rand(2) won't always work, as there is a slim possibility that it could equal 2, thus no sound would play. Also, a little optimization:
- Code: Select all
int n = rand(1.99999);
switch (n)
{
case 0:
INSERT PLAYSOUND #1 HERE
break;
case 1:
INSERT PLAYSOUND #2 HERE
break;
}
I changed the 2 to 1.99999, so that the maximum it can be is 1.99999. As this is being cast to an int, there is a fairly even chance of getting 0 or 1. Also, as long as you don't have mare than 255 options, you may as welll use a char instead of an int, which won't use as much space:
- Code: Select all
unsigned char n = rand(1.99999);

Posted:
Mon Aug 06, 2007 6:02 am
by summer_goth
Are you sure that sometimes it will be 2?
If the code is:
- Code: Select all
n=rand(2);
Then it will always be a number between 0 and smaller than the number in brackets.
Hence 0 and 1.
There is no possibility that it can be 2.

Posted:
Mon Aug 06, 2007 7:30 am
by d-soldier
Right you are sg, and regardless, if the file isn't found, it's not because of "n" being any number.

Posted:
Mon Aug 06, 2007 7:32 am
by DilloDude
summer_goth wrote:Then it will always be a number between 0 and smaller than the number in brackets.
I think it will be a number from 0 up to and including the number. I could be wrong, though.

Posted:
Mon Aug 06, 2007 1:41 pm
by Jay S.
DilloDude wrote:summer_goth wrote:Then it will always be a number between 0 and smaller than the number in brackets.
I think it will be a number from 0 up to and including the number. I could be wrong, though.
I just did a test, and I believe summer_goth is correct... If you use rand(2), it will equal either 0 or 1.


Posted:
Mon Aug 06, 2007 11:34 pm
by DilloDude
rand returns a real number. It will be 0 or 1 if you cast it to an int. But it could be anywhere from 0.0 to 1.99999 (or possibly 2?) if you are casting to an int, the only time it would return 2 would be if it returns exactly 2. 1.9999999999999999999999 would still become 1. As there are a great many pssibilities, 2 would become very rare, yet may still be there.

Posted:
Tue Aug 07, 2007 5:58 am
by summer_goth
In Microsoft Visual C++ rand returns an integer. Is it different in Game Editor?
http://msdn2.microsoft.com/en-us/library/398ax69y(VS.80).aspx
Also on the C++ reference site:
http://www.cplusplus.com/reference/clib ... /rand.html
Eh, so I am curious whether it works the same with Game Editor.

Posted:
Tue Aug 07, 2007 11:32 am
by DilloDude
In GE, rand is a real number. So whether it can return the max value you put in or just anything up to that number, I don't know. I always thought that the largest would be the maximum number, but you'd have to ask Makslane.

Posted:
Tue Aug 07, 2007 2:38 pm
by Troodon
We can do a simple test. Just make a fps 100 game cast random numbers witn rand(2). Then make an pacman appear when the variable is ==2.
In 1 minute it would try 6000 random numbers and in 5 minutes it would make 24000. 10 minutes would be almost 50 000 different numbers.
If there is no number 2 in 50 000 numbers, it's a very very small possibility that it could never be appearing in your game.


Posted:
Wed Aug 08, 2007 6:44 am
by summer_goth
DilloDude, it seems that you may be right. I see that Makslane is using round every time he uses rand.