Messages from File Script

Message from File ScriptVersion: v1r
By: Phoenixia

Introduction

This script is fairly simple, it alows you to use a text file as the source for the message text rather than the event dialogs. My major reason for writing it is that I don't like using batch entry and it makes it easier to spell check/modify things; as opposed to wading through all of the individual events.

Screenshots

There really aren't any relevant screenshots that would show what this script does.

Script

  1. class Game_Map
  2.   #By Phoenixia
  3.  
  4.   def build_heap
  5.     @text_heap = {}
  6.     temp = {}
  7.     page_temp = []
  8.     leaf_temp = []
  9.     label = ""
  10.     title = ""
  11.     start_seq = 0
  12.     File.open("Text.txt", "r") do |file|
  13.       file.each_line() do |line|
  14.         case start_seq
  15.         when 0
  16.           title = line.chomp
  17.           start_seq += 1
  18.           next
  19.         when 1
  20.           line.chomp!.slice!(0)
  21.           label = line
  22.           start_seq += 1
  23.           next
  24.         end
  25.         if line.include?(":EOF")
  26.           page_temp << leaf_temp.dup()
  27.           temp.store(label.dup(), page_temp.dup())
  28.           @text_heap.store(title.dup(), temp.dup())
  29.           leaf_temp.clear
  30.           page_temp.clear
  31.           temp.clear
  32.           next
  33.         elsif !line.include?("\t")
  34.           page_temp << leaf_temp.dup()
  35.           temp.store(label.dup(), page_temp.dup())
  36.           @text_heap.store(title.dup(), temp.dup())
  37.           page_temp.clear
  38.           leaf_temp.clear
  39.           temp.clear
  40.           label = ""
  41.           title = line.chomp
  42.           next
  43.         elsif line.include?("\t\t")
  44.           line.slice!(0..1)
  45.           leaf_temp << line.dup()
  46.           next
  47.         elsif line.include?("*****")
  48.           page_temp << leaf_temp.dup()
  49.           leaf_temp.clear
  50.           next
  51.         else
  52.           unless label.empty?
  53.             page_temp << leaf_temp.dup()
  54.             temp.store(label.dup(), page_temp.dup())
  55.             leaf_temp.clear
  56.             page_temp.clear
  57.           end
  58.           line.chomp!.slice!(0)
  59.           label = line.dup()
  60.           next
  61.         end
  62.       end
  63.     end
  64.   end
  65.  
  66.   def get_stacks
  67.     if @text_heap and @text_heap.include?(@map_id.to_s)
  68.       @text_stacks.clear if @text_stacks
  69.       @text_stacks = @text_heap[@map_id.to_s].dup()
  70.     end
  71.   end
  72.  
  73.   def get_stack(label)
  74.     get_stacks if !@text_stacks
  75.     @text_stack.clear if @text_stack
  76.     @text_stack = @text_stacks[label].dup.reverse!
  77.   end
  78.  
  79.   def stack?
  80.     return !@text_stack.empty?
  81.   end
  82.  
  83.   def stack_pop
  84.     leaf = @text_stack.pop
  85.     lines = leaf.size
  86.     text = ""
  87.     leaf.each() {|line| text += line}
  88.     return [lines,text]
  89.   end
  90. end
Expand to see the code.


  1. $game_map.get_stacks
Expand to see the code.

 
  1. if $game_temp.message_text != nil
  2.       # End
  3.       return false
  4.     end
  5.     stack_flag = $game_map.stack?
  6.     # Set message end waiting flag and callback
  7.     @message_waiting = true
  8.     $game_temp.message_proc = Proc.new { @message_waiting = false }
  9.     # Set message text on first line
  10.     unless stack_flag
  11.       $game_temp.message_text = @list[@index].parameters[0] + "\n"
  12.       line_count = 1
  13.     else
  14.       leaf = $game_map.stack_pop
  15.       line_count = leaf[0]
  16.       $game_temp.message_text = leaf[1]
  17.     end
  18.     # Loop
  19.     loop do
  20.       # If next event command text is on the second line or after
  21.       if @list[@index+1].code == 401 and !stack_flag
  22.         # Add the second line or after to message_text
  23.         $game_temp.message_text += @list[@index+1].parameters[0] + "\n"
  24.         line_count += 1
  25.       # If event command is not on the second line or after
  26.       else
  27.         # If next event command is show choices
  28.         if @list[@index+1].code == 102
  29.           # If choices fit on screen
  30.           if @list[@index+1].parameters[0].size <= 4 - line_count
  31.             # Advance index
  32.             @index += 1
  33.             # Choices setup
  34.             $game_temp.choice_start = line_count
  35.             setup_choices(@list[@index].parameters)
  36.           end
  37.         # If next event command is input number
  38.         elsif @list[@index+1].code == 103
  39.           # If number input window fits on screen
  40.           if line_count < 4
  41.             # Advance index
  42.             @index += 1
  43.             # Number input setup
  44.             $game_temp.num_input_start = line_count
  45.             $game_temp.num_input_variable_id = @list[@index].parameters[0]
  46.             $game_temp.num_input_digits_max = @list[@index].parameters[1]
  47.           end
  48.         end
  49.         # Continue
  50.         return true
  51.       end
  52.       # Advance index
  53.       @index += 1
  54.     end
  55.   end
Expand to see the code.


Instructions

Paste the Game_Map portion of the script anywhere above main. The $game_map.get_stacks goes beneath
@message_window = Window_Message.new in Scene_Map inside of the main method. Overwrite comand101 in the interpreter with the rest of the code.

Using the text file: The file from which the messages are read is a text file inside of the flder containing your game; by default it's named Text.txt. The file uses labels to determine which map and which conversation are being called. Map id's are untabbed, conversation labels are tabbed, as are seperators, actual text is double tabbed. For example:
1
label a
a
b
c
d
*****
e
*****
f
g
h
label b
i

Has two different conversations "label a" and "label b". The stars divide up the blocks of text, each block is one message window's worth of content. So, on the map with id 1 the "label a" conversation is one window displaying
the a, b, c, d each on its own line, then the next window would display e, then f, g, h. If we wanted to add more, we would just add more labels. If we wanted to start on map 2, we would just start a new line and put 2 without any tabs; then everything beneath it would be the conversations for map 2.

On the last line, you need to put ":EOF", else it won't read everything.

When using it in the game: At the start of the event put "$game_map.get_stack("LABEL")", where LABEL is the
label you used for what this event will be saying. Then, everytime you cal a message window, just leave it blank, and it will show whatever message you have in the file instead; every blank window moves it on to the next message from the file.

Choices: If you have a choice that wil change the flow of the convesation inside of your event, then you will need to call $game_map.get_stack("LABEL") under each branch for whatever labels deal with that branch, same goes for anyother type of conditional branching.

FAQ

Nothing yet.

Compatibility

I write all of my own scripts, so I don't know about any compatibility issues, however if any come up, I'll be sure to add them here, let me know.

Author's Notes

Just a few things:
1.) There is nothing that prohibits players from modifying your text, however you could easily run the file through a simple cipher to prevent this. You could also Marshal it, though neither really matters if someone is determined, just pointing it out.
2.) If you wanted to increase the width or number of lines used in your message window, you might want to use this since text editors have no limitations on such things; whereas the message dialog does.

Terms and Conditions

I don't really care if you give me credit, though it would be nice. All I ask is that you leave my name in the comment at the top of the script. Also, if you are actually making a commercial game with XP, I could care less if you use this in it.
can you post a demo?
I've tried editing my post a couple times, each time I come back, after logging out, it's back to its orginal. So, I'm posting this as a reply.

Two things
First, you need to add "build_heap" at the bottom of the initialize method in Game_Map
(Sorry, I forgot this when I posted...)

Here's a demo
http://rapidshare.com/files/269416363/Text_Demo.rar

*Actually, if anyone has any suggestions/anything they think would be useful to change, please let me know.
Also, for anyone who finds this useful; the other day I was tossing around the idea of writing up a small language that could replace rmxps event dialogue system, aswel as add some additonal functions. I would make it simpler than doing it right out of the script editor; if anyone would have an interest in something like this, let me know.
(This script is, essntially, just a test to see how I would need to go about it...)

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=66904