Parameter default value must be literal [WORKED AROUND]

Started by TerranRich, Thu 02/07/2009 01:13:18

Previous topic - Next topic

TerranRich

What does that mean, in regards to this piece of code that the error points to?

Code: ags

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:

Code: ags

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;
    }
}
Status: Trying to come up with some ideas...

Gilbert

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

monkey0506

#2
There was a similar topic 2 years ago here.

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:

Code: ags
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:

Code: ags
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.

TerranRich

That's a great workaround, monkey_05_06, thanks! I'll do just that. ;D
Status: Trying to come up with some ideas...

monkey0506

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)

SMF spam blocked by CleanTalk