Optional parameters (strings) (Solved)

Started by Pax Animo, Sat 13/08/2022 17:45:41

Previous topic - Next topic

Pax Animo

Eyup,

I'd like to have an optional parameter which is blank if unused, i'm trying:

Code: ags
import function target_animate(int, string saybg = "");


I've RTFM and have no problem setting optional int's but when trying with a string i get the following error,

Code: ags
Failed to save room room6.crm; details below
GlobalScript.ash(20): Error (line 20): Parameter default value must be literal


Edit: I could make the parameter none optional and just put "" in places where nothing is said but i don't like that idea and it feels unnecessary.   

Cheers for any help.
Misunderstood

Snarky

You can't set it to an empty string by default (since that is not, technically, a "literal" value). What you can do is set it to 0 (which is equivalent to null):

Code: ags
import function target_animate(int, String saybg = 0);


(You should also use upper-case String instead of lower-case string.)

Pax Animo

Thank you,

That's now all working as intended.
Misunderstood

Snarky


Pax Animo

#4
Quick side question,

Would the best approach to checking if the optional parameter has been used be along the lines of:

Code: ags
if (saybg != null) {
    character[id].SayBackground(saybg);
  }


if null is = 0?

Cheers.
Misunderstood

Snarky

Yes, something like that works. I think there's also a function called (going by memory) String.IsNullOrEmpty() that you can use.

SMF spam blocked by CleanTalk