Create a Random Variable:
				
Posted: 
Mon Jun 11, 2007 3:07 amby 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?
			 
			
				
				
Posted: 
Mon Jun 11, 2007 11:19 amby 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
              }
			 
			
				
				
Posted: 
Mon Jun 11, 2007 2:57 pmby d-soldier
				Thanks a bunch! Exactly what I needed!
			 
			
				
				
Posted: 
Mon Jun 11, 2007 8:07 pmby 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);;
}
			 
			
				
				
Posted: 
Mon Jun 11, 2007 10:11 pmby 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.
			 
			
				
				
Posted: 
Mon Jun 11, 2007 11:51 pmby d-soldier
				Right you are.. should have caught that myself... 

 .. speaking of random variables, what if I wanted my actor to move to a random location (within the view) on a keydown event?
 
			
				
				
Posted: 
Tue Jun 12, 2007 12:08 amby Sgt. Sparky
				events->add->keydown->whateverkey->
- Code: Select all
- xscreen = rand(view.width);
 yscreen = rand(view.height);

 
			
				
				
Posted: 
Tue Jun 12, 2007 12:11 amby 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.
 
			
				
				
Posted: 
Tue Jun 12, 2007 12:14 amby Game A Gogo
				sparky was quicker then I D:
			 
			
				
				
Posted: 
Tue Jun 12, 2007 12:40 amby d-soldier
				You guys are smarter then I know what to do with... 

 
			
				
				
Posted: 
Tue Jun 12, 2007 2:36 pmby Sgt. Sparky
				Game A Gogo wrote:smarty party 

 
Weeee!!! *grabs a bag of smartys and runs in circels with it*
eventually runs into a wall and gets knocked out*
Weeee!!!
 
			
				
				
Posted: 
Tue Jun 12, 2007 8:47 pmby 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.