"Custom" side scrolling text - easy to use

You must understand the Game Editor concepts, before post here.

"Custom" side scrolling text - easy to use

Postby morcior » Sun Apr 22, 2007 6:30 pm

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.
Last edited by morcior on Mon Apr 23, 2007 3:23 am, edited 1 time in total.
morcior
 
Posts: 83
Joined: Fri Jan 12, 2007 12:16 am
Location: UK
Score: 9 Give a positive score

Postby pixelpoop » Sun Apr 22, 2007 7:37 pm

every time I try out your code my GE crashes! I don't know what I am doing wrong
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby morcior » Sun Apr 22, 2007 11:50 pm

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");
morcior
 
Posts: 83
Joined: Fri Jan 12, 2007 12:16 am
Location: UK
Score: 9 Give a positive score

Postby pixelpoop » Mon Apr 23, 2007 2:53 am

thats what I have. I will post it if you could please take a look
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby pixelpoop » Mon Apr 23, 2007 2:55 am

here it is
Attachments
type_text.zip
this crashes my GE, but gives no error warnings
(1.33 KiB) Downloaded 195 times
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby morcior » Mon Apr 23, 2007 3:29 am

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^
Last edited by morcior on Tue Apr 24, 2007 2:02 am, edited 1 time in total.
morcior
 
Posts: 83
Joined: Fri Jan 12, 2007 12:16 am
Location: UK
Score: 9 Give a positive score

Postby pixelpoop » Mon Apr 23, 2007 11:19 pm

I just tried it out and it still crashes!? Did you upload the old version by mistake?
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby morcior » Tue Apr 24, 2007 2:03 am

yes :(
Attachments
type_text.zip
:)
(194.79 KiB) Downloaded 206 times
morcior
 
Posts: 83
Joined: Fri Jan 12, 2007 12:16 am
Location: UK
Score: 9 Give a positive score

Postby pixelpoop » Tue Apr 24, 2007 4:28 am

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 ++;
User avatar
pixelpoop
 
Posts: 276
Joined: Tue Aug 29, 2006 9:32 pm
Score: 28 Give a positive score

Postby morcior » Tue Apr 24, 2007 4:20 pm

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.
morcior
 
Posts: 83
Joined: Fri Jan 12, 2007 12:16 am
Location: UK
Score: 9 Give a positive score

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

Postby Hblade » Fri May 15, 2009 10:52 pm

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?
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

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

Postby Hblade » Fri May 15, 2009 10:59 pm

I fixed it.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest