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.

Writing and Reading a txt file (Pokemon Essentials based)

Script Request Template
This is a request for a new script.

Script Title:
Writing to a txt file, then searching the txt file and displaying text according to data in text file (confusing yes, read on and I will give a better explanation
RMXP or RMVX:
XP
Detailed Description:
The script I am requesting should be very simple, but I'm completely stumped to how I should do it.
I would like, when the script is run, that it records: 2 variables (lets name these x and y) and the current map id.
I don't really mind the format of the exported data, but it could look something like this: x, y, mapid.
The second part of the script would be to search the data, and display y and mapid depending on x.

This is for a Pokemon game. For those who have played Pokemon, I intend for this script to display the summary data: " met at level 1 at route 100" or whatever. the two variables, x and y, would be the Pokemon's name and level respectively.
I'm using Pokemon Essentials (thanks so much Poccil!). The second portion of the script would be placed in a summary window, and depending on the Pokemon's name(I believe th syntax is pokemon.name) being viewed (variable x), it would display y and the mapid corresponding to the Pokemon's name.
Other Scripts I am using (in order):
Pokemon Essentials

A big thanks in advance to anyone who can help me.
 
Put a text file in the root folder of your game(where the Game.exe and Game.rxproj is)and put this in it:
[rgss] 
d1 = "variable x";
d2 = "variable y";
d3 = "map id";
@data = [d1,d2,d3];
 
[/rgss]
and in your code put
[rgss] 
file = File.open('<yourfile>.txt', 'rb')
eval(file.read)
file.close
 
[/rgss]
and to write the data, put
[rgss] 
s = "d1 = #{@variable_x};\n"+
     "d2 = #{@variable_y};\n"+
     "d3 = #{@map_id};\n"+
     "@data = [d1,d2,d3];"
file = File.open('<yourfile>.txt', 'wb')
file.write(s)
file.close
 
[/rgss]
 
Here you go something small that works easily ;)
I assumed there is a Pokemon.name and pokemon.level method for a pokemon

2 Methods: (the third one can't be called by you)

-> Pokemon_data.meet(pokemon, map_id ) (if map_id isn't specified, it'll use the current map_id ), this method record the pokemon object, in a hash. using his name as a key. then the level and map id are stored in an array as a value ( ie : :name => [ lvl , map_id ] )

-> Pokemon_data.get_date(pokemon) will return the array with the lvl and map_id.. of that pokemon.


Be aware that, it can only register one name for a pokemon ( two pokemons can't have the same name ), as far as i remember you can collect several identical pokemons in the real game...( i mean several with the same name ). You might need another way of identify them in a unique way.

[rgss] 
module Pokemon_data
  def self.meet(pokemon, map_id = $game_map.map_id)
    my_load
    # Get the data from the parameters
   
    name = pokemon.name
    level = pokemon.level
    map_id = map_id
   
    @data[name.to_sym] = [level, map_id]
   
    File.open('Pokemon_data.rb', 'wb') { |file| file.write "@data = "+@data.inspect }
  end
  def self.get_date(pokemon)
    my_load
    @data[pokemon.name.to_sym]
  end
 
  class << self
    private
    def my_load
      if @data.nil? # if no hash exists ( file hasn't been loaded or doesn't exist
        @data = {} # if no hash exists... create one
        $LOAD_PATH << "./" unless $LOAD_PATH.include? "./"
        if File.exists? "Pokemon_data.rb"
          Kernel.send:)load,"Pokemon_data.rb") # But overwrite data if the file exists
        else
          File.open('Pokemon_data.rb','wb') { |file| file.close} # just create an empty file
        end
      end
      @data # returns the hash
    end
  end
end
 
[/rgss]
 
Thanks alot you guys! Really appreciated.

Trebor, remember me? From back in the old Netplay + days :tongue:

Edit: Well, it works if I remove the square brackets from the write and load parts (@data[pokemon...].) If I leave the brackets I get a no method error ( for the brackets, something like no method error:[]= )
Any help here?

Edit2: This is the exact error I get:
---------------------------
Pokemon Essentials
---------------------------
Exception: NoMethodError

Message: undefined method `[]' for true:TrueClass

PokeBattle_Battle:41:in `get_date'

PokemonSummary:166:in `drawPageOne'

PokemonSummary:82:in `pbStartScene'

PokemonSummary:784:in `pbStartScreen'

PokemonScreen:846:in `pbSummary'

PokemonScreen:1301:in `pbPokemonScreen'

PokemonScreen:1202:in `loop'

PokemonScreen:1451:in `pbPokemonScreen'

PokemonMenu:154:in `pbStartPokemonMenu'

PokemonMenu:153:in `pbFadeOutIn'
 
ah interseting seems that the @data = Loadthefile returned true, it worked fine for me when i tested it on 1.8.6 on irb... :p strange
I'll fix it in an edit...

And no, sorry, i don't remember you...


EDIT: Happiness!
[rgss] 
module Pokemon_data
  def self.meet(pokemon, map_id = $game_map.map_id)
    my_load
    # Get the data from the parameters
   
    name = pokemon.name
    level = pokemon.level
    map_id = map_id
   
    @data[name.to_sym] = [level, map_id]
   
    File.open('Pokemon_data.rb', 'wb') { |file| file.write "@data = "+@data.inspect }
  end
  def self.get_date(pokemon)
    my_load
    @data[pokemon.name.to_sym]
  end
 
  class << self
    private
    def my_load
      if @data.nil? # if no hash exists ( file hasn't been loaded or doesn't exist
        @data = {} # if no hash exists... create one
        $LOAD_PATH << "./" unless $LOAD_PATH.include? "./"
        if File.exists? "Pokemon_data.rb"
          Kernel.send:)load,"Pokemon_data.rb") # But overwrite data if the file exists
        else
          File.open('Pokemon_data.rb','wb') { |file| file.close} # just create an empty file
        end
      end
      @data # returns the hash
    end
  end
end
 
[/rgss]
 
I don't see any difference between this one and the one you posted earlier :/

Edit: Disregard that, I didn't see that you had edited it.

Edit2: Error after error :p

Now I get:
---------------------------
Pokemon Essentials
---------------------------
Exception: TypeError

Message: cannot convert nil into String

SpriteWindow:541:in `text_size'

SpriteWindow:541:in `pbDrawTextPositions'

SpriteWindow:540:in `each'

SpriteWindow:540:in `pbDrawTextPositions'

PokemonSummary:196:in `drawPageOne'

PokemonSummary:83:in `pbStartScene'

PokemonSummary:954:in `pbStartScreen'

PokemonScreen:846:in `pbSummary'

PokemonScreen:1301:in `pbPokemonScreen'

PokemonScreen:1202:in `loop'
After using the basic Pokemon Essentials script to draw text:
[@pkmndata,210,65,0,numberbase,numbershadow],
(@pkmndata = Pokemon_Data.get_date(pokemon))

Hopefully this is the last one :p
 
That's because it didn't find the pokemon you wanted...
before displaying it, you should check if @pkmndata isn't nil
[rgss] 
unless @pkmndata.nil?
  display_my_data
end
 
[/rgss] ( i don't know the script so i'm just assuming stuff )
 
Nope, even if I specify the Pokemon inside the .rb file, it still returns nil. I also noticed that if I write the data again, it overwrites the existing data, is there no way to add to the existing data, and when loaded, it returns the level and the map id of the pokemon.name specified. Sorry if I wasn't clear enough in the first post.
 
well when you meet a pokemon it adds data to the hash, creates it if needed, and then save it to the file... when it "overwrites" the file it has the previous data inside it...
well that's what you ask , return y and map_id according to x.. right, where y is the level, and x the name? WHat did i miss?

told you that if you write the same pokemon name(but with actual different level/map_id), twice it'll overwrite the previous one with that name...
I can create something that might work as said in the pm i've sent earlier.

what do you mean by "specify the pokemon inside the rb file" ?
 
By specify, I mean I am in a scene where the pokemon.name equals the name in the .rb file. But still, when I have a dfiferent pokemon name it still overwrites the previous name. (e.g I had "Goldeen" inside , I called the write method when the name was "Magikarp" and it still overwrited the Goldeen data.
 

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