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.

Reading Lines in a .txt file

OS

Sponsor

How would I go about loading a *.txt file, reading each of it's lines and saving the lines read with the other data that is saved?

I think I am supposed to use Marshal.dump(data, filename) for the saving bit?

But for the loading bit, I am not sure what methods to call. I think I saw a File.open() method in an other post, but I can't remember.

Any help would be appreciated.

Peace!
 
True that RMXP uses Marshall.load and Marshall.dump for the savedata files, but it doesn't save to .txt files...

Simple READ to EOF (end of file):
Code:
  # Open a file for reading
  f = open(filename, "r")
  # Perform until the End of File
  while not f.eof?
    # fdata is read from the file
    fdata = f.gets
  end
  # Close the file
  f.close

Simple Write
Code:
  # Open the file for writing
  f = open(filename, "w")
  # Write a line to a file
  # (the \n makes a line break)
  f.print fdata + "\n"
  # Close the file
  f.close

This is VERY basic, and should be used for stuff you wouldn't have IN a save file, like a configuration file.
 

OS

Sponsor

I think I have it figured out, but thanks for posting anyways. I am using IO.readlines() as I see it used in NearFantastica's NPC Interaction Script. My own script appears to work, but I am getting an error in one of my smaller classes.

Here is the script:
Code:
class Scene_Data
  def initialize(filename = "NoFileFound", type = 0)
    file = filename
    if file == "NoFileFound"
      print "No File Found Error :: #{@file}"
      $scene = Scene_Title.new
    end
    @list = []
    if type == 0
      @type = "Males"
    elsif type == 1
      @type = "Females"
    elsif type == 2
      @type = "Animals"
    elsif type == 3
      @type = "Objects"
    end
    #file.upcase!
    setup(file, @type)
  end
  
  def setup(file, type)
    set_id
    @file = file + ".rxdata"
    @text = IO.readlines("Data/Interact Data/#{type}/#{@file}")
    load_variables(type)
  end
  
  def set_id
    if @type != "Object"
      for i in 0..$npc.size
        if $npc[i] == nil
          @id = i
          return
        end
      end
    elsif @type == "Object"
      for i in 0..$obj.size
        if $obj[i] == nil
          @id = i
          return
        end
      end
    end
  end
  
  def load_variables(type)
    if type == "Males" or type == "Females" or type == "Animals"
      $npc[@id].first_name = @text[0]
      $npc[@id].last_name = @text[1]
      $npc[@id].age = @text[2]
      return
    elsif type == "Objects"
      $obj[@id].name = @text[0]
      $obj[@id].items = @text[1]
    end
  end
  
  def main
    if type == "Males" or type == "Females" or type == "Animals"
      print $npc[0].last_name + ", " + $npc[0].first_name
    elsif @type == "Objects"
      print $obj[0].name + " " + $obj[0].items[3]
    else
      print "No File Found Error :: #{@file}"
      $scene = Scene_Title.new
    end
    $scene = Scene_Map.new
  end
end

Code:
class Scene_Title
  NPC_COUNT = 1
  OBJ_COUNT = 1
  def command_new_game
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Stop BGM
    Audio.bgm_stop
    # Reset frame count for measuring play time
    Graphics.frame_count = 0
    # Make each type of game object
    $game_temp          = Game_Temp.new
    $game_system        = Game_System.new
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    $game_self_switches = Game_SelfSwitches.new
    $game_screen        = Game_Screen.new
    $game_actors        = Game_Actors.new
    $game_party         = Game_Party.new
    $game_troop         = Game_Troop.new
    $game_map           = Game_Map.new
    $game_player        = Game_Player.new
    $npc = []
    $obj = []
    # NPC and OBJ
    if type == 0
      for i in 1..NPC_COUNT
        $npc[i] = Game_NPC.new
      end
    elsif type == 1
      for i in 1..OBJ_COUNT
        $obj[i] = Game_Object.new
      end
    end
    # Set up initial party
    $game_party.setup_starting_members
    # Set up initial map position
    $game_map.setup($data_system.start_map_id)
    # Move player to initial position
    $game_player.moveto($data_system.start_x, $data_system.start_y)
    # Refresh player
    $game_player.refresh
    # Run automatic change for BGM and BGS set with map
    $game_map.autoplay
    # Update map (run parallel process event)
    $game_map.update
    # Switch to Map Screen
    $scene = Scene_Map.new
  end
end

class Game_NPC
  # Attributes
  attr_accessor :first_name
  attr_accessor :last_name
  attr_accessor :age
  # Initialize
  def initialize
    @first_name = ""
    @last_name = ""
    @age = 0
  end
  # First Name = string
  def first_name=(string)
    @first_name = string
  end
  # Last Name = string
  def last_name=(string)
    @last_name = string
  end
  # Age
  def age=(int)
    @age = int
  end
end

class Game_Object
  # Attributes
  attr_accessor :name
  attr_accessor :items
  # Initialize
  def initialize
    @name = ""
    @items = []
  end
end

The script is meant to be activated from inside an NPC or OBJ. But I keep getting an error with my Game_NPC and Game_OBJ classes.

The Error is:

NoMethodError occurred

undefined method 'first_name=' for nil:NilClass

If someone can help, I would be grateful.

Peace!
 

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