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.

txt-file Script Reader using eval?

Title line says it all really, I'm trying to convert a txt file to be read as a script, using the built-in eval command that's supposed to evaluate a string. Now Yeah, here's my minimalistic attempt (doesn't include stuff like no comment line reading yet, but hey - I was trying to get it working first :p )...

[rgss]module ScriptReader
  #--------------------------------------------------------------------------
  def self.lineup?(id)
    entries = Dir.entries('Scripts/.')
    for entry in entries
      return true if entry.include?('RGSS' + id)
    end
    return false
  end
  #--------------------------------------------------------------------------
  def self.script_content(id)
    file = File.open('Scripts/RGSS' + id + ".txt")
    content = file.readlines
    return content
  end
  #--------------------------------------------------------------------------
end
 
 
begin
  Graphics.freeze
  entries = Dir.entries('Scripts/.')
  for id in '01'..'99'
    break unless ScriptReader.lineup?(id)
    script = ScriptReader.script_content(id)
    for line in script
      line.gsub('/n', '')
      eval(line)
    end
  end
  $scene.main while $scene != nil
end
[/rgss]

I get undefined errors when I run this reading some scripts, so I figure eval totally isn't meant to push scripts into the interpreter for permanent use, but more like for instant effect. I'd still like to see a solution for this, so here's my barebones script, if you can work anything out, please post it ^^

Just if anyone's interested, this was tested with my Database Flag reader script linked in my signature - if it should work for you, try it with that script!
 

Zeriab

Sponsor

If you are trying to evaluate text files as if they were .rb files I would suggest file.read instead of file.readlines and then simply evaluate that. I.e.
[rgss]   def self.script_content(id)
     file = File.open('Scripts/RGSS' + id + ".txt")
     content = file.read
     return content
   end
[/rgss]

[rgss]begin
  Graphics.freeze
  entries = Dir.entries('Scripts/.')
  for id in '01'..'99'
    break unless ScriptReader.lineup?(id)
    script = ScriptReader.script_content(id)
    eval(script)
  end
  $scene.main while $scene != nil
end
[/rgss]
 

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