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.

File Database- read states from text file

Hi guys. I made this script to practice Ruby.
I decided to post here, maybe someone will find it useful.

what this script does:
-read states from a text file (numbers, strings, arrays)
-give a name to each value, for easy find. (hash table)
  each value in a line gets a name. ie, the first value is 'race'.
  to get the race in line x, do matfind( x, 'race' )
-can be used to add states to an enemy, or actor.
  save actor's data in line number 'actor_id' in the text file,
    actor's weapon is matfind(actor_id, 'weapon'), etc.

instructions are inside the code.

Code:
   #-------------------------------------------------------------------#
   #       File Database                                               #
   #      loads states from text file                                  #
   #-------------------------------------------------------------------#
   # created by silver wind.
   # * Description
   #   this script helps you easily insert data to your game.
   #   ie, if you want to add weapon, size and race to each enemy.
   #   instead of a matrix: {[3,34, "zombie"], [2,500,"fairy"]...}
   #   you can use a text file with: "3 34 #zombie
   #                                  2 500 #fairy"
   #   each value must be an integer, a string or an array of integers.
   #   arrays are for saving several weapons for each enemy, etc.
   #   a matrix called $matdata holds everything. (see 'how to use' below)
   #
   # * compatibility: this is a basic script- should work with SDK, 
   #    and every other script out there.
   #
   # * using the hash table
   #   edit the hash in line 112 to: 'state_name'=>index
   #   if the 1st value in each line is a race, do: 'race'=>0, and so on.
   #
   # * syntax for text file-
   #   break each line into values, seperated by ' ':
   #   example: "11 29 300"
   #   if your value is an array, use '[' before it and ']' after it.
   #   seperate values with ','.
   #   example: value 2 is an array- "value1 [a,b] value3"
   #   to add a string value, use # before it. exapmle : "num1 #myString num2"
   # 
   # how to use:
   # 1.make a new text file called 'myfile', with your data. save it inside your 
   # game folder. (c:/program files/Enterbrain/rmxp/project_name).
   # 2.add this line to Main : matrix()
   # 3.to access the saved data, use: matfind(line, state_name).
   #    *line is also your enemy/actor id. *There's no line 0.
   #    example: race of actor num 2 is- matfind(2, 'race')
   # 4.remember! these are CONSTANT values- they don't change during the game. 
   #     don't use this for enemy level or something similar.
   #
   # NOTE: you can add these states to Game_Enemy/Actor class, like this:
   # def state_name  
   #   return matfind(self.id,state_name)
   # end
   #
   # * replace state_name with your state name. make one for each new state.
   #   Now you can use enemy.race, and be happy! :p
   #
   # known bugs:
   # 1.no support for fraction values(1.5 will become 1).
   #--------------------------------------------------------------------------#
   
#-------------------------------------------------------
#  subArray() 
# convert arrays inside the line, from string 
# like: "[1,22,3]" to array with the values : '1', '22', '3'
#-------------------------------------------------------
def subArray(arr)
  arr2=[]
  i=0
  arr[arr.length-1]=arr[arr.length-1].chop
  for i in (0..arr.length-1)
    j=0
    arr2=Array.new
    # erase all ','
    arr[i].each_byte do |c| 
      if (c.chr!=',') 
        arr2[j]=c.chr
        j+=1
      end
    end
    arr[i]=arr2
  end
  # erase '['
  arr2= arr[0]
  size= arr2.length
 # size= arr[0].length -2
 # size= size-2
   for i in (0..size)
     arr[0][i]=arr[0][i+1]
     end
 #arr2[arr2.length-1]=nil 
 #arr[0]=arr2
  return arr
  #arr[0].each_byte do |c|
    #if (c.chr!=',') 
      #arr2[i]=c.chr
    #end
    #i=i+1
  #end
end
#--------------------------------------------------
# mat_to_i. 
#    convert to integer, unless first char is '#'.
#--------------------------------------------------
 def mat_to_i(str)
   unless (str[0]==35) # ascii value of '#'
     return str.to_i
   end
   str2=String.new
   for i in (0...str.length-1)
    str2= str2 << str[i+1]
  end
   return str2
 end
 
 #-----------------------------------------------
 # hash table
 #-----------------------------------------------
 # to get actor's race, use matfind(actor_id, 'race')
 def matfind(id, key)
   hash = {'species'=>0,'name'=>1,'type'=>2,'family'=>3,'skill_groups'=>4,
           'evoLv'=>5,'evoIDs'=>6,'evoTypes'=>7,'evoKeys'=>8,
           'maxSize'=>9, 'maxweight'=>10}
   state = hash[key]
   return $matdata[id-1][state]
   end
#-------------------------------------------------------------------
#    matrix() 
#    converts .txt file to a matrix.
#-------------------------------------------------------------------
def matrix()
  #looking for file 'myfile.txt' in the project folder..
  $matdata= Array.new
  sub_arr= Array.new
  arr2=[2,4,6]
  temp_arr=[]
  i=j=k=0
  aFile = File.open("myfile.txt", "r")
  data_table = IO.readlines("myfile.txt")
  
  data_table.each do |pokemon| 
    pokemon.each(' ') do |value|
      value= value.chomp(' ')
      if (value[0,1]=="[")
        #found sub array inside the curent line!
        value.each(',') do |val|
          sub_arr[k]=val
          k=k+1
        end
        k=0
        value=subArray(sub_arr)
        sub_arr=Array.new
      end

      temp_arr[i]=value
      i=i+1
    end
    # temp_arr is a single line from myfile.txt.
    #print temp_arr
    $matdata.push(temp_arr)
    temp_arr=Array.new
    j=j+1
    i=0
  end
  aFile.close
  #-----------------------------#
  # convert string to integer.  
  #-----------------------------#
    for i in (0..$matdata.length-1)
    for j in (0..$matdata[i].length-1)  #was $matdata[0]
      
      if ($matdata[i][j].is_a?(Array))
         # convert each value in the array to string
         for l in (0..$matdata[i][j].length-1)
           temp=$matdata[i][j][l]         
           $matdata[i][j][l]=String.new
           #copy each char, except the nil value.
           for k in (0..temp.length-2)
             unless(!temp[k])
            $matdata[i][j][l]= $matdata[i][j][l] << temp[k]
            end
          end
          k=temp.length-1
          if(temp[k])
            $matdata[i][j][l]= $matdata[i][j][l] << temp[k]
            end
           #$matdata[i][j][l]=$matdata[i][j][l].to_i
           $matdata[i][j][l]=mat_to_i($matdata[i][j][l])
         end
       end

      if ($matdata[i][j].is_a?(String))
        #$matdata[i][j]=$matdata[i][j].to_i
        $matdata[i][j]=mat_to_i($matdata[i][j])
      end
  
    end
  end
  # method end
  end
 
quote from the script:
  #  this script helps you easily insert data to your game.
  #  ie, if you want to add weapon, size and race to each enemy.
  #  instead of a matrix: {[3,34, "zombie"], [2,500,"fairy"]...}
  #  you can use a text file with: "3 34 #zombie
  #                                  2 500 #fairy"
  #  if your value is an array, use '[' before it and ']' after it.
  #  seperate values with ','.
  #  example: value 2 is an array- "value1 [a,b] value3"
  #  to add a string value, use # before it. exapmle : "num1 #myString num2"
numeric values are, obviously, converted to numbers. using # you can save the value as string.
 

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