Dialogues in text files
#1
Dialogues in text files
by trebor777
v 1.5

Nov 24, 2005

This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


Script's Purpose: Instead of put each message, in each event, I only call a command which refers to an ID in a array, array filled with the text file's content. This text file is created in Data folder.


Of course, there is a light syntax to respect, when you fill the text file. I put an Example at the end of this post.
I've told me this script could be useful, in case, for example, of a translation, or anything else. Every Dialogues, would be easier to find & to translate.
INSTRUCTIONS:
This Script intends to work with Dubealex's AMS, but it could work with a clear project, But you will only got 4 commands instead of .... a lot !

So here we go !
Version 1.5

What's New ?
- Only one script...no more redefined method in window_message refresh method
- Completely compatible with Dubealex's AMS
- Comment support


Code:
class Interpreter
  #--------------------------------------------------
  alias trebor_dial_command_101 command_101
  def command_101
    bool= trebor_dial_command_101
    $game_temp.message_text.gsub!(/\\[Dd]ial_pnj_ID\[([0-9]+)\]/) do
      $data_pnj[$1.to_i] != nil ? $game_temp.message_text=load_dial($data_pnj[$1.to_i]) : ""
    end
    $game_temp.message_text.gsub!(/\\[Dd]ial_story_ID\[([0-9]+)\]/) do
      $data_story[$1.to_i] != nil ? $game_temp.message_text=load_dial($data_story[$1.to_i]) : ""
    end
    if bool !=nil
      return bool
    end
  end
  #---------------------------------------------------
  def load_dial(str)  #str le message à transmettre type string
    @str=str.dup  # Enregistrement de la chaine originale
    @old_str=str.dup
    @command_array=[]
    @command_size=[]
    @command_trie=[]
    @com=[]
    @str=clear_com(@str)
    last_str=@old_str
    if str.size>=40
      nb = 40
      nb_ligne=(str.size/nb).ceil
      if nb_ligne>=4
        last_str="Message trop long"
      else
      for i in 1..nb_ligne
        deb_int=(nb*(i-1))
        fin_int=(nb*i)
        temp=@str[deb_int..fin_int]
        id_sp=temp.rindex(" ")
        if id_sp!=nil
          id=id_sp+(deb_int)
        else
          id =fin_int
        end
        str_g=@str[0..id]
        str_d=@str[(id+1)..(@str.size-1)]
        str_d==nil ? str_d="" : str_d=str_d
        @str=str_g+"\n"+str_d
      end
      for i in 0..@com.size-1
        deb_com=@com[i][1]
        nb_nl=@str[0..deb_com].count("\n")
        deb_com>1? str_fg=@str[0..deb_com+nb_nl-1] : str_fg=""
        deb_com>0? str_fd=@str[deb_com+nb_nl..@str.size] : str_fd=@str
        str_fg==nil ? str_fg="" : str_fg=str_fg
        str_fd==nil ? str_fd="" : str_fd=str_fd
        
        @str=str_fg+@com[i][0]+str_fd
      end
      last_str=@str
      end
    end
    return last_str # renvoie la chaine non nettoyée, découpée ou non
  end
  #---------------------------------
  def clear_com(str)
    begin
      last_text = str.clone
      str.gsub!(/\\[Vv]\[([0-9]+)\]/) { $game_variables[$1.to_i] }
    end until str == last_text
    str.gsub!(/\\[Nn]\[([0-9]+)\]/) do
      $game_actors[$1.to_i] != nil ? $game_actors[$1.to_i].name : ""
    end
    str.gsub!(/\\[Mm]\[([0-9]+)\]/) do
      $data_enemies[$1.to_i] != nil ? $data_enemies[$1.to_i].name : ""
    end
    str.gsub!(/\\[Pp]rice\[([0-9]+)\]/) do
      $data_items[$1.to_i] != nil ? $data_items[$1.to_i].price : ""
    end
    str.gsub!(/\\[Cc]lass\[([0-9]+)\]/) do
      $data_classes[$data_actors[$1.to_i].class_id] != nil ? $data_classes[$data_actors[$1.to_i].class_id].name : ""
    end
    str.gsub!(/\\[Mm]ap/) do
      $game_map.name    != nil ? $game_map.name    : ""
    end
    count_command(/\\[%]/,str)
    count_command(/\\[Ff]\[(.*?)\]/,str)
    count_command(/\\[Nn]ame\[(.*?)\]/,str)
    count_command(/\\[Pp]\[([-1,0-9]+)\]/,str)
    count_command(Regexp.new('/\\[Cc]\[([0123456789ABCDEF#]+)\]/'),str)
    count_command(/\\[Gg]/,str)
    count_command(/\\[Ss]\[([0-9]+)\]/,str)
    count_command(/\\[Aa]\[(.*?)\]/,str)
    count_command(/\\[Cc]\[([0-9]+)\]/,str)
    count_command(/\\[Tt]\[(.*?)\]/,str)
    count_command(/\\[.]/,str)
    count_command(/\\[|]/,str)
    count_command(/\\[>]/,str)
    count_command(/\\[<]/,str)
    count_command(/\\[!]/,str)
    count_command(/\\[~]/,str)
    count_command(/\\[Ee]\[([0-9]+)\]/,str)
    count_command(/\\[Ii]/,str)
    count_command(/\\[Oo]\[([0-9]+)\]/,str)
    count_command(/\\[Hh]\[([0-9]+)\]/,str)
    count_command(/\\[Bb]\[([0-9]+)\]/,str)
    count_command(/\\[Rr]\[(.*?)\]/,str)
    @command_trie=@command_array.sort {|x,y| x <=> y }
    for i in 0..@command_trie.size-1
      deb_com=@command_trie[i]
      j=@command_size[@command_array.index(deb_com)]
      fin_com=deb_com+j-1
      str_com=@old_str[deb_com..fin_com]
      if str_com[-1,1]!="]"
        str_com.chop!
      end
      @com.push([str_com,deb_com])
    end
    return str
  end
  #-----------------------------------------------------
  def count_command(reg,str)
    it = 0
    for i in 0..@old_str.size
      if @old_str.index(reg,it) !=nil
        id=@old_str.index(reg,it)
        it=id+1
        @command_array.push(id)
        last_str_size=str.size
        str.sub!(reg, "")
        diff=last_str_size-str.size
        @command_size.push(diff)
      end
    end
  end
  #-----------------------------------------------------
