Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Creator on Mon 16/08/2010 12:40:49

Title: Making optional variables (SOLVED)
Post by: Creator on Mon 16/08/2010 12:40:49
Is there a way to make optional variables in custom functions?
E.G.


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


However, I know that "optional" in not a keyword.
Title: Re: Making optional variables
Post by: LRH on Mon 16/08/2010 12:44:21
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?
Title: Re: Making optional variables
Post by: Creator on Mon 16/08/2010 13:05:32
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?
Title: Re: Making optional variables
Post by: Matti on Mon 16/08/2010 13:54:41
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.
Title: Re: Making optional variables
Post by: Gilbert on Mon 16/08/2010 14:04:34
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:

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


And in the corresponding header you put:

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:

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

Title: Re: Making optional variables
Post by: monkey0506 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. The following will work fine:

// 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.

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.
Title: Re: Making optional variables
Post by: Creator on Wed 18/08/2010 10:53:54
Thanks Gilbet, that's exactly what I wanted.
Title: Re: Making optional variables
Post by: Gilbert on Wed 18/08/2010 11:14:39
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.