random moving

Posted By: Marky Mark

random moving - 08/10/07 18:57

Hey,

I'd like to do a function that choose randomly a angle between 10 and 170 degrees, then move the entity into this way. Here's my plan:

function chooseway()
{
//choose a random direction
}

then in some entities actions that I want to follow the given directions,

action X
{
...
// move at X speed to the direction chosen by chooseway()
}

is this possible? Can someone help me coding my idea?
Thanks a lot !
Posted By: dudeBot

Re: random moving - 08/10/07 21:00

try using randomize() - 'Seeds' the random() sequence with a new random number.

and then random() - Returns a random number.

I dont think that you can set a numeric range, so to get an integer
between 10 and 170 here's what I'd do...

//newPanValue is set to a number beween 0 and 160
newPanValue = int(random(161));

//incrementing it by 10 will get you in your 10 to 170 range
newPanValue += 10;

Now you can set the pan of the entity to this value.
Posted By: LogantheHogan

Re: random moving - 08/10/07 21:15

Yep. Dudebot is right. And then the rest:

Code:


function choose_way()
{
my.pan = random(160) + 10;
}


action X
{
randomize();
choose_way();
while(1)
{
temp.x = 5*time_step; // change 5 to whatever speed you want
temp.y = 0;
temp.z = 0;
c_move(me,temp,nullvector,null);
wait(1);
}
}



Hope that helps.
Posted By: Marky Mark

Re: random moving - 08/10/07 21:33

But I want the result of choose_way() to be the same once called. Let's say I want the result in 4 actions, I can't use the way you showed cause the action is calling choose_way(), so if in my second action I call choose_way again, it will generate a new random number?
Posted By: LogantheHogan

Re: random moving - 08/10/07 22:04

Okay, I understand. In that case you'll just need to use a global variable. Let's call it picked_angle.

Code:

var picked_angle;

starter choose_way()
{
randomize();
picked_angle = random(160) + 10;
}

action X
{
while(picked_angle == 0) { wait(1); } // wait until choose_way has run
my.pan = picked_angle;
while(1)
{
temp.x = 5*time_step; // change 5 to whatever speed you want
temp.y = 0;
temp.z = 0;
c_move(me,temp,nullvector,null);
wait(1);
}
}



Now, all entities with action X attached will travel in the same (but random) direction.

Better?

EDIT: changed choose_way to a starter, and added WHILE at the beginning of X
Posted By: Marky Mark

Re: random moving - 08/10/07 23:35

Ok let's try it, it looks like working. I'll post back in some minutes
Posted By: Marky Mark

Re: random moving - 08/10/07 23:37

Oh btw, Can I just store what you put inside the action into a function so it would be much more easy to work with? (1 line of code instead of 11 in each actions)

thanks
Posted By: Marky Mark

Re: random moving - 08/10/07 23:44

Btw #2: It works I love you.
© 2024 lite-C Forums