Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: imsomnia212 on Wed 20/05/2020 06:05:07

Title: Creating own speech gui
Post by: imsomnia212 on Wed 20/05/2020 06:05:07
Hi, I was trying to create my own function to write the speech on a gui, this function works perfect drawing the text on the gui, but the only problem is that the player can walk while and interact while the speech is draw in the gui (something like SayBackground) and the speech can't be skipped. Searching in the help menu of ags I found that you can use GUI.AsTextWindow but I can't get to work it. I use the thumbleweed script and the AGS version is the latest version.

I tried to add a timer to "Block" the interactions while the speech is on screen but doesn't work.

Code (ags) Select
// new module script
DynamicSprite*messagetext;

#define GWIDTH 300
#define GHEIGHT 30
#define GTIMER 1
#define TEST 1

void MySay(this Character*, String message) {
  if (messagetext == null) messagetext = DynamicSprite.Create(GWIDTH, GHEIGHT);
  DrawingSurface*ds = messagetext.GetDrawingSurface();
  ds.Clear();
  ds.DrawingColor = this.SpeechColor;
  int w = GWIDTH - 20;
  int wt = GetTextWidth(message, Game.SpeechFont) + 5;
  if (wt < w) w = wt;
  gSpeech.Width = w + 20;
  gSpeech.X = (Screen.Width - w - 20) / 2;
  int y = (GHEIGHT - GetTextHeight(message, Game.SpeechFont, w)) / 2;
  ds.DrawStringWrapped(10, y, w, Game.SpeechFont, eAlignCenter, message);
  ds.Release();
  gSpeech.BackgroundGraphic = messagetext.Graphic;
  gSpeech.Visible = true;
  SetTimer(TEST, (message.Length / game.text_speed));
  SetTimer(GTIMER, ((message.Length / game.text_speed) + 1) * GetGameSpeed());
}

function repeatedly_execute() { //this was supposed to work like "Blocking" but don't work
  if (gSpeech.Visible) {
    Wait(TEST);
  }
}

function repeatedly_execute_always() {
  GUI*g = gSpeech;
  gBlack.Visible = g.Visible;
  if (g.Visible) {
    gBlack.SetPosition(g.X, g.Y);
    gBlack.SetSize(g.Width, g.Height);
  }

if (IsTimerExpired(GTIMER)) gSpeech.Visible = false;

}
Title: Re: Creating own speech gui
Post by: Khris on Wed 20/05/2020 07:27:08
I'm pretty sure you can simply  Wait(((message.Length / game.text_speed) + 1) * GetGameSpeed());  at the end of the MySay function?
Title: Re: Creating own speech gui
Post by: imsomnia212 on Wed 20/05/2020 08:14:45
Oh I didn't know that will work, thanks. But now I can't get how to make the speech skippable
Title: Re: Creating own speech gui
Post by: Khris on Wed 20/05/2020 08:39:40
Use WaitMouseKey() instead.
Title: Re: Creating own speech gui
Post by: imsomnia212 on Wed 20/05/2020 09:32:00
I tried that but nothing happen
Title: Re: Creating own speech gui
Post by: Khris on Wed 20/05/2020 10:42:41
Can you be a bit more specific? And maybe post the code you're currently using?
Title: Re: Creating own speech gui
Post by: imsomnia212 on Wed 20/05/2020 22:21:35
The code is this:
Code (ags) Select
// new module script
DynamicSprite*messagetext;

#define GWIDTH 300
#define GHEIGHT 30
#define GTIMER 1

void MySay(this Character*, String message) {
  if (messagetext == null) messagetext = DynamicSprite.Create(GWIDTH, GHEIGHT);
  DrawingSurface*ds = messagetext.GetDrawingSurface();
  ds.Clear();
  ds.DrawingColor = this.SpeechColor;
  int w = GWIDTH - 20;
  int wt = GetTextWidth(message, Game.SpeechFont) + 5;
  if (wt < w) w = wt;
  gSpeech.Width = w + 20;
  gSpeech.X = (Screen.Width - w - 20) / 2;
  int y = (GHEIGHT - GetTextHeight(message, Game.SpeechFont, w)) / 2;
  ds.DrawStringWrapped(10, y, w, Game.SpeechFont, eAlignCenter, message);
  ds.Release();
  gSpeech.BackgroundGraphic = messagetext.Graphic;
  gSpeech.Visible = true;
  SetTimer(GTIMER, ((message.Length / game.text_speed) + 1) * GetGameSpeed());
  WaitMouseKey(((message.Length / game.text_speed) + 1) * GetGameSpeed()); //I added this but doesn't work
}

function repeatedly_execute_always() {
  GUI*g = gSpeech;
  gBlack.Visible = g.Visible;
  if (g.Visible) {
    gBlack.SetPosition(g.X, g.Y);
    gBlack.SetSize(g.Width, g.Height);
  }

if (IsTimerExpired(GTIMER)) gSpeech.Visible = false;

}
Title: Re: Creating own speech gui
Post by: Khris on Thu 21/05/2020 00:00:26
Remove the timer code, and again, please describe how the code fails.
"Doesn't work" is a useless problem description. We know it doesn't work.
Title: Re: Creating own speech gui
Post by: imsomnia212 on Thu 21/05/2020 02:33:01
Oh, sorry my bad. What I mean with that is the code works fine if I use only the wait function, the speech draws and I only can interact when it finishes, but if I use the waitmousekey the speech draws fine, I can interact but the speech is still draw on the screen (doesn't skipped with the click).
Title: Re: Creating own speech gui
Post by: Crimson Wizard on Thu 21/05/2020 02:45:34
I think you are missing "gSpeech.Visible = false;" after WaitMouseKey.
(and remove "if (IsTimerExpired(GTIMER)) gSpeech.Visible = false;" from rep-exec of course)

The blocking speech algorithm is simple:
* display speech
* call wait
* hide speech
Title: Re: Creating own speech gui
Post by: imsomnia212 on Thu 21/05/2020 03:07:47
Ooooh thank you so much! It worked perfectly now.