Page 1 of 1

favourite functions

PostPosted: Tue Apr 22, 2008 7:15 am
by Fuzzy
Lets post functions that we have made or use. You can recreate a GE function, but I really want to see your own stuff. You should have a comment for each line so people understand it.

Code: Select all
int Odd(int number) // finds out if a number is even or odd
{
    return (number & 1) ? 1 : 0; // gives you a 1 if the number is odd, or zero if its even. I''ll explain this works below
}


its a type of if statement that can be used inside another line. in the first set of brackets is a condition. like if(n > v). follow it with a ? mark. then the first result.. if the condition is true. then a : mark. next is the result if the condition is false. end with a semi-colon.

so use it like this.

s = (MAKSLANE > FUZZY) ? 1 : 2;

so s would equal 1, because GE is better than GM!otherwise, it would be = 2.

Re: favourite functions

PostPosted: Tue Apr 22, 2008 9:31 am
by DST
Here's my shooting formation for vertical space shooters; This takes your player's powerup level, a variable called 'power'(global, integer), and creates spread shots from it.

To use this, your shot must have this in its CreateActor>ScriptEditor:
Code: Select all
directional_velocity = 12; // or whatever you want your shotspeed to be
angle = myburstangle; //sets angle based on the variable "myburstangle" (global, integer)


Code: Select all
void spreadshot(){ // creates function
    switch (power){
case 1: //if power level is 1
myburstangle = 90; //sets angle for the shot
CreateActor("myshot", "doublegold", "no parent", "no path", 0, -15, false);
//change the names to match your shot's name and animation names, and the x/y position to match
//your ship. Notice how my animation names are descriptive of what that shot does...sidelaser etc.
break;
case 2:
myburstangle = 90;
CreateActor("myshot", "doublegold", "no parent", "no path", 0, -15, false);
myburstangle = 60;
CreateActor("myshot", "30goldr", "no parent", "no path", 0, -15, false);
myburstangle = 120;
CreateActor("myshot", "30goldl", "no parent", "no path", 0, -15, false);
break;
case 3:
myburstangle = 90;
CreateActor("myshot", "doublegold", "no parent", "no path", 10, -15, false);
myburstangle = 90;
CreateActor("myshot", "doublegold", "no parent", "no path", -10, -15, false);
myburstangle = 60;
CreateActor("myshot", "30goldr", "no parent", "no path", 0, -15, false);
myburstangle = 120;
CreateActor("myshot", "30goldl", "no parent", "no path", 0, -15, false);
myburstangle = 45;
CreateActor("myshot", "45laserr", "no parent", "no path", 0, -15, false);
myburstangle = 135;
CreateActor("myshot", "45laserl", "no parent", "no path", 0, -15, false);
break;
case 4:
myburstangle = 90;
CreateActor("myshot", "doublegold", "no parent", "no path", 10, -15, false);
myburstangle = 90;
CreateActor("myshot", "doublegold", "no parent", "no path", -10, -15, false);
myburstangle = 60;
CreateActor("myshot", "30goldr", "no parent", "no path", 0, -15, false);
myburstangle = 120;
CreateActor("myshot", "30goldl", "no parent", "no path", 0, -15, false);
myburstangle = 45;
CreateActor("myshot", "45laserr", "no parent", "no path", 0, -15, false);
myburstangle = 135;
CreateActor("myshot", "45laserl", "no parent", "no path", 0, -15, false);
myburstangle = 0;
CreateActor("myshot", "sidelaserr", "no parent", "no path", 10, 0, false);
myburstangle = 180;
CreateActor("myshot", "sidelaserl", "no parent", "no path", -10, 0, false);
break;

              }


                 }


Now in player 1's 'keydown', all you have to add is this:
Code: Select all
spreadshot();


Make sure player 1's power is set on collision with powerup; and reset on DestroyActor;

Hope this helps!

Re: favourite functions

PostPosted: Tue Apr 22, 2008 9:48 am
by Bee-Ant
To create more than 1 actor in the same time
Code: Select all
int i;
for(i=0;i<actorcount;i++)
{
    CreateActor("actor", "actoranimation", "(none)", "(none)", 0, 0, FALSE);
}

Re: favourite functions

PostPosted: Tue Apr 22, 2008 1:57 pm
by asmodeus
ChangeBgColor(red, green, blue);

here is a code to change the bg color, but first you have to create an actor in the background, that is totally white and infinite, and call is BgColor.
Code: Select all
void ChangeBgColor(int value_red, int value_green, int value_green)
{
    BgColor.r = value_red;        // changes the red value for the bg
    BgColor.g = value_green;    // changes the green value for the bg
    BgColor.b = value_blue;      // changes the blue value for the bg
}


example for an actor during playing:
Code: Select all
ChangeBgColor(0, 128, 255);  // this make the bg looks like a blue sky

Re: favourite functions

PostPosted: Tue Apr 22, 2008 8:48 pm
by pyrometal
Here are 3 useful math functions I made specially for this topic!

Finding a factorial:

Code: Select all
unsigned long int factorial(unsigned char A) //finding factorials
{
    unsigned long int ret = A; // ret is returned
 
    while(A>1)    //This loop makes "ret" the factorial value
    {
        A--;      //Decrement for next multiplication
        ret *= A; //Return value is multiplied.
    }
 
    return ret;
}



Determining if a number is prime:

Code: Select all
char is_prime(unsigned long int A) //Is the input number a prime number?
{
    unsigned long int i = 2; //i is the loop counter, used with modulus.
    char ret = 1;            //Assume number is prime initially.
 
    while((A % i) && (i <= (float)A/2)) // when first condition is false, number is not prime.
    {                                   // when second condition is false, number is prime.
        i++;
    }
 
    if (i <= (float)A/2) // If true, then number is not prime and returns 0.
    {
        ret = 0;
    }
 
    return ret; //Prime returns 1. Not prime returns 0.
}



Making divisions safer:

Code: Select all
double safe_division(double A, double B) // Avoids division by 0.
{
    double ret;
 
    if (B)
    {
        ret = A/B; //Normal Division.
    }
    else
    {
        ret = 0; //Division by 0 will return 0.
    }
 
    return ret;
}



Hope these are useful to you if needed! --pyro

Re: favourite functions

PostPosted: Wed Apr 23, 2008 5:58 am
by Fuzzy
Great functions guys. I especially like the background one and the one for prime numbers.

Here is a similar one to my first. This one takes an int and checks to see if its positive or negative.
Code: Select all
int CheckNeg(int num){return (((int)num & (1 <<31)) < 0)? -1:1; }


if the number is positve, it returns 1, if its negative, it returns -1. Use it for finding which direction to head for instance.

Code: Select all
xvelocity = xvelocity*CheckNeg(player.x-enemy.x);
yvelocity = yvelocity*CheckNeg(player.y-enemy.y);