Local Chat box (for Icarnia)

Young Jordie requested i make a chat box for him. Thought i may as well share it around.

so badum. here it is. Not sure if it works, but i tested it with myself and it seemed fine. If it’s faulty, i’ll be back later to fix and edit this post.

This shud hopefully display a litte box in the bottom left and show the last 10 things said… No scroller, the buffer is only 10 vars large. eh.

btw, if this is already somewhere on the forums, facepalm wasted time. neways, its almost 2am, time to try sleep.

Here you go:

// NPC made by potato
if (created||playerenters) {
  timereverywhere;
  this.drawIndexStart = 500;
  this.chatLines = 10;
  this.chatIndex = 0;
  setarray this.chatText, this.chatLines;
  setarray this.chatName, this.chatLines;
  timeout = .1;
}

if(playerenters){
  toweapons -chat;
}

if(playerchats){
  //transmit chat
  for(this.i=0;this.i<playerscount;this.i++){
    triggeraction players[this.i].x+1.45,players[this.i].y+1.45,SendChat,#a,#c;
  }
}

if(actionSendChat){
  //store chat into local array
  setstring this.chatName[#v(this.chatIndex)], #p(0): ;
  setstring this.chatText[#v(this.chatIndex)], #p(1);
  this.chatIndex++;
  if(this.chatIndex == this.chatLines){
    this.chatIndex = 0;
  }
}

if(timeout){
  //reset box position
  //divided by 16 to get tiles -> pixel
  rect.x = playerx-18.5;
  rect.y = playery+4.6;
  rect.width = 24;
  rect.height = 12;

  //restrict to screen values
  if( rect.x < 0 ){ rect.x = 0; }
  if( rect.y < 0 ){ rect.y = 0; }
  if( rect.width > screenwidth/16 ){ rect.width = screenwidth/16; }
  if( rect.hieght > screenwidth/16 ){ rect.hieght = screenhieght/16; }
  if( rect.x+rect.width > 64 ){ rect.x = 64-rect.width; }
  if( rect.y+rect.height > 64 ){ rect.y = 64-rect.height; }

  //show bg
  showimg 260,chat_background.png,rect.x-.5, rect.y-.5;
  changeimgcolors 260,1,1,1,.1;
  changeimgvis 260, 2;

  //draw box title
  showimg 256,@TEMPSITC.TTF@@Chat,rect.x+.5, rect.y;
  changeimgcolors 256,.2,.2,.7,1;
  changeimgzoom 256, .6;
  changeimgvis 256, 3;

 //draw chat
  this.drawIndex = this.drawIndexStart;
  this.chatIndexRead = this.chatIndex-1;
  if(this.chatIndexRead < 0 ){ this.chatIndexRead = 0; }
  for(this.i = 0; this.i < this.chatLines; this.i++){
    setstring this.chatname, #s(this.chatName[#v(this.chatIndexRead)]);
    setstring this.chattext, #s(this.chatText[#v(this.chatIndexRead)]);
    showimg this.drawIndex,@TEMPSITC.TTF@bold@#s(this.chatname),rect.x+.5, rect.y+this.chatLines-this.i;
    if( startswith(#a,#s(this.chatname)) ){
      changeimgcolors this.drawIndex,.7,1,.7,1;
    } else {
      changeimgcolors this.drawIndex,1,1,1,1;
    }
    changeimgzoom this.drawIndex, .6;
    changeimgvis this.drawIndex, 3;
    this.drawIndex++;
    showimg this.drawIndex,@TEMPSITC.TTF@@#s(this.chattext),rect.x+.8+(textwidth(.6,TEMPSITC.TTF,bold,#s(this.chatname))/16), rect.y+this.chatLines-this.i;
    if( startswith(#a,#s(this.chatname)) ){
      changeimgcolors this.drawIndex,.7,1,.7,1;
    } else {
      changeimgcolors this.drawIndex,1,1,1,1;
    }
    changeimgzoom this.drawIndex, .6;
    changeimgvis this.drawIndex, 3;
    this.drawIndex++;
    this.chatIndexRead--;
    if(this.chatIndexRead == -1){
      this.chatIndexRead = this.chatLines-1;
    }
  }
  timeout = .05;
}

Lil bulky, and it looks like you’re mixing variables with strings.
Variables arrays and String arrays with very differently.

addstring list,text;
insertstring list,index,text;
replacestring list,index,text;
removestring list,text;
deletestring list,index;
sarraylen(list)
lindexof(list,text)
#I(list,index) - gives you the string on place ‘index’

seems like an awesome idea. i would keep either a certain number of strings as server
variables or one server variable array to contain the most recent strings. have every
client’s npc update the current server strings according to what has been said… either with
a generic playerchats to capture everything or a specific start string to make it register.
ps. might incorporate this idea…

i noticed those in the commands.rtf but im still not quite sure how to use them ^^"
how do i delcare a string array? are string arrays read only, cos it seems there are only functions to add or remove, none to alter?
Also, as you said, my way is a bit confused… but it seems to still work, what are the pros and cons of doing what i did and using string arays? =D

i was planning to do that initialy. but that would be a gloabl chat, which always gets ugly ><
This one is juts local =D

String arrays are pretty much any string with

setstring string,hello,your,name,is,now,bobert;
Basically, quotes and command define indexes.

ahh sweet, i’ll edit this later to be more ‘conventional’ xDDDD


Edit:

Heres revision 2 =DDDD
i used string arrays this time. and i fixed up the positioning, so it still appears in the corect place even if the screen is resized o.o

// NPC made by potato
if (created) {
  this.chatLines = 7;
  this.rect.maxwidth = 150;
  this.rect.height = 140;
  setstring this.background_img,chat_background.png;

  timereverywhere;
  this.drawIndexStart = 300;
  setstring this.chatText,;
  setstring this.chatName,;
  for(this.i=0;this.i<this.chatLines;this.i++){
    addstring this.chatName,;
    addstring this.chatText,;
  }
}

if(isweapon){
  timeout = 0.1;
}

if(playerenters){
  toweapons -chat;
}

if(playerchats){
  if(strequals(#c,)){ return; }
  //transmit chat
  for(this.i=0;this.i<playerscount;this.i++){
    triggeraction players[this.i].x+1.45,players[this.i].y+1.45,SendChat,#a,#c;
  }
}

if(actionSendChat){
  //store chat into local array
  addstring this.chatName,#p(0);
  addstring this.chatText,#p(1);
  //remove oldest
  deletestring this.chatName,0;
  deletestring this.chatText,0;
}

if(timeout){
  //adjust width (expands if needed, other wise, it will shrink to max width)
  this.rect.width = this.rect.maxwidth;
  for(this.i=0; this.i < this.chatLines; this.i++){
    setstring this.chatname, #I(this.chatName,this.chatLines-this.i): ;
    setstring this.chattext, #I(this.chatText,this.chatLines-this.i);
    this.lineWidth = textwidth(.7,TEMPSITC.TTF,bold,#s(this.chatname))+textwidth(.7,TEMPSITC.TTF,bold,#s(this.chattext));
    if(this.lineWidth > this.rect.width){
      this.rect.width = this.lineWidth;
    }
  }
  //reset box position
  this.rect.x = screenwidth-this.rect.width;
  this.rect.y = screenheight-this.rect.height;

  this.drawIndex = this.drawIndexStart;

  //show bg
  showimg this.drawIndex,#s(this.background_img),this.rect.x, this.rect.y;
  changeimgmode this.drawIndex,2;
  changeimgcolors this.drawIndex,.5,.5,.5,.4;
  changeimgvis this.drawIndex, 4;
  this.drawIndex++;

  //draw box title
  showimg this.drawIndex,@TEMPSITC.TTF@bold@Chat,this.rect.x+20, this.rect.y+10;
  changeimgcolors this.drawIndex,.6,.6,.9,1;
  changeimgzoom this.drawIndex, .6;
  changeimgvis this.drawIndex, 4;
  this.drawIndex++;

  //draw chat
  for(this.i = 0; this.i < this.chatLines; this.i++){
    setstring this.chatname, #I(this.chatName,this.chatLines-(this.i+1)): ;
    setstring this.chattext, #I(this.chatText,this.chatLines-(this.i+1));
    if(strequals(#s(this.chattext),)){ continue; }
    //draw account name
    showimg this.drawIndex,@TEMPSITC.TTF@bold@#s(this.chatname),this.rect.x+10, this.rect.y+this.rect.height-20 -(this.i*16);
    if( startswith(#a,#s(this.chatname)) ){
      changeimgcolors this.drawIndex,.7,1,.7,1;
    } else {
      changeimgcolors this.drawIndex,1,1,1,1;
    }
    changeimgzoom this.drawIndex, .6;
    changeimgvis this.drawIndex, 4;
    this.drawIndex++;

    //draw message
    showimg this.drawIndex,@TEMPSITC.TTF@@#s(this.chattext),this.rect.x+16+textwidth(.6,TEMPSITC.TTF,bold,#s(this.chatname)), this.rect.y+this.rect.height-20 -(this.i*16);
    if( strequals(#a,#s(this.chatname)) ){
      changeimgcolors this.drawIndex,.7,1,.7,1;
    } else {
      changeimgcolors this.drawIndex,1,1,1,1;
    }
    changeimgzoom this.drawIndex, .6;
    changeimgvis this.drawIndex, 4;
    this.drawIndex++;
  }
  timeout = .05;
}

If this is for everyone to use can you pm me a img like it uses but blue like in final fantasy games if possible if not then nvm
okay i have used the img given and this revision version but it does not work i have tried

MAKE IT YOURSELF!

haha, yeh this is for everyone to use. as for the image. i have to agree with spoon, make it urself =.=

if u cant get it to align properly, send me the image and i’ll send u the dimensions to use.

have fun.

You should probably use shoot instead of triggeraction, because if the player’s lag exceeds the time it takes for him to move 2 tiles in any direction, he won’t get the message.

Plus, with your triggeraction way of doing it, your client has to send out a packet for every player in the level. Using shoot, you only send one.

ahhhhh ic
ty

I just tested this online with someone who was showing it to me and noticed that sometimes when they talk the chat doesnt show up on my chat screen…
possibly because you aren’t using the shoot method of passing information along to the clients?

Is seriously no one going to bitch about the lack of layer 4+ and how the image literally follows the player around, and not the screen?

OH HELL CAN’T GET LAYER 4+ AND THE IMAGE FOLLOWS THE PLAYER!! WHAT IS THIS SHIT!?

iuno, i was usinfg layer 3 before. trying to find a method to get absolute co-ordinate using the playerxy. so when i found out aoiut layer 4, it was a blessing x_____X

and yeh, when i can be bothered, i’ll switch to the shoot method. i just love triggeraction so much since i discovered it <3


edit:’
erm… yeh, i odnt undertsand the shoot method. i think this is right, not sure.

// NPC made by potato
if (created) {
  this.chatLines = 7;
  this.rect.maxwidth = 150;
  this.rect.height = 140;
  setstring this.background_img,chat_background.png;

  timereverywhere;
  this.drawIndexStart = 300;
  setstring this.chatText,;
  setstring this.chatName,;
  for(this.i=0;this.i<this.chatLines;this.i++){
    addstring this.chatName,;
    addstring this.chatText,;
  }
}

if(isweapon){
  timeout = 0.05;
}

if(playerenters){
  toweapons -chat;
}

if(playerchats&&isweapon){
  if(strequals(#c,)){ return; }
  //transmit chat
  setshootparams SendChat,#a,#c;
  shoot -10,-10,0.05,0.05,0.05,0.05,,,,,;
}

if(actionprojectile || actionprojectile2){
  if(strequals(#p(2),SendChat)){
    //store chat into local array
    addstring this.chatName,#p(3);
    addstring this.chatText,#p(4);
    //remove oldest
    deletestring this.chatName,0;
    deletestring this.chatText,0;
  }
}


if(timeout){
  //adjust width (expands if needed, other wise, it will shrink to max width)
  this.rect.width = this.rect.maxwidth;
  for(this.i=0; this.i < this.chatLines; this.i++){
    setstring this.chatname, #I(this.chatName,this.chatLines-this.i): ;
    setstring this.chattext, #I(this.chatText,this.chatLines-this.i);
    this.lineWidth = textwidth(.7,TEMPSITC.TTF,bold,#s(this.chatname))+textwidth(.7,TEMPSITC.TTF,bold,#s(this.chattext));
    if(this.lineWidth > this.rect.width){
      this.rect.width = this.lineWidth;
    }
  }
  //reset box position
  this.rect.x = screenwidth-this.rect.width;
  this.rect.y = screenheight-this.rect.height;

  this.drawIndex = this.drawIndexStart;

  //show bg
  showimg this.drawIndex,#s(this.background_img),this.rect.x, this.rect.y;
  changeimgmode this.drawIndex,2;
  changeimgcolors this.drawIndex,.5,.5,.5,.4;
  changeimgvis this.drawIndex, 4;
  this.drawIndex++;

  //draw box title
  showimg this.drawIndex,@TEMPSITC.TTF@bold@Chat,this.rect.x+20, this.rect.y+10;
  changeimgcolors this.drawIndex,.6,.6,.9,1;
  changeimgzoom this.drawIndex, .6;
  changeimgvis this.drawIndex, 4;
  this.drawIndex++;

  //draw chat
  for(this.i = 0; this.i < this.chatLines; this.i++){
    setstring this.chatname, #I(this.chatName,this.chatLines-(this.i+1)): ;
    setstring this.chattext, #I(this.chatText,this.chatLines-(this.i+1));
    if(strequals(#s(this.chattext),)){ continue; }
    //draw account name
    showimg this.drawIndex,@TEMPSITC.TTF@bold@#s(this.chatname),this.rect.x+10, this.rect.y+this.rect.height-20 -(this.i*16);
    if( startswith(#a,#s(this.chatname)) ){
      changeimgcolors this.drawIndex,.7,1,.7,1;
    } else {
      changeimgcolors this.drawIndex,1,1,1,1;
    }
    changeimgzoom this.drawIndex, .6;
    changeimgvis this.drawIndex, 4;
    this.drawIndex++;

    //draw message
    showimg this.drawIndex,@TEMPSITC.TTF@@#s(this.chattext),this.rect.x+16+textwidth(.6,TEMPSITC.TTF,bold,#s(this.chatname)), this.rect.y+this.rect.height-20 -(this.i*16);
    if( strequals(#a,#s(this.chatname)) ){
      changeimgcolors this.drawIndex,.7,1,.7,1;
    } else {
      changeimgcolors this.drawIndex,1,1,1,1;
    }
    changeimgzoom this.drawIndex, .6;
    changeimgvis this.drawIndex, 4;
    this.drawIndex++;
  }
}