Problem with multiple optional parameters?

Started by monkey0506, Wed 15/02/2006 03:48:03

Previous topic - Next topic

monkey0506

Okay, I'll admit...I'm revising Akumayo's weather module...but it's being bothersome...I have the following in the module header:

Code: ags
struct WeatherEffects {
  import static void Start(int fallingspeed, int windspeed, int slot_a, int slot_b = 0, int slot_c = 0, int slot_d = 0, int slot_e = 0, int slot_f = 0, int slot_g = 0);
  // ...
  };


Then in the module script I have this:

Code: ags
static void WeatherEffects::Start(int fallingspeed, int windspeed, int slot_a, int slot_b, int slot_c, int slot_d, int slot_e, int slot_f, int slot_g/*, int slot_h, int slot_i, int slot_j, int slot_k, int slot_l*/) {
  // ...
  }


This all compiles and works fine.  Seeing as I'm using the default template and was too lazy to import any rain or anything, I just have some keys (7) raining all over the Caribbean.

As you probably guessed by the commented part of the function definition, I am trying to add more optional slot parameters.  Unfortunately, if I add the additional parameter (slot_h) to the declaration (in the header), so that it looks like this:

Code: ags
struct WeatherEffects {
  import static void Start(int fallingspeed, int windspeed, int slot_a, int slot_b = 0, int slot_c = 0, int slot_d = 0, int slot_e = 0, int slot_f = 0, int slot_g = 0, int slot_h = 0);
  // ...
  };


And then I add (uncomment) the parameter in the definition (in the script):

Code: ags
static void WeatherEffects::Start(int fallingspeed, int windspeed, int slot_a, int slot_b, int slot_c, int slot_d, int slot_e, int slot_f, int slot_g, int slot_h/*, int slot_i, int slot_j, int slot_k, int slot_l*/) {
  // ...
  }


The game crashes with the following error:

Quote---------------------------
Adventure Game Studio
---------------------------
Script link failed: Runtime error: unresolved import 'WeatherEffects::Start^10'

---------------------------
OK   
---------------------------

Am I doing something wrong here?  If I need to I can upload a copy of the game...but for now I have to go...school tomorrow *cries*...

Gilbert

As far as I remember AGS had a limit of something like at most 10 parameters for functions, this was true at least for older versions, don't know if it was raised to a higher limit with new versions though.

monkey0506

I thought it was 14.  I'm pretty sure...I was pretty sure...

GarageGothic

This is slightly off topic, but since this thread reminded me of it, it's as good as time as any to ask:

Could someone please direct me to any tutorials covering the static void thing? It's totally undocumented (a search of AGS help comes up with zero results for the word "void"), but I've seen it in other people's scripts and don't have a clue what it does.

Also, how do you tag a parameter to be optional? Whenever I write a function, it seems to expect the exact number of parameters that are specified in the function MyFunction(int param, string param) line.

Thanks.

Gilbert

I can't help with the void thing since I will never use it (I think it probably marks a function to return nothing, so if you try to get a value off from such function in your script there'll be an error, signaling you had done something wrongly, correct me if I'm wrong), which I wouldn't even care about it.

For optional parameters you can refer to the manual:
Scripting --> Script language keywords --> function

it's somewhere near the bottom.

GarageGothic

Thanks Gilbot, that's exactly what I needed (strange how I overlooked that when searching for "optional parameters" in the help).

scotch

You posted while I was typing this:

As Gil says, void is just another return type to give a function, like int or Object*, only this one indicates that is doesn't return anything. Ã, Use of that is encouraged in languages where you can specify it to make it clear that the function doesn't return anything to whoever is using the function, and so that the compiler can catch errors when people are trying to assign the result to something. Ã, There may also be some minute performance advantages.
I guess people just discovered it by accident (it's available in just about every statically typed C style language), the same goes for static member functions in structs... because they certainly aren't documented anywhere I have seen.
Are non static member functions allowed? Could someone please write something showing the various undocumented script language features? I feel like I'm doing things more laboriously than I could be...

For GarageGothic, functions inside structs (aka classes, in object oriented programming) are called "member functions", or "methods". Ã, When you call one you need to call it on an instance of the struct like:

struct MyStructType {
Ã,  Ã, float method(); // this is a normal method, I don't know if AGS can do them or takes this syntax
Ã,  Ã, static int staticMethod(); // note that "static" isn't connected to "void" at all, it's can be used with any return type
}

MyStructType s;
s.method();

which is very useful, the function will be able to manipulate stuff inside that struct instance "s". Ã, Sometimes you want something more like a global function, but would prefer to put it in the struct definition, just to look tidier really. Ã, This is what the "static" modifier is for; it basically tells the compiler that this is a global function that doesn't operate on a specific instance of the class, in fact, you can't call it on an instance of the class at all, you call it like:

MyStructType.staticMethod();

Gilbert

Quote from: scotch on Wed 15/02/2006 09:08:46
Are non static member functions allowed?
I think this must be allowed, otherwise you can't use the this pointer to access members of declared objects.

GarageGothic

Thanks for the explanation scotch - now my head hurts :) I see that I must really learn more about object oriented programming to understand the full scope of thse functions. I'm using large amounts of structs, but most of the time I'm relying on trial-and-compile-error to show me the way.

monkey0506

You can have static and nonstatic methods, but note that you have to use the keyword import before the return type.  So...you could do it like this:

Code: ags
struct MyStructType {
  import float method();
  import static int staticMethod();
  protected import float protectedMethod();
  protected import static int protectedStaticMethod(); // this is inaccessible in AGS conventions.
  };


Protected methods (& members) don't autocomplete, and are only accessible inside of another member function (in AGS).

Pumaman

The reason for a lot of this stuff being undocumented is that it's only half-finished ... for example, static member variables are not supported, so if static functions were "official" then you'd get loads of complaints that they were useless without static variables, and so forth.

One day when I get round to finishing the support for OO stuff, it'll all be properly documented.

SMF spam blocked by CleanTalk