Making optional variables (SOLVED)

Started by Creator, Mon 16/08/2010 12:40:49

Previous topic - Next topic

Creator

Is there a way to make optional variables in custom functions?
E.G.

Code: ags

function KillPeople(String motive, optional bool guilt)
{
    Stuff();
}


However, I know that "optional" in not a keyword.

LRH

I can't really think of a way of doing that, at least not in the manner you have given an example with. However, why not just make one of the guilt values stand for 'not applicable' or something similar?

Creator

Quote from: Domithan on Mon 16/08/2010 12:44:21
However, why not just make one of the guilt values stand for 'not applicable' or something similar?

What do you mean?

Matti

As a workaround you can set the optional variable to something that hasn't any effect, that's what Domithan meant I think. Wouldn't work for a bool obviously as you surely need both true and false..

Like:

function KillPeople(String motive, int guilt)
{
   if (guilt==1) ....
   else if(guilt==2) ...
}

That way guilt set to 0 or something higher than 3 wouldn't have any impact thus it would be kind of optional.

Gilbert

#4
You can create optional parameters to a function (to some extend) as long as this function is:
1. declared in the Global Script or in a Script Module; and
2. is imported in the Global Script header (or the header of the Module).

So, say, you may declare something in the Global Script or in a Module like:
Code: ags

function blah(int haha, int wawa){
  ...
}


And in the corresponding header you put:
Code: ags

import function blah(int haha, int wawa=-1);


Then, if you call the function blah() in your game and ignore the parameter wawa its value would be assumed to be -1:
Code: ags

blah(10);  //haha=10, wawa=-1


monkey0506

To clarify the use of import it does not have to be in a script header at all. The following will work fine:

Code: ags
// Script.asc

import function KillPeople(String motive, bool guilt=true);

function KillPeople(String motive, optional bool guilt)
{
    Stuff();
}


You can supply default values for any parameter types comparable to integer types, namely bool, char, short, int, and any enumerated type (including built-in and user-defined enums).

You can also supply a default value of null for any pointer type by substituting 0 (zero), but these types can only have a default value of null. You can't assign other pointer-values to these type of parameters.

Code: ags
import function DoIt(Character *theCharacter=0);

function DoIt(Character *theCharacter) {
  if (theCharacter == null) theCharacter = player; // substitute player character as default value
  // ...
}


Just to be totally clear on the point, the only real stipulations regarding import are that the import must appear before the function definition (in the same script or in a script header) and you must define the function before it can be used. But you will need an import to define default values for optional parameters.

Creator

Thanks Gilbet, that's exactly what I wanted.

Gilbert

Quote from: monkey_05_06 on Mon 16/08/2010 17:14:09
To clarify the use of import it does not have to be in a script header at all.
Yeah, of course. If you only want a function declared in say, the global script to be used in only a few rooms of the game you can just import that function on top of these rooms' scripts. I do this all the time. However, for a normal user, it's simpler to do this in the headers so that all the scripts have access to the function.

SMF spam blocked by CleanTalk