Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Pax Animo on Sun 21/08/2022 23:41:28

Title: Optional parameter for eBlocking
Post by: Pax Animo on Sun 21/08/2022 23:41:28
Hey,

I'm wondering if there is a way to use an optional parameter in a function which checks if an animation runs as blocked or not, I'm currently checking which character ID has been passed into the function and then using a "if/else" check.

Code (ags) Select
function animate(int id, String saybg, int struct_id1, int struct_id2)
{
  if (character[id].Loop == 0 || character[id].Loop == 2 || character[id].Loop == 4 || character[id].Loop == 5) {
    if (id == player.ID) {
      character[id].Animate(8, 4, eOnce, eBlock);
      //
    }
    else {
      character[id].Animate(8, 4, eOnce, eNoBlock);
    }
  }


My goal would be to replace the eBlock/eNoblock with the parameter and get rid of the id check.
Title: Re: Optional parameter for eBlocking
Post by: eri0o on Sun 21/08/2022 23:47:45
on the header, you can import the function and declare the optional values there using the equal sign.
Title: Re: Optional parameter for eBlocking
Post by: Pax Animo on Mon 22/08/2022 00:03:51
How do i declare the optional value for eBlock/eNoBlock is it a bool?
Title: Re: Optional parameter for eBlocking
Post by: Crimson Wizard on Mon 22/08/2022 00:04:54
in header:
Code (ags) Select

import function animate(int id, String saybg, int struct_id1, int struct_id2, BlockingStyle block = eBlock);


in script:
Code (ags) Select

function animate(int id, String saybg, int struct_id1, int struct_id2, BlockingStyle block)
{
     character[id].Animate(8, 4, eOnce, block);
}
Title: Re: Optional parameter for eBlocking
Post by: Pax Animo on Mon 22/08/2022 00:09:00
Oh there we go, i didn't know about BlockingStyle, brilliant!, thank you eri0o and Crimson as all ways :-)

https://adventuregamestudio.github.io/ags-manual/StandardEnums.html#blockingstyle
Title: Re: Optional parameter for eBlocking
Post by: Pax Animo on Mon 22/08/2022 00:22:36
Quote from: Crimson Wizard on Mon 22/08/2022 00:04:54
in header:
Code (ags) Select

import function animate(int id, String saybg, int struct_id1, int struct_id2, BlockingStyle block = eBlock);


in script:
Code (ags) Select

function animate(int id, String saybg, int struct_id1, int struct_id2, BlockingStyle block)
{
     character[id].Animate(8, 4, eOnce, block);
}

Just to go a little bit deeper into this, i already have optional parameters in the function such as:

Code (ags) Select
import function animate(int, String saybg = 0, int = -1, int = -1);

I'm assuming the BlockingStyle should be the first optional parameter check as it takes precedence over the other optional parameters.

Edit: i see from your post you're naming the optional variables as they where declared, is this necessary or am i going to get a horrible bug for not doing so?
Title: Re: Optional parameter for eBlocking
Post by: Crimson Wizard on Mon 22/08/2022 01:28:48
Quote from: Pax Animo on Mon 22/08/2022 00:22:36
I'm assuming the BlockingStyle should be the first optional parameter check as it takes precedence over the other optional parameters.

Well, that's purely a question of convenience, but all the optional parameters must be after non-optional ones.

Quote from: Pax Animo on Mon 22/08/2022 00:22:36
Edit: i see from your post you are naming the optional variables as they where declared, is this necessary or am i going to get a horrible bug for not doing so?

No, names in import declaration and in function definition may be different.
One thing that I'd mention is that not giving any name at all means whoever uses this function won't know what these parameters mean (and script editor won't show their names in a hint).
Title: Re: Optional parameter for eBlocking
Post by: Pax Animo on Mon 22/08/2022 01:43:19
Quote from: Crimson Wizard on Mon 22/08/2022 01:28:48

One thing that I'd mention is that not giving any name at all means whoever uses this function won't know what these parameters mean (and script editor won't show their names in a hint).

Oh yeah, i did notice that i had to check on a few occasions what an optional parameter was used for as it just showed "int/bool and so on" in the editor hint.

Your mention clears up that question nicely, thanks again.

I also didn't know that the "e"Block was a enum, my knowledge of AGS has levelled up :-)
Title: Re: Optional parameter for eBlocking
Post by: Crimson Wizard on Mon 22/08/2022 13:36:27
Quote from: Pax Animo on Mon 22/08/2022 01:43:19
I also didn't know that the "e"Block was a enum, my knowledge of AGS has levelled up :-)

For the information, there's a list of all enums in the manual:
https://adventuregamestudio.github.io/ags-manual/StandardEnums.html