How do I do random?

Game Editor comments and discussion.

How do I do random?

Postby supa-tails » Mon Aug 06, 2007 12:54 am

How do I do random scripting, like if you shoot an enemy, it plays a random sound effect?
................................................................................................ www.freewebs.com/thebiverse .....................................................................................




c'ya on the other side
User avatar
supa-tails
 
Posts: 269
Joined: Fri Jul 27, 2007 3:03 pm
Location: In an aliens tummy... Or in cumming Georgia USA
Score: 17 Give a positive score

Postby d-soldier » Mon Aug 06, 2007 1:03 am

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
}
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby supa-tails » Mon Aug 06, 2007 1:14 am

It didn't work, it said Owie1(name of the sound) wasn't found :(
................................................................................................ www.freewebs.com/thebiverse .....................................................................................




c'ya on the other side
User avatar
supa-tails
 
Posts: 269
Joined: Fri Jul 27, 2007 3:03 pm
Location: In an aliens tummy... Or in cumming Georgia USA
Score: 17 Give a positive score

Postby d-soldier » Mon Aug 06, 2007 1:37 am

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.
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby arcreamer » Mon Aug 06, 2007 2:28 am

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.
arcreamer
 
Posts: 398
Joined: Tue Jul 03, 2007 4:08 pm
Score: 9 Give a positive score

Postby DilloDude » Mon Aug 06, 2007 5:55 am

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);
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby summer_goth » Mon Aug 06, 2007 6:02 am

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.
User avatar
summer_goth
 
Posts: 153
Joined: Fri Jun 22, 2007 8:07 am
Location: South Africa
Score: 5 Give a positive score

Postby d-soldier » Mon Aug 06, 2007 7:30 am

Right you are sg, and regardless, if the file isn't found, it's not because of "n" being any number.
User avatar
d-soldier
 
Posts: 703
Joined: Sun Apr 08, 2007 2:13 am
Score: 61 Give a positive score

Postby DilloDude » Mon Aug 06, 2007 7:32 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Jay S. » Mon Aug 06, 2007 1:41 pm

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. :)
User avatar
Jay S.
 
Posts: 118
Joined: Thu Apr 26, 2007 6:51 pm
Location: My computer desk. :P
Score: 9 Give a positive score

Postby DilloDude » Mon Aug 06, 2007 11:34 pm

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby summer_goth » Tue Aug 07, 2007 5:58 am

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.
User avatar
summer_goth
 
Posts: 153
Joined: Fri Jun 22, 2007 8:07 am
Location: South Africa
Score: 5 Give a positive score

Postby DilloDude » Tue Aug 07, 2007 11:32 am

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.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Postby Troodon » Tue Aug 07, 2007 2:38 pm

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. :)
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Postby summer_goth » Wed Aug 08, 2007 6:44 am

DilloDude, it seems that you may be right. I see that Makslane is using round every time he uses rand.
User avatar
summer_goth
 
Posts: 153
Joined: Fri Jun 22, 2007 8:07 am
Location: South Africa
Score: 5 Give a positive score

Next

Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron