Page 1 of 1

Create a Random Variable:

PostPosted: Mon Jun 11, 2007 3:07 am
by d-soldier
Is it possible to script an object the randomly create a number, and then use that number to do something? ... For example, Using a timer event, every ten seconds an object will create one of four possible objects (powerups in this case) and the possibility of creating nothing too... does that make sense?

PostPosted: Mon Jun 11, 2007 11:19 am
by pixelpoop
on a create actor event put this:

int n;
n=rand(100);
if (n==0){put code here that will exacute 1% of the time
}
if (n<=10 && n>0){put code here to exacute 10% of the time
}

PostPosted: Mon Jun 11, 2007 2:57 pm
by d-soldier
Thanks a bunch! Exactly what I needed!

PostPosted: Mon Jun 11, 2007 8:07 pm
by d-soldier
.. I originall put this script on a timer for a filled-region actor, and it didn't do anything... so I then moved it to the create-actor script (as instructed) amd still doesn't do anything... Did I do something wrong here?

int n;
n=rand(100);
if (n=0)
{
CreateActor("pu_1up", "pu_shield00000", "(none)", "(none)", 0, 0, false);
}
if (n<=50 && n>0)
{
CreateActor("pu_gun", "pu_gun00000", "(none)", "(none)", 0, 0, false);
}
if (n<=100 && n>50)
{
CreateActor("pu_shield", "pu_shield00000", "(none)", "(none)", 0, 0, false);;
}

PostPosted: Mon Jun 11, 2007 10:01 pm
by Sgt. Sparky
d-soldier wrote:.. I originall put this script on a timer for a filled-region actor, and it didn't do anything... so I then moved it to the create-actor script (as instructed) amd still doesn't do anything... Did I do something wrong here?

int n;
n=rand(100);
if (n=0)...

n ==, not n =. :D

PostPosted: Mon Jun 11, 2007 10:11 pm
by pixelpoop
thanks Sgt Sparky,

Just to clarify,
in the if statement it should read n==0
when declaring what n is equal to you would use a single equals

note: I have edited my original code with this correction.

PostPosted: Mon Jun 11, 2007 10:13 pm
by Sgt. Sparky
pixelpoop wrote:thanks Sgt Sparky,

Just to clarify,
in the if statement it should read n==0
when declaring what n is equal to you would use a single equals

note: I have edited my original code with this correction.

that is what I said. xD :lol:

PostPosted: Mon Jun 11, 2007 11:51 pm
by d-soldier
Right you are.. should have caught that myself... :cry: .. speaking of random variables, what if I wanted my actor to move to a random location (within the view) on a keydown event?

PostPosted: Tue Jun 12, 2007 12:08 am
by Sgt. Sparky
events->add->keydown->whateverkey->
Code: Select all
xscreen = rand(view.width);
yscreen = rand(view.height);

:D

PostPosted: Tue Jun 12, 2007 12:11 am
by Game A Gogo
maybe somethign like:
Code: Select all
x=rand(view.width)+view.x;
y=rand(view.height)+view.y;

Calculates a random number whithing the views size and add the position to it.

PostPosted: Tue Jun 12, 2007 12:14 am
by Game A Gogo
sparky was quicker then I D:

PostPosted: Tue Jun 12, 2007 12:40 am
by d-soldier
You guys are smarter then I know what to do with... :D

PostPosted: Tue Jun 12, 2007 12:46 am
by Game A Gogo
smarty party :D

PostPosted: Tue Jun 12, 2007 2:36 pm
by Sgt. Sparky
Game A Gogo wrote:smarty party :D

Weeee!!! *grabs a bag of smartys and runs in circels with it*
eventually runs into a wall and gets knocked out*
Weeee!!!

PostPosted: Tue Jun 12, 2007 8:47 pm
by Fuzzy
Code: Select all
switch( var holding a rand value)
{
    case 0:    createactor;
                 break;
    case 1:    createactor;
                 break;   
    case 2:    createactor;
    default : break;
}


Use as many cases as you like.

The break statements bails out from the test, otherwise it might ring true for several cases and execute their code. Default will run after all the others. Especially if you dont break before then. Note that I left a break out after the case 2, because the default handles in. I dont like redundancy.