Optional parameter for eBlocking

Started by Pax Animo, Sun 21/08/2022 23:41:28

Previous topic - Next topic

Pax Animo

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

eri0o

on the header, you can import the function and declare the optional values there using the equal sign.

Pax Animo

How do i declare the optional value for eBlock/eNoBlock is it a bool?
Misunderstood

Crimson Wizard

in header:
Code: ags

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


in script:
Code: ags

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

Pax Animo

#4
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
Misunderstood

Pax Animo

#5
Quote from: Crimson Wizard on Mon 22/08/2022 00:04:54
in header:
Code: ags

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


in script:
Code: ags

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
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?
Misunderstood

Crimson Wizard

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

Pax Animo

#7
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 :-)
Misunderstood

Crimson Wizard

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

SMF spam blocked by CleanTalk