What I want to do is have a predefined battle function, I can code the function, all I need to know how to do is have the function sitting there, so when I call it, all it takes is one or two variables to set up and go. So every time I don't have to input damage calculators, defense algorithms etc.
Help would be greatly appreciated, thanks.

What I want to do is have a predefined battle function, I can code the function, all I need to know how to do is have the function sitting there, so when I call it, all it takes is one or two variables to set up and go. So every time I don't have to input damage calculators, defense algorithms etc.
Help would be greatly appreciated, thanks.

Wut?
I don't think we have enough information to help you to a great extent, but you may look into passing by value and passing reference, as well as passing arrays and pointers.

You're essentially describing the purpose of a function.

Here's an example attack function.

bool attack(int atk, int def)
{
  return (atk > def);
}

Here, the algorithm is simple: return true if the attack value is more than the defense value; otherwise, return false.
Now you can call this in code like this:

bool battle1 = attack(1,5);
// atk = 1, def = 5 so the attack is a failure (returns false)

// Now with some variables
int a = 12;
int b = 10;
bool battle2 = attack(a,b);
// atk = 12, def = 10 so the attack is a success (returns true);

That way you don't have to write out the code in the function each time. Also, it provides some abstraction in your code which allows you to focus more on what is going on and not so much on the implementation of a detailed part of the code.

Edited 5 Years Ago by Mouche: n/a

That may work, but all I was really asking for was to be able to sort a set of algorithms into one simple function that can be called with a few lines of code instead of retyping it. So in the function I could define how much damage is done, from specific integer values, then to use it all I would have to do is call a battle function every time I want a battle. So instead of writing

if (steps == 10)
//Insert battle algorithm here.

// I could do something like

if (steps == 10)
// some function to call a predefined "battle function"

If you want multiple battle algorithms in one function, use one of the parameters to decide between algorithms. For example:

bool attack(int atk, int def, int alg)
{
   if (alg == 0)
   {
      // Use one battle algorithm
   }
   else if (alg == 1)
   {
      // Use a different battle algorithm
   }
   else if (alg == 2)
   {
      // Use a third battle algorithm
   }
   else
   {
      // Use a fallback algorithm
   }
}

Though I don't understand why you'd want to do that exactly. Can you give some more details if that's not what you're looking for?

then to use it all I would have to do is call a battle function every time I want a battle. So instead of writing

That's the whole point of defining a function like I showed above. You define it somewhere such as a header file, and then later you can just call the function (e.g. attack()) to reuse all the code in the function.

This question has already been answered. Start a new discussion instead.