Page 1 of 1

Random path... but not all...

PostPosted: Sun Jul 25, 2010 2:46 am
by FERdeBOER
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.

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

PostPosted: Sun Jul 25, 2010 5:36 am
by DST
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).

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

PostPosted: Sun Jul 25, 2010 12:16 pm
by FERdeBOER
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!

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

PostPosted: Mon Jul 26, 2010 7:39 pm
by FERdeBOER
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?

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

PostPosted: Mon Jul 26, 2010 8:22 pm
by DST
rand() generates a number up to and including the number specified. Since your array values are 0, 1, and 2, use rand(2) instead.

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

PostPosted: Mon Jul 26, 2010 8:29 pm
by FERdeBOER
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

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

PostPosted: Tue Jul 27, 2010 4:13 am
by DilloDude
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.

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

PostPosted: Tue Jul 27, 2010 11:27 am
by FERdeBOER
Thank you Dillo Dude!