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) ;)
Well, you're kind of there ... Use an int in the function declaration, to specify which int is to be changed, something like:
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 (http://www.adventuregamestudio.co.uk/manual/enum.htm) for the int names, e.g. (in the Global Header):
enum IntNames {
eIntRed,
eIntBlue,
eIntGreen
// And so on
};
And then the function would be:
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
}