Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: HovSky on Wed 15/11/2017 14:18:42

Title: Start a function after dialog
Post by: HovSky on Wed 15/11/2017 14:18:42
(https://i.imgur.com/cl8ATTV.png)

Hello,
how can I initialize a function after a dialog option?
Started with specific problem, but if I know it in general will help me immense.

Thank you :)
Title: Re: Start a function after dialog
Post by: Thanyx on Wed 15/11/2017 14:31:28
Hello,

I'm not sure if I understand your question correctly. But in general, if you want to add a code inside a dialog, you can just SPACE or TAB to a side and start writing.
When you tab or space in dialog section in AGS it will start writing a code.

Example:
Code (ags) Select


@S
Ego: Hello
Guy: Hi

1@
Ego: How are you?
Guy: Im fine.
   
    cYou.Walk(120, 35)
    cYou.FaceDirection(eDirectionLeft)

Ego: I like your picture on the wall.
Guy: Yeah, I painted it.

return

Title: Re: Start a function after dialog
Post by: Snarky on Wed 15/11/2017 14:45:49
You can't, really.

The easiest way is usually to just call the function just before you exit the dialog, like Thanyx explained. By indenting the line with a space, you can call regular AGS script functions from the dialog.

There's another way that can be useful if you have something that should happen for many/all dialogs, but it's a bit more complex, so only proceed if you really need it....

Spoiler
You can use a flag that keeps track of whether you're in a dialog (or better, what dialog you're in). The way this works is that you have to create a wrapper function that sets the flag when you start the dialog, and always call that instead of Dialog.Start(). You can use a script module like this:

Code (ags) Select
// Dialog Extension Header
import Dialog* CurrentDialog(static Dialog);
import Dialog* Finished(static Dialog);
import void StartCustom(this Dialog*);

// Dialog Extension Script
Dialog* _currentDialog;
Dialog* _finishedDialog;

Dialog* CurrentDialog(static Dialog)
{
  return _currentDialog;
}

Dialog* Finished(static Dialog)
{
  return _finishedDialog;
}

void StartCustom(this Dialog*)
{
  _currentDialog = this;
  this.Start();
}

function repeatedly_execute()
{
  // repeatedly_execute() doesn't (normally) run during dialogs,
  //so this will only set currentDialog to null once the dialog is over
  _finishedDialog = _currentDialog;
  _currentDialog = null;
}


Now in your code you can do something like:

Code (ags) Select
function cNeighbor_Interact()
{
  dNeighbor.StartCustom();  // Start the dNeighbor dialog, making sure we track it by using .StartCustom()!
}

function repeatedly_execute()
{
  if(Dialog.Finished() != null)
  {
    // Things you want to do any time ANY dialog has finished
  }
  if(Dialog.Finished() == dNeighbor)
  {
    // Things you want to do any time the dNeighbor Dialog in particular has finished
  }
}


(Code untested, there may be bugs)
[close]
Title: Re: Start a function after dialog
Post by: HovSky on Wed 15/11/2017 14:56:35
Quote from: Snarky on Wed 15/11/2017 14:45:49
You can't, really.

The easiest way is usually to just call the function just before you exit the dialog, like Thanyx explained. By indenting the line with a space, you can call regular AGS script functions from the dialog.

There's another way that can be useful if you have something that should happen for many/all dialogs, but it's a bit more complex, so only proceed if you really need it....

Spoiler
You can use a flag that keeps track of whether you're in a dialog (or better, what dialog you're in). The way this works is that you have to create a wrapper function that sets the flag when you start the dialog, and always call that instead of Dialog.Start(). You can use a script module like this:

Code (ags) Select
// Dialog Extension Header
import Dialog* CurrentDialog(static Dialog);
import Dialog* Finished(static Dialog);
import void StartCustom(this Dialog*);

// Dialog Extension Script
Dialog* _currentDialog;
Dialog* _finishedDialog;

Dialog* CurrentDialog(static Dialog)
{
  return _currentDialog;
}

Dialog* Finished(static Dialog)
{
  return _finishedDialog;
}

void StartCustom(this Dialog*)
{
  _currentDialog = this;
  this.Start();
}

function repeatedly_execute()
{
  // repeatedly_execute() doesn't (normally) run during dialogs,
  //so this will only set currentDialog to null once the dialog is over
  _finishedDialog = _currentDialog;
  _currentDialog = null;
}


Now in your code you can do something like:

Code (ags) Select
function cNeighbor_Interact()
{
  dNeighbor.StartCustom();  // Start the dNeighbor dialog, making sure we track it by using .StartCustom()!
}

function repeatedly_execute()
{
  if(Dialog.Finished() != null)
  {
    // Things you want to do any time ANY dialog has finished
  }
  if(Dialog.Finished() == dNeighbor)
  {
    // Things you want to do any time the dNeighbor Dialog in particular has finished
  }
}


(Code untested, there may be bugs)
[close]

This seems really usefull, and will test it definitely.

Quote from: Thanyx on Wed 15/11/2017 14:31:28
Hello,

I'm not sure if I understand your question correctly. But in general, if you want to add a code inside a dialog, you can just SPACE or TAB to a side and start writing.
When you tab or space in dialog section in AGS it will start writing a code.

Example:
Code (ags) Select


@S
Ego: Hello
Guy: Hi

1@
Ego: How are you?
Guy: Im fine.
   
    cYou.Walk(120, 35)
    cYou.FaceDirection(eDirectionLeft)

Ego: I like your picture on the wall.
Guy: Yeah, I painted it.

return



Thank you for such a fast reply, everything works as i imagined :-D