end
#==========================================================================
class Scene_Title
  alias dial_main main
  def main
    dial_main
    #$data_pnj =create_data("text/pnj.txt")
    $data_story = create_data("text/story.txt")
  end
  #--------------------------------------------------------------------------
  def create_data(filename)
    data =nil
    if FileTest.exist?(filename)
      filestr= IO.readlines(filename)
      data=[]
      it = 0
      for line in filestr
        phrase = line.split('\244')
        if phrase.length>1
          data.push(phrase[1].chomp)
          it+=1
        else
          if phrase[0][0,1] !='#' and it>=1
            data[it-1]+=phrase[0].chomp
          else
            next
          end
        end
      end
    end
    return data
  end
  #--------------------------------------------------------------------------
end


Now thr text file's constraints:

- 2 files to create in Data folder: PNJ.txt & story.txt
- You Must Save in UTF-8 in case you'll use accents or Special letter else RPG MAker won't display them.
- One message on One Line.
- Each message must begin by: Idvalue\244
the first begin with this ID value : 0.
- Of course, Sort IDs in Crescent Order^^...

The event message command :
- For a non-Playable Character: (I mean not very important dialogues)
Dial_pnj_ID[n°id]
- For a Story Dialogue:
Dial_story_ID[n°id]

An example of text file : (extract from my story.txt)
Code:
O\244Bonjour message IdO.
1\244\N[1]Message plus long d'id 1, de type normal qu'on pourrait croiser dans n'importe quel jeu, et qui s'avère en effet très long, n'est-ce pas ? oui c'est vrai qu'il est long! Même très très très long...
2\244prout
3\244 Valeur de la variable 0022 :\V[22]
4\244 Or : \G
5\244 Nom de la map : \map


EDIT from 25/11/2005 at 6 Pm :
I added the demos and change the old code by the updated one.

EDIT from 1/12/2005 at 8 Pm :
Version 1.5
Remove demo.
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Personal Save Files XP polraudio 0 2,645 11-05-2008, 01:00 PM
Last Post: polraudio
  Personal Save Files polraudio 0 2,503 07-08-2008, 01:00 PM
Last Post: polraudio
  Many Many Save Files!! stupidstormy36 0 2,581 04-10-2008, 01:00 PM
Last Post: stupidstormy36



Users browsing this thread: 1 Guest(s)