favourite functions

You must understand the Game Editor concepts, before post here.

favourite functions

Postby Fuzzy » Tue Apr 22, 2008 7:15 am

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.
Last edited by Fuzzy on Wed Apr 23, 2008 6:23 am, edited 1 time in total.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: favourite functions

Postby DST » Tue Apr 22, 2008 9:31 am

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!
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: favourite functions

Postby Bee-Ant » Tue Apr 22, 2008 9:48 am

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);
}
User avatar
Bee-Ant
 
Posts: 3723
Joined: Wed Apr 11, 2007 12:05 pm
Location: http://www.instagram.com/bee_ant
Score: 210 Give a positive score

Re: favourite functions

Postby asmodeus » Tue Apr 22, 2008 1:57 pm

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
Attachments
bgcolor.png
Use this for the background
bgcolor.png (196 Bytes) Viewed 1470 times
User avatar
asmodeus
 
Posts: 483
Joined: Thu Oct 11, 2007 5:04 pm
Location: Germany
Score: 43 Give a positive score

Re: favourite functions

Postby pyrometal » Tue Apr 22, 2008 8:48 pm

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
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: favourite functions

Postby Fuzzy » Wed Apr 23, 2008 5:58 am

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);
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest