Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Cuthalion on Thu 03/09/2009 22:48:57

Title: Keep scrolling while a message is being displayed.
Post by: Cuthalion on Thu 03/09/2009 22:48:57
I'm quite new to AGS and I get lost easily ???. I've searched the forum for a feature like this one but didn't find anything suitable to what I want.

I'm making an intro sequence and have my room (1280x480) scrolling from left to right with:

function room_AfterFadeIn()
{
while (xpos < 640)
{
  SetViewport(xpos, 0);
  Wait(2);
  xpos = xpos + 1;
}
}


I would like to have a series of messages being displayed during that scrolling, but I don't know how to put them in and keep the room scrolling.

Thanks in advance ;)
Title: Re: Keep scrolling while a message is being displayed.
Post by: RickJ on Fri 04/09/2009 09:36:14
First of all allow me to explain what's wrong with the example code you give.   At runtime, AGS first executes user scripts that queue up commands that are later executed by the engine.  So when you put a SetViewport() function in a while loop, your script will continue executing while it iterates through the loop.   The engine will only see and execute the last SetViewport() command.  To achieve the scroll effect you seek it is necessary for your "while loop" to span multiple game scans.   This can be achieved using the repeatedly execute event as follows.

Create a GUI named gMsg that contains a label named gMsgLabel.   The add the following code to your room.  I don't have access to an AGS workstation at the moment so I can't test anything.  However, I think there is enough here to give you and idea of what needs to be done.


// Scroll Room variables
#define MAX_DELAY 1
int delay=0;
int xpos=0;

// Display message variables
#define MAX_MSG 10
#define MAX_MSGTIME 100
int msgtime=0;
int msgid=0;

function room_RepExec() {

   // Scroll Room
   delay++;
   if (delay>MAX_DELAY) {
      delay = 0;
      if (xpos<640) {
         xpos++;
         SetViewport(xpos, 0);
      }
   }

   // Enable message GUI
   if (msgid<MAX_MSG) gMsg.Visible = true;
   else gMsg.Visible = false;

   // Display Messages
   msgtime++;
   if (msgtime>MAX_MSGTIME) {
      msgtime = 0;
      msgid++;
      if (msgid=1) gMsgLabel.Text = "Message-1";
      else if (msgid=2) gMsgLabel.Text = "Message-2";
      else if (msgid=3) gMsgLabel.Text = "Message-3";
         :   
      else if (msgid=N) gMsgLabel.Text = "Message-N";
   }
}






Title: Re: Keep scrolling while a message is being displayed.
Post by: Cuthalion on Fri 04/09/2009 22:01:07
Thank you very much, RickJ. That was a good lesson of order and method, and I think I've understood every step you wrote. There seems to be some error though, as I'm getting a "Parse error in expr near 'msgid'", on line 34 [[if (msgid=1) gMsgLabel.Text = "Message-1";]].
I can't find what it is, as I've named the GUI and label as you said, it all looked perfect to my eyes.

(I'm using the latest version of AGS, so there should be no trouble with the gMsgLabel.Text function)
Title: Re: Keep scrolling while a message is being displayed.
Post by: Matti on Fri 04/09/2009 22:13:38
It has to be: if (msgid==1)...

Note the two equal signs you need when checking a variable..

RickJ must have missed that in his code but it's a mistake I make way to often myself..
Title: Re: Keep scrolling while a message is being displayed.
Post by: Cuthalion on Fri 04/09/2009 23:12:03
Yeah that was it, thanks Mr Matti!

Now I've been able to check it, it doesn't seem to work. The room doesn't scroll, and neither works the rest of the code ???.
Title: Re: Keep scrolling while a message is being displayed.
Post by: RickJ on Fri 04/09/2009 23:57:58
You can put in some display statements in RepExec to see what's going on. 

Display("delay=%d  xpos=%d", delay, xpos);   // to debug the scrolling

Display("msgid=%d  msgtime=%d", msgid, msgtime);   // to debug the message display

You may want to increase MAX_DELAY and MAX_MSGTIME so you can see what's going on more easily.
Execution of the script will pause on the Display() instruction,  press enter to continue.

@MR. Matti,  thanks for the correction.  I should know better than that  :-[
Title: Re: Keep scrolling while a message is being displayed.
Post by: Cuthalion on Sat 05/09/2009 00:42:05
Ok, I finally got it. AGS was expecting to get a "function room_AfterFadeIn()" and it would do nothing until I put it (empty) at the beginning of the script. There's so much I have to learn... :)

Thanks so much for your aid, it works like a charm. Now I'll be able to go on learning.
Title: Re: Keep scrolling while a message is being displayed.
Post by: Scorpiorus on Sat 05/09/2009 01:45:21
I think changing it in a while loop should also work, as long as you don't forget to regularly update the engine state by calling Wait(1) or another blocking command. But having it implemented via RepExec, as RickJ suggested, is much neater as the user interface is not disabled then, and some other things may be running as well.

QuoteIt has to be: if (msgid==1)... Note the two equal signs you need when checking a variable.. RickJ must have missed that in his code but it's a mistake I make way to often myself..

I've been doing some Delphi programming for quite a while. Now I constantly keep writting if(a=1), if(a<>1), a:=1 everywhere... God, that's really terrible to switch back and forth!
Title: Re: Keep scrolling while a message is being displayed.
Post by: Matti on Sat 05/09/2009 02:34:48
I used Turbo Pascal for many years, so I'm used to = and <> too..