how does a function function?

Talk about making games.

how does a function function?

Postby cyber ruler » Tue May 27, 2008 7:38 pm

I'm trying out a bunch of programming languages right now including Qbasic, DarkBasic, JustBasic, C++, JavaScript, and Flash, but I still use GE for my games because, well, it's easy (and fun) to use.

anyway, to the point. I like functions, they help. A lot. In GE, first I want to know, can you actually use functions. If so, can someone tell me how to create one (what commands, cuz I tried the obvious (function)) in global code, then call it from an event from one of the actors (again, what command do I use, I think it has something to do with call, or goto). Score +1 if you can tell me :D
All of my friends tell me programming is my thing. I go to tech school and I'm in I.T., yep, programming all day. My friends ask for help all of the time. But, they tell me my graphics suck... and I have to agree. O, well :/
cyber ruler
 
Posts: 52
Joined: Sat Apr 28, 2007 7:59 pm
Location: Je ne parle englais : /
Score: 0 Give a positive score

Re: how does a function function?

Postby Kodo » Tue May 27, 2008 11:08 pm

Code: Select all
void myfunciton()
{
 // your code here
}

The word 'void' above tells the compiler that the function will return nothing. But it can still be useful.

To use this function you'd just type:

Code: Select all
myfunction();

You can do all kinds of handy things with them too, like passing values in. The function below still doesn’t return a value (hence the 'void') at the start, but it does receive a value that can be acted on.

Code: Select all
void playFX(int soundID)
{
  switch (soundID)
  {
   case 0:
    // play sound here
    break;
   case 1:
    // play another sound here
    break;
   default:
    // do this if the value of soundID isnt handled above
    break;
   }
}


To use this you'd just type:

Code: Select all
playFX(yourSoundID);

You might want to do some clever math in a function and have the result returned. In which case you might want to do something like:

Code: Select all
result = mathFunction(3, 2);


The function for this might look like

Code: Select all
int mathFunction(int a, int b)
{
  a++;   // add one to a
  b+=2; // add 2 to b

  return(a*b); // return a*b
  // not exactly clever math, but you get my point!
}


Notice the word 'int' at the start of the function name, this indicates that the function will return an Integer value. Also notice that the variables used in the function (a and b) are declared inside the the ( ), both are expected to be integer values in this case.

I'd suggest getting a book on C. C is an excellent language to help build a general understanding of programming; it'll help you understand more about other languages too when you come to study them. I'd actually recommend two books that complement each other very well, both cover similar material but in different ways, so where one doesn’t make sense often the other one does:

C for Dummies (Dan Gookin – Published by Wiley)
Absolute Beginner’s Guide to C (Greg Perry– Published by SAMS Publishing)
Inogames: http://www.inogames.com iOS gaming
Firetop Adventure (app store): http://itunes.apple.com/us/app/firetop- ... ?mt=8&ls=1
User avatar
Kodo
 
Posts: 449
Joined: Thu Oct 20, 2005 8:20 pm
Location: UK
Score: 23 Give a positive score

Re: how does a function function?

Postby DST » Wed May 28, 2008 12:53 am

Good Explanation! I can see my help is not needed here, so its off to the next wacky adventure for SuperDST!
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: how does a function function?

Postby catacomber » Wed May 28, 2008 1:07 am

Using functions is a very cool thing to do because it can make your game run faster. Check out the repetitive things your game does and try to make the repetitive things functions.

But--Kodo--elaborate a little for us if you can ----

Some functions are built into ge like atoi and a whole lot of others --but if you want to declare (define) your own which you have to do before you can use it,

WHERE do you declare it in GE and how? I know how to do it with variables, but haven't gotten to declaring my own functions in ge yet.

That is a little puzzling but I will search it. I found this:

viewtopic.php?f=1&t=2442&hilit=define+function

which says you do it in the global code, where you can do a whole lot of other things also:

viewtopic.php?f=2&t=148&hilit=functions

but can you give me a clear example of how to define a new user created function in ge?

And then call it, for example, from a collision event?

I know how to do a lot of stuff in C but how to do it in GE is the question. : )
User avatar
catacomber
 
Posts: 195
Joined: Fri Sep 28, 2007 5:36 pm
Score: 10 Give a positive score

Re: how does a function function?

Postby Fuzzy » Wed May 28, 2008 6:04 am

I wouldnt use this. Its just an example that should be crystal clear. This would make everything much more complicated and slow.

