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.

Manage text files with event commands

This script gives a little control to create and modifiy text files to eventers.
You can create a file, erase it, append text, add or remove a specific line.
Also you can configure the files extensions and paths.

You can ask for bugs, suggerences, compatibilitzations and any other things.

You can get the code in this simple web. Im going to use it on now because it saves alot of time updating posts and other things.

http://usuarios.multimania.es/kisap/english_list.html

Code:
#==============================================================================

# Manage text files with event commands

# By gerkrt/gerrtunk

# Version: 1

# License: GPL, credits

# Date: 05/01/2011

# IMPORTANT NOTE: to acces the more actualitzed or corrected version of this 

# script check here: [url=http://usuarios.multimania.es/kisap/english_list.html]http://usuarios.multimania.es/kisap/english_list.html[/url]

#==============================================================================

 

=begin

 

-------INTRODUCTION-----------

 

This script gives a little control to create and modifiy text files to eventers.

You can create a file, erase it, append text, add or remove a specific line.

Also you can configure the files extensions and paths.

 

----------INSTRUCTIONS----------------

 

1. First of all you have to create the text file. You do this with the

script command newTxt. That will create a new text file called at your will.

 

2. Then you can erase it using delTxt or modify it and continue to append contents

using appTxt, that add new text after the existent one. 

 

You can also check for the rest of the functions to do other things:

 

newTxt(name, text). This command create a new text file named 'name' and adds

the text 'text.

 

delTxt(name). This deletes the text file of the specified name. 

 

  newTxt("test", "Initial text") --> Creates a test.txt file with  Initial text inside.

  delTxt("test") --> Erases it

 

appTxt(name, text). Appends at the end of the file a new text. If you make

a newTxt over a file that already exist it will be overwritten.

 

  With the file called "test":

  "start of file and line 1

  line 2

  line3

  end of file and line 4

  "

  If you do a appTxt("test", "Append \n me \n))  it will result in:

  

  "start of file and line 1

  line 2

  line3

  end of file and line 4

  Append

  me

  

  "

 

addLine(name, text, line). Insert a line of text in the specified position of 

the file. A line means a piece of text thats beyond two \n (or the end of start 

of the file). The insert moves all the lines after the one you are pushing, so:

 

  With the file called "test":

  "start of file and line 1

  line 2

  line3

  end of file and line 4

  "

  If you do a addLine("test", "test line \n", 2) it will be inserted where the

  actual line 2 is, so it will result in:

  

  "start of file and line 1

  test line

  line 2

  line3

  end of file and line 4

  "

 

 

delLines(name, lines). Deletes a list of lines from the file named 'name'.

Works in a similar way that the add line but deleting them. It also

accepts more lines to erase:

 

 

  With the file called "test":

  "start of file and line 1

  line 2

  line3

  end of file and line 4

  "

  If you do a delLine("test", [2,4])  it will result in:

  

  "start of file and line 1

  line3

  "

 

-------SPECIAL CARACTERS------------

 

You can add new lines marks and tabulations marks using these codes:

 

\t (tab)

\n (newline)

 

This works internally for the system, but the reader doesent see ethese symbols 

but their true meaning. So if you write:

 

text = " testing \n \ttabulating it"

 

It will read as:

 

testing

  tabulating it

 

------CONFIGURATION-------------

 

Texts_file_path: Ifor you put this to empty "", it wont create a folder.

Also you have to add the \\ at the end of the folder name.

 

Texts_file_extension: You can changue the extension of all the text files.

 

 

=end

 

# Configuration

module Wep

  Texts_file_path = "Text Files\\"

  Texts_file_extension = ".txt"

end

 

  

 

 

 

 

 

# Create Folder for text files

if Wep::Texts_file_path  != ''

    Dir.mkdir(Wep::Texts_file_path) if !FileTest.directory?(Wep::Texts_file_path)

end

  

class Interpreter

  #--------------------------------------------------------------------------

  # * newTxt

  # creates a new text file

  # name: file name, text: text to write in the file

  #--------------------------------------------------------------------------

  def newTxt(name, text='')

    file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'w')

    file.write text

    file.close

  end

  

  #--------------------------------------------------------------------------

  # * delTxt

  # deletes a  text file

  # name: file name

  #--------------------------------------------------------------------------

  def delTxt(name)

    File.delete Wep::Texts_file_path + name + Wep::Texts_file_extension

  end

  

  #--------------------------------------------------------------------------

  # * addLine

  # insert line in text file

  # name: file name, text: text to write in the file, line: line number(-1 internal)

  #--------------------------------------------------------------------------

  def addLine(name, text, line)

    # Extract a array of lines from the file and insert text

    line_arr = File.readlines(Wep::Texts_file_path + name + Wep::Texts_file_extension)

    line_arr.insert(line-1, text)

    # Save it

    file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'w')

    file.write line_arr

    file.close

  end

  

  #--------------------------------------------------------------------------

  # * delLines

  # deletes lines in text file

  # name: file name, lines: array of line numbers to erase(-1 internal)

  #--------------------------------------------------------------------------

  def delLines (name, lines)

    # Extract a array of lines from the file and insert text

    line_arr = File.readlines(Wep::Texts_file_path + name + Wep::Texts_file_extension)

    # Delete specified lines

    lines.each do |index|

      line_arr.delete_at(index-1)

    end 

    # Save it

    File.open(Wep::Texts_file_path+name+Wep::Texts_file_extension, "w") do |f| 

      line_arr.each{|line| f.puts(line)}

    end

  end

  

  #--------------------------------------------------------------------------

  # * appTxt

  # appends text to a  text file

  # name: file name text: text to append

  #--------------------------------------------------------------------------

  def appTxt (name, text)

    file = open(Wep::Texts_file_path + name + Wep::Texts_file_extension, 'a')

    file.write text

    file.close

  end

  

end
 

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