Question about functions with ints

Started by James (Aka: The Geek), Thu 23/02/2006 21:08:36

Previous topic - Next topic

James (Aka: The Geek)

Riddle me this,lets say I am making a game (I'm not right now but I am curious about how this would work) :-\
and I have a function like this:

int red;
int blue;
int green;

function addsome (){
int ran random (2)
if (ran==0)red==1;
if (ran==1)red==2;
if (ran==2)red==3;
Ã,  }

I need to use this function many times but, often times I
need to do this with other ints.Ã,  :o
Is there any I could do this without having to write many functions and instead do something like this:

function addsome(int,red);
function addsome(int,blue);
function addsome(int,green);

Please explain to me how to do this
The first person to reply gets one million dollars ;D
(just kidding) ;)
"I reject you reality and substitute my own"     -Adam Savage

"When 900 years old you reach look as good you will not"     -Yoda

"LLAP!"     -James

Ashen

Well, you're kind of there ... Use an int in the function declaration, to specify which int is to be changed, something like:
Code: ags

function AddSome (int which) {
  int ran = 1 + Random(2); // I.e. will be 1, 2 or 3
  if (which == 0) red = ran; // NOTE: Single '=' for setting values. Only use '==' for checking.
  else if (which == 1) blue = ran;
  else if (which == 2) green = ran;
  // And so on for any other ints you may have
}


To make it less confusing, you could make an enum for the int names, e.g. (in the Global Header):
Code: ags

enum IntNames {
  eIntRed,
  eIntBlue,
  eIntGreen
  // And so on
};


And then the function would be:
Code: ags

function AddSome (IntNames which) {
  int ran = 1 + Random(2);
  if (which == eIntRed) red = ran;
  else if (which == eIntBlue) blue = ran;
  else if (which == eIntGreen) green = ran;
  // And so on
}
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk