Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: nick.keane on Sat 24/11/2007 04:28:15

Title: Function Overloads
Post by: nick.keane on Sat 24/11/2007 04:28:15
Got a quick question, possibly dumb :/.

Are function overloads possible in AGS?
Just considering the 3.0 beta, of course.
Title: Re: Function Overloads
Post by: monkey0506 on Sat 24/11/2007 04:46:30
AGS doesn't support overloads. You can do this (which isn't necessarily an overload, but allows you to have same-named functions multiple times):

struct MyStruct {
  import function MyFunction(int a);
  };

struct MyOtherStruct {
  import function MyFunction(int a);
  };

import function MyFunction(int a);

/*

struct MyThirdStruct {
  import function MyFunction(int a); // ERROR - function MyFunction already defined
  };

*/


As you can see you can use functions with the same name throughout structs unless (or until) a function with the same name has already been declared. So you can define MyStruct::MyFunction, MyOtherStruct::MyFunction, and MyFunction but once the global MyFunction has been declared you can't use it as the name of struct-functions any more.

AGS 3.0 provides basically the same set of rules: You can have functions with the same name across multiple structs (although only one function per name per struct ;)) unless a global function by the same name has already been declared.

However AGS 3.0 does provide extender methods so it's possible to include functions into pre-defined (both built-in and user) types:

struct MyStruct {
  import function DoSomething();
  };

import function MyFunction(this Character*, int a);
import function MyFunction(this GUI*, int a);
import function MyFunction(this Object*, int a);
import function MyFunction(this MyStruct*, int a);
import function MyFunction(int a);
// import function MyFunction(this GUIControl*, int a); // ERROR - function MyFunction already defined


Unfortunately this is as close to overloads as AGS currently allows.
Title: Re: Function Overloads
Post by: Radiant on Sat 24/11/2007 15:05:06
Furthermore, you can make parameters to a function optional, as long as you declare the function in a script header. This allows you to call the same function with e.g. one, two, or three parameters.
Title: Re: Function Overloads
Post by: monkey0506 on Sun 25/11/2007 03:50:47
Quote from: Radiant on Sat 24/11/2007 15:05:06as long as you declare the function in a script header

Actually you don't even have to make the function global, you just have to have an import defined for the function. I often make default parameters for internal functions by doing this:

// main script

import void DoSomething(int a, int b=7, c=9, d=382);

void DoSomething(int a, int b, int c, int d) {
  /* blah blah */
  }


The import line allows me to set default parameters, while putting the line in the main body of the script keeps it local to that script.