Making blocking code non-blocking (SOLVED)

Started by Kinoko, Mon 21/03/2005 07:32:03

Previous topic - Next topic

Kinoko

Is there any way I can make this code non-blocking? The Wait's are the problem but I'm not sure if/how I could use timers to get past this.

Code: ags

function TypeLineInvis (string say, int xpos, int vpos, int width, int delay, int wait) {
if (IsGamePaused()==0) {
	StrCopy (line, say);

	StopMoving(EGO);
	SetTextWindowGUI(31);
	  int textid=0;
	  int length=0;  
	  int i=0;
	  StrCopy(displayedline,"");
	  textid = CreateTextOverlay(xpos, 100, 400, 1, 1, displayedline);
	
		  //get string length, to know how long the loop must run
	  length=StrLen(line); //set string length
		 //start loop
	while((i<length) && (1||(Wait(delay)==0))) {
		 // pick character at position "i"and stick it at the end of string "displayed line"
	    StrFormat(displayedline, "%s%c", displayedline, StrGetCharAt(line,i));
		 //set textoverlay as displayedline
	    SetTextOverlay(textid,xpos,vpos,width,1,1,displayedline); 
	
		 // if a space is added to the string do not play a sound, else play a 'tick'
	    if (StrGetCharAt(line,i) == ' '){
	   }
	   else{
		PlaySound(1);
	    }
		   //increase the loop counter
	    i++;  
	   if (i==length) if (wait>0) Wait(wait);
	   }
	 RemoveOverlay(textid);
	 Wait(5);
}
}


SSH

Code: ags


#define Typetimer1   10
#define Typetimer2   11

  int textid;
  int length; 
  int typecount;

function TypeLineInvis (string say, int xpos, int vpos, int width, int delay, int wait) {
if (IsGamePaused()==0) {
StrCopy (line, say);

StopMoving(EGO);
SetTextWindowGUI(31);
  typecount=0;
  StrCopy(displayedline,"");
  textid = CreateTextOverlay(xpos, 100, 400, 1, 1, displayedline);

  //get string length, to know how long the loop must run
  length=StrLen(line); //set string length
//start loop
  SetTimer(Typetimer1, delay);
}


in rep_ex:

Code: ags

if (IsTimerExpired(Typetimer1)) {
   // pick character at position "i"and stick it at the end of string "displayed line"
    StrFormat(displayedline, "%s%c", displayedline, StrGetCharAt(line,typecount));
   //set textoverlay as displayedline
    SetTextOverlay(textid,xpos,vpos,width,1,1,displayedline);

    // if a space is added to the string do not play a sound, else play a 'tick'
    if (StrGetCharAt(line,typecount) != ' ') PlaySound(1);
   //increase the loop counter
    typecount++; 
   if ((typecount==length) {
    if (wait>0)) SetTimer(Typetimer2, wait);
    else RemoveOverlay(textid);
   } else {
      SetTimer(Typetimer1, delay);
   }
} else if (IsTimerExpired(Typetimer2)) {
  RemoveOverlay(textid);
}


or something like that...
12

Kinoko

Thanks a lot for that, the code looks good. ^_^ I'm just having a bit of an error with "width" because it's declared as a local in the function TypeLineInvis (... line, which means I can't use it in the SetTextOverlay(textid,xpos,vpos,width,1,1,displayedline); line which is in repeatedly_execute.

I'm not sure how to fix this.

SSH

Ooops, yeah, forgot about the arguments. Declare a global var:

int current_width;

and inside TypeLineInvis, do:

current_width = width;

and then change the rep_ex references to width to current_width.


You're not going to be doing more than one line at once, I assume?
12

Kinoko

#4
Nope, only one at a time.

What I ended up doing was pretty much that, but I had to do it for a whole bunch of variables. So now I have this:

Code: ags

#define Typetimer1   10
#define Typetimer2   11
  int textid;
  int length;
  int typecount;
  int i;
  int widtha;
  int delaya;
  int vposa;
  int xposa;
  int waita;

function TypeLineInvis (string say, int xpos, int vpos, int width, int delay, int wait) {
if (IsGamePaused()==0) {
	StrCopy (line, say);
	
	StopMoving(EGO);
	SetTextWindowGUI(31);
	  typecount=0;
	  StrCopy(displayedline,"");
	  textid = CreateTextOverlay(xpos, 100, 400, 1, 1, displayedline);
	
	  //get string length, to know how long the loop must run
	  length=StrLen(line); //set string length
	//start loop
	  SetTimer(Typetimer1, delay);
	  widtha=width;
	  delaya=delay;
	  vposa=vpos;
	  xposa=xpos;
	  waita=wait;
}
}


In repeatedly_execute:

Code: ags

if (IsTimerExpired(Typetimer1)) {
	  while((i<length) && (1||(Wait(delaya)==0))) {
	   // pick character at position "i"and stick it at the end of string "displayed line"
	    StrFormat(displayedline, "%s%c", displayedline, StrGetCharAt(line,typecount));
	   //set textoverlay as displayedline
	    SetTextOverlay(textid,xposa,vposa,widtha,1,1,displayedline);
	
	    // if a space is added to the string do not play a sound, else play a 'tick'
	     if (StrGetCharAt(line,typecount) != ' ') PlaySound(1);
	   //increase the loop counter
	      typecount++;
	     if (typecount==length) {
	       if (waita>0) SetTimer(Typetimer2, waita);
	       else RemoveOverlay(textid);
	       } 
             else {
	       SetTimer(Typetimer1, delaya);
	       }
	  } //
}
else if (IsTimerExpired(Typetimer2)) {
	    RemoveOverlay(textid);
	    }


(There were also some extra ('s missing here and there)

Now, the game runs and the function will execute, but after the line has typed out, the typing sound just goes on and on repeatedly.

EDIT: I've been over the code but I just can't see where it's going wrong. The -only- line I don't understand is while((i<length) && (1||(Wait(delaya)==0))) {, particularly the "(1||(Wait(delaya)==0))" bit. Still, just because I don't understand it, doesn't mean it has anything to do with what the problem is.

SSH

In rep_ex, change the "while" to an "if" and remove the Wait from the condition .i.e.

if (typecount < length) {
12

Kinoko

#6
Thanks heaps, SSH! ^_^ It works now.

Just in case anyone's interested, I've modified the code now to work the way I want it in a certain room.

In room:
Code: ags

TypeLineInvis("Aldora", 20, 160, 1, 10, 200);
int yposit=0;
int xposit=0;
while (yposit < 50) {
  SetViewport(xposit, yposit);
  Wait(6);
  yposit++;
  xposit++;
}


Function:
Code: ags

function TypeLineInvis (string say, int xpos, int vpos, int queue, int delay, int wait) {
if (IsGamePaused()==0) {
	StopMoving(EGO);
	SetTextWindowGUI(31);
	
	StrCopy(displayedline,"");
	StrCopy (line, say);
	typecount=0;
        textid = CreateTextOverlay(xpos, 100, 400, 1, 1, displayedline);
	length=StrLen(line);
	SetTimer(6, delay);
	delaya=delay;
	vposa=vpos;
	xposa=xpos;
	waita=wait;
	cue=queue;
	}
}


In repeatedly_execute_always:
Code: ags

if (IsTimerExpired(6)) {
	  if (typecount<length) {
	    StrFormat(displayedline, "%s%c", displayedline, StrGetCharAt(line,typecount));
	    SetTextOverlay(textid,xposa,vposa,275,1,1,displayedline);
	     if (StrGetCharAt(line,typecount)!=' ') PlaySound(1);
	     typecount++;
	     if (typecount==length) {
	       if (waita>0) SetTimer(7, waita);
	       else RemoveOverlay(textid);
	       } 
             else {
	       SetTimer(6, delaya);
	       }
	  }
}
else if (IsTimerExpired(7)) {
  RemoveOverlay(textid);
  SetTimer(5, 100);
}

if (IsTimerExpired(5)) {
  if (cue==1) TypeLineInvis("The great and powerful woman who created the lands, the oceans, the skies...", 20, 160, 0, 10, 200);
}


This allows me to have the lines follow one another whilst the screen scrolls - it looks great! Thanks so much for the help SSH ^_^

SMF spam blocked by CleanTalk