Code: Select all
// this is the portion that goes in global code. it defines the function
int checkvalue(int number) // its going to return an answer in the form of an integer. the function is named checkvalue and takes 1 parameter in the form of an int
// number is simply a placeholder name. it will be substituted when the function is used
{
    int state = 0; // define a local int called state. this will vanish when the function ends. set the value to 0 in case the next line isnt true
    if (number == 1) // check the number for a value of 1
    {
        state = 1; // our check turned out true, so change the value of state to reflect that.
    }
    return state; // return the value of state. it doesnt matter if its 0 or 1
}


now to use it.

in a keypress event under script put this

Code: Select all
yvelocity = (yvelocity+0.05)*checkvalue(canjump);  // add 0.05 to yvelocity then multiply it by checkvalue(canjump)


in effect, checkvalue(canjump) becomes a simple value of 1 or 0 when the key is pressed! function takes an input(int number) and returns an int. return means that the value is exported out of the function. Everything else is discarded.

It becomes a generic test. we could use any of the following:
Code: Select all
test = checkvalue(heartbeat);
test = checkvalue(daylight);
test = checkvalue(functions_understood);
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: how does a function function?

Postby Kodo » Wed May 28, 2008 12:55 pm

You’re right Catacomber, normally if coding in say Visual Studio or another environment you are required to declare\prototype your functions before you can use them.

You can get away without prototyping them by making sure they appear in your program before the Entry Point, which is usually MAIN{}.

To prototype in this situation (not in GE) you’d simply take the first line of your function and paste it in your file header or above the Entry Point and put a semi-colon after it, it’d look something like:

Code: Select all
void myfunciton(int a, int b);

You’d then be free to use the function pretty much as you like.

The great thing with GE is that you don’t need to worry about any of this :) Just create your function in the Global Code area and you can use it wherever you like!
Inogames: http://www.inogames.com iOS gaming
Firetop Adventure (app store): http://itunes.apple.com/us/app/firetop- ... ?mt=8&ls=1
User avatar
Kodo
 
Posts: 449
Joined: Thu Oct 20, 2005 8:20 pm
Location: UK
Score: 23 Give a positive score

Re: how does a function function?

Postby catacomber » Wed May 28, 2008 1:55 pm

Thank you, Fuzzy and Kodo. Now it's very clear. :D Printing this page for my GE manual.
User avatar
catacomber
 
Posts: 195
Joined: Fri Sep 28, 2007 5:36 pm
Score: 10 Give a positive score

Re: how does a function function?

Postby Fuzzy » Wed May 28, 2008 8:15 pm

I confess that I forgot about protoyping. Isnt it a pretty antiquated feature from single pass compilers?
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: how does a function function?

Postby Kodo » Wed May 28, 2008 8:56 pm

Maybe, I don’t know much about past compilers, the history etc. I know some require it, some don’t; but many C compilers do not check for parameter matching either in type or count, so if you do prototype your functions and you try to pass the wrong number of parameters the compiler will flag an error, which is very useful for debugging. I will admit though that I’m no expert, I just spend a lot of time working in Perl\CGI, C and C++ so I’ve managed to pick up quite a bit, enough to get by most of the time, I’m just happy to pass on what I do know :)
Inogames: http://www.inogames.com iOS gaming
Firetop Adventure (app store): http://itunes.apple.com/us/app/firetop- ... ?mt=8&ls=1
User avatar
Kodo
 
Posts: 449
Joined: Thu Oct 20, 2005 8:20 pm
Location: UK
Score: 23 Give a positive score

Re: how does a function function?

Postby catacomber » Thu May 29, 2008 3:07 am

My C book:

C Primer Plus by Stephen Prata which I adore for its clarity,

says the C standard recommends you provide prototypes for all the functions you use. It says it's so the compiler can check to see whether the function is used correctly or not. : ) It says older compilers throw up when they see such prototyping stuff and don't know what to do with it. : )

Am just happy we can funct our own funky functs in GE and that now I know what to do to be functy in GE. : )
User avatar
catacomber
 
Posts: 195
Joined: Fri Sep 28, 2007 5:36 pm
Score: 10 Give a positive score

Re: how does a function function?

Postby cyber ruler » Thu May 29, 2008 10:27 pm

Geez... I didn't think such a simple question would create such a big thread :?
cyber ruler
 
Posts: 52
Joined: Sat Apr 28, 2007 7:59 pm
Location: Je ne parle englais : /
Score: 0 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron