Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Pax Animo on Sat 13/08/2022 17:45:41

Title: Optional parameters (strings) (Solved)
Post by: Pax Animo on Sat 13/08/2022 17:45:41
Eyup,

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

Code (ags) Select
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) Select
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.
Title: Re: Optional parameters (strings)
Post by: Snarky on Sat 13/08/2022 18:07:27
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) Select
import function target_animate(int, String saybg = 0);

(You should also use upper-case String instead of lower-case string.)
Title: Re: Optional parameters (strings) (Solved)
Post by: Pax Animo on Sat 13/08/2022 18:16:56
Thank you,

That's now all working as intended.
Title: Re: Optional parameters (strings)
Post by: Snarky on Sat 13/08/2022 18:27:24
Great!
Title: Re: Optional parameters (strings)
Post by: Pax Animo on Sat 13/08/2022 21:05:44
Quick side question,

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

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


if null is = 0?

Cheers.
Title: Re: Optional parameters (strings)
Post by: Snarky on Sat 13/08/2022 22:13:16
Yes, something like that works. I think there's also a function called (going by memory) String.IsNullOrEmpty() that you can use.