Random path... but not all...

Talk about making games.

Random path... but not all...

Postby FERdeBOER » Sun Jul 25, 2010 2:46 am

Hi.

Is there a possibility to make an actor to use some of the paths randomly, but not all of them?

I.e. : I've created 6 paths. I want actor A to follow path 1,2 or 3, and actor B to follow 4,5 or 6.

Can it be done?

Thanks.
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Random path... but not all...

Postby DST » Sun Jul 25, 2010 5:36 am

You can use strcpy in most of the Game Editor functions.

example:

Code: Select all
char pathname[10];
int i=rand(3);
if(i>1){
    strcpy(pathname, "uno");
       }
else if(i<=1){
    strcpy(pathname, "dos");
            }
ChangePath("Event Actor", pathname, BOTH_AXIS);



Simply strcpy the name of the actor, animation, or pathname into a char charname[charlength]; string and you can then call the function using it. Make sure not to use quotes around the string name in when you're calling the function.

So you can then create an array to store these, and pick from the array:

Code: Select all
char pathnames[6][10]={"uno", "dos", "tres", "quattro", "cinco"};
int i=round(rand(5));
char thispath[10];
strcpy(thispath, pathnames[i]);
ChangePath("Event Actor", thispath, BOTH_AXIS);


And yes, you can use numbers instead of letters. I just felt like practicing my spanish tonight.

Make sure the length of your char[] array is long enough to hold the word +1 letter.
So the word "yes" requires a char[4] length array (the last character is string termination).
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Random path... but not all...

Postby FERdeBOER » Sun Jul 25, 2010 12:16 pm

DST wrote:And yes, you can use numbers instead of letters. I just felt like practicing my spanish tonight.

I tend to use names instead numbers, is more self-explanatory.
Your Spanish is good, but quattro is Italian, not Spanish :)

Make sure the length of your char[] array is long enough to hold the word +1 letter.
So the word "yes" requires a char[4] length array (the last character is string termination).

Didn't know that!

Thank you very much!
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Random path... but not all...

Postby FERdeBOER » Mon Jul 26, 2010 7:39 pm

Your code works like a charm... I just have a problem, not always work...
I think I've made something wrong with the adaption, maybe with rand...
Code: Select all
void searchpatrol()
{
    char pathnames[3][12]={"patrolpath", "patrolpath1", "patrolpath2"};
    int i=round(rand(3));
    char thispath[12];
    strcpy(thispath, pathnames[i]);
    ChangePath("Event Actor", thispath, BOTH_AXIS);
}

The object most of the times follow one of the paths, but not always.
What do I made wrong?
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Random path... but not all...

Postby DST » Mon Jul 26, 2010 8:22 pm

rand() generates a number up to and including the number specified. Since your array values are 0, 1, and 2, use rand(2) instead.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Random path... but not all...

Postby FERdeBOER » Mon Jul 26, 2010 8:29 pm

DST wrote:rand() generates a number up to and including the number specified. Since your array values are 0, 1, and 2, use rand(2) instead.

:oops:
I knew that but, for some reason, this time I was thinking on that number as the quantity of random numbers, not the numbers considered. :roll:

Sorry for the stupid question and thank you very much
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score

Re: Random path... but not all...

Postby DilloDude » Tue Jul 27, 2010 4:13 am

If you're using round(rand(2), then you won't get an even probability of all options:
rand(2) will return a real number from 0 to 1.999... When you use round, you round it to the nearest integer. So if it's less than 0.5, it'll round to 0 - that's a range of 0.5. If it's 0.5 or more but less than 1.5, it'll round to 1 - a range of 1. If it's 1.5 or more, it'll round to 2 - a range of 0.5. So the probability of the result being 1 is twice as likely as 0 or 2, meaning you'll get 1 more often than the others.
If you just use
Code: Select all
int i = rand(3);
This time without the round, it'll be even. Because you're storing it as an integer, any decimal value will be dropped, so from 0 to 0.999... = 0, 1 to 1.999... = 1, and 2 to 2.999... = 2, resulting in an even probability.
Image
User avatar
DilloDude
 
Posts: 866
Joined: Tue Jan 24, 2006 9:51 am
Location: Nyerellion
Score: 58 Give a positive score

Re: Random path... but not all...

Postby FERdeBOER » Tue Jul 27, 2010 11:27 am

Thank you Dillo Dude!
FERdeBOER
 
Posts: 36
Joined: Fri Mar 12, 2010 8:03 pm
Location: Good question...
Score: 0 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest