Page 1 of 1

"Custom" side scrolling text - easy to use

PostPosted: Sun Apr 22, 2007 6:30 pm
by morcior
Since novices code isn't as efficient as it could be, and now sparky is posting his memory destroying version of the same code, I wrote up a cleaner piece of code that uses minimal CPU + RAM, has less than half the code, and has a much better way of calling the function to draw.

the code:
Code: Select all
char buf[512];
int pos, ptimer;
Actor *tgt;

#define TYPE_SPEED 5
#define APPEND_CHAR "_"

void drawText()
{
   if (ptimer < TYPE_SPEED) { ptimer++; return; }
   ptimer = 0;

   if (pos<0)
      if (pos==-1) { pos = -2; strcpy(tgt->text, buf); return; }
      else if (pos==-2) { pos = -1; strcpy(tgt->text, buf); strcat(tgt->text, APPEND_CHAR); return; }
      else return;
   
   strncpy(tgt->text, buf, pos);
   strncat(tgt->text, APPEND_CHAR, 1);
   
   pos++;
   if (pos > strlen(buf)) pos = -1;
}

void writeText(Actor *ent, char *txt)
{
   sprintf(ent->text, " ");
   sprintf(buf, txt);
   tgt = ent;
   pos = 1;
   ptimer = 0;
}


To use the code:

1. Copy the above code to global code
2. Create a "draw actor" event on a text actor with script editor action:

Code: Select all
drawText();


3. Use the function writeText() whenever you want to type out text. EG, in a mousedown event on an actor:

Code: Select all
writeText(&mytextactor, "hello world");


The first parameter of the function is the name of the text actor you wish to draw to. Make sure that all the text actors you wish to use typing on have the drawText(); call in their "Draw Actor" event.

You can adjust the typing speed and the character appended to the text by modifying the 2 #DEFINE statements at the top of my code. If you wanted to make it play a sound as it types its 1 extra line of code. If you cant see where that line would go then post here and I'll help.

PostPosted: Sun Apr 22, 2007 7:37 pm
by pixelpoop
every time I try out your code my GE crashes! I don't know what I am doing wrong

PostPosted: Sun Apr 22, 2007 11:50 pm
by morcior
make sure when you call writeText() to include a "&" infront of the actor name that you want to draw to.

INCORRECT
Code: Select all
writeText(an_actor, "hello");


CORRECT
Code: Select all
writeText(&an_actor, "hello");

PostPosted: Mon Apr 23, 2007 2:53 am
by pixelpoop
thats what I have. I will post it if you could please take a look

PostPosted: Mon Apr 23, 2007 2:55 am
by pixelpoop
here it is

PostPosted: Mon Apr 23, 2007 3:29 am
by morcior
I fixed the problem, in testing I had been writing text on the "view->create actor" event and had overlooked something in the drawText() routine.

I attached the fixed ged and updated the code above^

PostPosted: Mon Apr 23, 2007 11:19 pm
by pixelpoop
I just tried it out and it still crashes!? Did you upload the old version by mistake?

PostPosted: Tue Apr 24, 2007 2:03 am
by morcior
yes :(

PostPosted: Tue Apr 24, 2007 4:28 am
by pixelpoop
yaaa, it works...
but you have to change the game properties to "show mouse" for the mouse event to work.

thanks for the great contribution morcior, score ++;

PostPosted: Tue Apr 24, 2007 4:20 pm
by morcior
thanks :)

you don't *need* show mouse for the actor mouse down event to fire, it just makes it a lot easier to click on the actor! also your game had frame rate set to 250 or something so i reduced it to 30.

Re: "Custom" side scrolling text - easy to use

PostPosted: Fri May 15, 2009 10:52 pm
by Hblade
I'm getting an error in this code. The global code seems to be fine, but I put "writeText(pamtext, "hello world");" in, and it tells me cannot convert struct to * struck. wtf?

Re: "Custom" side scrolling text - easy to use

PostPosted: Fri May 15, 2009 10:59 pm
by Hblade
I fixed it.