Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: TerranRich on Thu 02/07/2009 01:13:18

Title: Parameter default value must be literal [WORKED AROUND]
Post by: TerranRich on Thu 02/07/2009 01:13:18
What does that mean, in regards to this piece of code that the error points to?


import function Fade(this Object*, FadeDirection fdDir, float fSeconds = 0.5);


I'm guessing it's having trouble assigning a default value of 0.5 to the float variable fSeconds. What's going on?

This is the imported function in question:


function Fade(this Object*, FadeDirection fdDir, float fSeconds)
{
   if (fdDir == In) {
       // trans from 100->0
       this.Transparency = 100;
       this.Visible = true;
       this.TweenTransparency(fSeconds, 0, eEaseInEaseOutTween, eBlockTween);
   } else {
       // trans from 0->100
       this.Transparency = 0;
       this.TweenTransparency(fSeconds, 100, eEaseInEaseOutTween, eBlockTween);
       this.Visible = false;
   }
}
Title: Re: Parameter default value must be literal
Post by: Gilbert on Thu 02/07/2009 01:46:40
I may be wrong, but as far as I remember support for default values is limited. From the manual, it seems that only int values are supported (http://www.adventuregamestudio.co.uk/manual/function.htm), so only int variables or variables "compatible with" int (e.g. char, bool) are supported. In fact, as far as I remember the range of acceptable value is even limited to the short type (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=31829.0) and I don't know whether this is fixed.

So, you're possibly out of luck if you want to have default values of float or String.
Title: Re: Parameter default value must be literal
Post by: monkey0506 on Thu 02/07/2009 02:12:49
There was a similar topic 2 years ago here (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=31935).

float values still may not be made as optional parameters.

String and pointer values may have the value 0 (zero) substituted for null.

So you can't have optional floats, but you can do:

import void SayHello(this Character*, String name=0);

void SayHello(this Character*, String name) {
 if (name == null) { // parameter was omitted
   this.Say("Hello!");
 }
 else this.Say("Hello %s!", name);
}


Note that this works for String and pointer-types (Character*, GUI*, Hotspot*, Object*, etc.) ONLY!

Note that although the value null does not equate to 0 (that is, (null == 0) would return false), however as optional parameters we can substitute the value 0 for null. CJ is aware of this and although it's not something that's "officially supported" he did advise me that it should be okay for future use (so it's not something that's going to be "fixed").

If you need an optional float...what you could actually do is pass a String instead:

import function Fade(this Object*, FadeDirection fdDir, String fstrSeconds=0);

function Fade(this Object*, FadeDirection fdDir, String fstrSeconds)
{
   if (String.IsNullOrEmpty(fstrSecond)) fstrSeconds = "0.5";
   float fSeconds = fstrSeconds.AsFloat;
   if (fdDir == In) {
       // trans from 100->0
       this.Transparency = 100;
       this.Visible = true;
       this.TweenTransparency(fSeconds, 0, eEaseInEaseOutTween, eBlockTween);
   } else {
       // trans from 0->100
       this.Transparency = 0;
       this.TweenTransparency(fSeconds, 100, eEaseInEaseOutTween, eBlockTween);
       this.Visible = false;
   }
}

// with parameter
oMyobj.Fade(In, "0.75"); // or String.Format("%f", my_float)

// without parameter

oMyobj.Fade(In); // fSeconds defaults to 0.5


Using this method and the optional String workaround it would technically be possible to provide optional values for any data type/size. You would just have to create a variable in the function to catch the actual value and then pass it through the parameter list with String.Format.
Title: Re: Parameter default value must be literal
Post by: TerranRich on Thu 02/07/2009 07:56:43
That's a great workaround, monkey_05_06, thanks! I'll do just that. ;D
Title: Re: Parameter default value must be literal [WORKED AROUND]
Post by: monkey0506 on Thu 02/07/2009 11:35:12
Glad to hear you like it! Arguably it is more of a "hassle" having to convert all your floats into Strings to pass them through the parameter list, but such is the price of the optional float I suppose. 8)