Method();

Non-platform specific questions.

Method();

Postby Troodon » Sun Oct 16, 2011 11:23 am

How can I build and call methods with parameters?

Like, I code a method
listTopPlayers(int myVariable)
{
//the method's code where it uses the variable myVariable
}

and then whenever I want to use the tool, I just call it somehow, like listTopPlayers(10);

So that I don't need to write the code again everytime when I need it. Or if I want to change the tool, I don't have to change it everywhere in the code.

Thanks
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: Method();

Postby skydereign » Sun Oct 16, 2011 11:29 am

You mean functions? You declare them in global code using this format.
Global Code
Code: Select all
return_type
function_name (argument list...)
{
    // code
}

So for your function you'd probably set return_type to void.
Code: Select all
void
listTopPlayers (int myVariable)
{
    // code
}


Then call the function whenever.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Method();

Postby Troodon » Sun Oct 16, 2011 11:34 am

Thanks, that looks right. How do I call the function?

function_name(argument); ?
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: Method();

Postby Game A Gogo » Sun Oct 16, 2011 11:35 am

Exactly!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Method();

Postby Troodon » Sun Oct 16, 2011 12:01 pm

Thank you!
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: Method();

Postby lcl » Sun Oct 16, 2011 12:08 pm

You may find this useful, a tutorial of functions: viewtopic.php?f=4&t=10848

:D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Method();

Postby Troodon » Sun Oct 16, 2011 1:57 pm

Kiitän.

Still one thing, can I overload the function to do different things with different parameters?
I mean different depending if the user puts:
function_name();
function_name(argument);
function_name(argument,anotherArgument);
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: Method();

Postby Game A Gogo » Sun Oct 16, 2011 3:36 pm

unfortunately no, the parameters you put in your function declaration always has to be present when you use them
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Method();

Postby Troodon » Sun Oct 16, 2011 3:40 pm

Ok. Well it's good enough anyway. :)

Cool that this community is still active enough to give answers the same day one asks a question!
I can't die, I already tried
~on the forums since GE 1.3.3~
User avatar
Troodon
 
Posts: 1539
Joined: Thu Jan 12, 2006 3:29 pm
Location: HELL
Score: 56 Give a positive score

Re: Method();

Postby Game A Gogo » Sun Oct 16, 2011 8:04 pm

well we try! It's all about collective effort, or something :D
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest