Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

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

[rgss]class Game_Map
  #By Phoenixia
 
  def build_heap
    @text_heap = {}
    temp = {}
    page_temp = []
    leaf_temp = []
    label = ""
    title = ""
    start_seq = 0
    File.open("Text.txt", "r") do |file|
      file.each_line() do |line|
        case start_seq
        when 0
          title = line.chomp
          start_seq += 1
          next
        when 1
          line.chomp!.slice!(0)
          label = line
          start_seq += 1
          next
        end
        if line.include?(":EOF")
          page_temp << leaf_temp.dup()
          temp.store(label.dup(), page_temp.dup())
          @text_heap.store(title.dup(), temp.dup())
          leaf_temp.clear
          page_temp.clear
          temp.clear
          next
        elsif !line.include?("\t")
          page_temp << leaf_temp.dup()
          temp.store(label.dup(), page_temp.dup())
          @text_heap.store(title.dup(), temp.dup())
          page_temp.clear
          leaf_temp.clear
          temp.clear
          label = ""
          title = line.chomp
          next
        elsif line.include?("\t\t")
          line.slice!(0..1)
          leaf_temp << line.dup()
          next
        elsif line.include?("*****")
          page_temp << leaf_temp.dup()
          leaf_temp.clear
          next
        else
          unless label.empty?
            page_temp << leaf_temp.dup()
            temp.store(label.dup(), page_temp.dup())
            leaf_temp.clear
            page_temp.clear
          end
          line.chomp!.slice!(0)
          label = line.dup()
          next
        end
      end
    end
  end
 
  def get_stacks
    if @text_heap and @text_heap.include?(@map_id.to_s)
      @text_stacks.clear if @text_stacks
      @text_stacks = @text_heap[@map_id.to_s].dup()
    end
  end
 
  def get_stack(label)
    get_stacks if !@text_stacks
    @text_stack.clear if @text_stack
    @text_stack = @text_stacks[label].dup.reverse!
  end
 
  def stack?
    return !@text_stack.empty?
  end
 
  def stack_pop
    leaf = @text_stack.pop
    lines = leaf.size
    text = ""
    leaf.each() {|line| text += line}
    return [lines,text]
  end
end
[/rgss]

[rgss]$game_map.get_stacks
[/rgss]
 
[rgss]if $game_temp.message_text != nil
      # End
      return false
    end
    stack_flag = $game_map.stack?
    # Set message end waiting flag and callback
    @message_waiting = true
    $game_temp.message_proc = Proc.new { @message_waiting = false }
    # Set message text on first line
    unless stack_flag
      $game_temp.message_text = @list[@index].parameters[0] + "\n"
      line_count = 1
    else
      leaf = $game_map.stack_pop
      line_count = leaf[0]
      $game_temp.message_text = leaf[1]
    end
    # Loop
    loop do
      # If next event command text is on the second line or after
      if @list[@index+1].code == 401 and !stack_flag
        # Add the second line or after to message_text
        $game_temp.message_text += @list[@index+1].parameters[0] + "\n"
        line_count += 1
      # If event command is not on the second line or after
      else
        # If next event command is show choices
        if @list[@index+1].code == 102
          # If choices fit on screen
          if @list[@index+1].parameters[0].size <= 4 - line_count
            # Advance index
            @index += 1
            # Choices setup
            $game_temp.choice_start = line_count
            setup_choices(@list[@index].parameters)
          end
        # If next event command is input number
        elsif @list[@index+1].code == 103
          # If number input window fits on screen
          if line_count < 4
            # Advance index
            @index += 1
            # Number input setup
            $game_temp.num_input_start = line_count
            $game_temp.num_input_variable_id = @list[@index].parameters[0]
            $game_temp.num_input_digits_max = @list[@index].parameters[1]
          end
        end
        # Continue
        return true
      end
      # Advance index
      @index += 1
    end
  end
[/rgss]

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.
 
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...)
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top