Checking/Writing a specific word in a ruby file

Andrea87Sky

Veteran
Veteran
Joined
Aug 30, 2017
Messages
97
Reaction score
1
First Language
English
Primarily Uses
RM2k3
(Sorry for the bad english :C)

(RPG Maker XP)

I wanna make the game to check if in a specific file there's a specific line that contains a specific word, like for example i wanna make the game to check if there's in the file "file01.rb" (in the Codes folder) contains in the first line "LauncherLoaded = true", and if there's that word to do a specific action, and if there's not that word to do another action.

Also it's possible to make the game to write specific things in that file?

I need a quick answer, please.
 
Last edited:

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
30,026
Reaction score
7,026
First Language
German
Primarily Uses
RMMV
For what maker do you need this?

If it is for VX Ace, then search the forum for a script called "SES External Text", that can show you how to handle textfiles in RGSS3 (which may or may not be similiar to RGSS and RGSS2)
 

Andrea87Sky

Veteran
Veteran
Joined
Aug 30, 2017
Messages
97
Reaction score
1
First Language
English
Primarily Uses
RM2k3
For what maker do you need this?

If it is for VX Ace, then search the forum for a script called "SES External Text", that can show you how to handle textfiles in RGSS3 (which may or may not be similiar to RGSS and RGSS2)
Emh, i think it's a thing of ruby does, so i think every rpg maker game are the same about it, and i'm talking about of stuff like "if File.exist?("Codes/data_CTRL.txt")" not like the textbox. Anyway i'm talking about XP.
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,136
Reaction score
809
First Language
Hungarian
Primarily Uses
RMVXA
No, different engines use different Ruby versions, so it's not the same, or rather, not necessarily the same.

You could try something like this:
Code:
    File.open("filename_here","r+") do |f|
      f.each_line do |line| # This will iterate each line in the file
        next unless line =~ /LauncherLoaded=(.*)/
        # Do your stuff here
        # Access that regex variable with $1
      end
    end

Open the help files of your RPG Maker and there should be some information about the File module there (but I don't have XP, so I can't be sure about this).
Depending on what you want, you might need to change the opening mode ("r+" in the example) to suit your needs.
 

Andrea87Sky

Veteran
Veteran
Joined
Aug 30, 2017
Messages
97
Reaction score
1
First Language
English
Primarily Uses
RM2k3
No, different engines use different Ruby versions, so it's not the same, or rather, not necessarily the same.

You could try something like this:
Code:
    File.open("filename_here","r+") do |f|
      f.each_line do |line| # This will iterate each line in the file
        next unless line =~ /LauncherLoaded=(.*)/
        # Do your stuff here
        # Access that regex variable with $1
      end
    end

Open the help files of your RPG Maker and there should be some information about the File module there (but I don't have XP, so I can't be sure about this).
Depending on what you want, you might need to change the opening mode ("r+" in the example) to suit your needs.
Can't i do like:
"next unless line =~ /LauncherLoaded=true/
do stuff
else
if line is LauncherLoaded=false
do something else
end"?

Also it's possible to write in the file when the game is checking it? Like i wanna do which if it's "true" the game will write in the file "false" instead
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,136
Reaction score
809
First Language
Hungarian
Primarily Uses
RMVXA
That would go like this:
Code:
    File.open("filename_here","r+") do |f|
      f.each_line do |line| # This will iterate each line in the file
        if line.include?("LauncherLoaded=true")
          # Do your stuff here 
          # Access that regex variable with $1
        elsif line.include?("LauncherLoaded=false")
          # Do your other stuff here
        end
      end
    end

Writing into the file to a specific line/position requires the read-write position to be moved to that exact line first.
You can get the current read-write position with the f.pos method, and you can also modify it with f.pos = value if needed.

But if you don't know the syntax for a simple if/else statement, I suggest that you first learn the basic syntax of the Ruby language instead and than move on to the more complicated ones (such as this one - and if you want to write at a specific position in the file, it gets even more complicated).

But what do want to do with this file exactly? Because depending on what you want to use this for, it may be or may not be a good idea.
Writing a file a lot of times repeatedly is not good for performance, and it's also bad for any hard-disk, so you should avoid doing that at all cost.
 

Andrea87Sky

Veteran
Veteran
Joined
Aug 30, 2017
Messages
97
Reaction score
1
First Language
English
Primarily Uses
RM2k3
That would go like this:
Code:
    File.open("filename_here","r+") do |f|
      f.each_line do |line| # This will iterate each line in the file
        if line.include?("LauncherLoaded=true")
          # Do your stuff here
          # Access that regex variable with $1
        elsif line.include?("LauncherLoaded=false")
          # Do your other stuff here
        end
      end
    end

Writing into the file to a specific line/position requires the read-write position to be moved to that exact line first.
You can get the current read-write position with the f.pos method, and you can also modify it with f.pos = value if needed.

But if you don't know the syntax for a simple if/else statement, I suggest that you first learn the basic syntax of the Ruby language instead and than move on to the more complicated ones (such as this one - and if you want to write at a specific position in the file, it gets even more complicated).

But what do want to do with this file exactly? Because depending on what you want to use this for, it may be or may not be a good idea.
Writing a file a lot of times repeatedly is not good for performance, and it's also bad for any hard-disk, so you should avoid doing that at all cost.
Nah don't worry, i just wanna make a system like Undertale, ya know, that game remembers if you reset or delete the savefiles, i wanna make some choices in a file.

Anyway what about the writing question? How i write in a specific word in a specific line?
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,136
Reaction score
809
First Language
Hungarian
Primarily Uses
RMVXA
Code:
  def write_da_file(file)
    File.open(file,"r+") do |f|
      wpos = 0
      f.each_line do |line|
        if line =~ /LauncherLoaded=(.*)/
          wpos = f.pos - line.size 
          break
        end
      end
      f.pos = wpos
      f.write("LauncherLoaded=#{some_code}\n")
    end
  end
This should work. It checks if the line matches the regex. If it is, it sets the read-write position of the file to the start of the line.
After that, you can modify that line however you want with the f.write method. What I wrote there is just an example, you can rewrite that line any way you want.
Note that if the file got no such line, it will rewrite the first line with whatever you want to write on that specific line. You can avoid this by some extra checks to either skip the writing if that happens or add the line to the end of the file instead.

There are other ways of doing this, I just find this way the easiest to read.
 

Andrea87Sky

Veteran
Veteran
Joined
Aug 30, 2017
Messages
97
Reaction score
1
First Language
English
Primarily Uses
RM2k3
Code:
  def write_da_file(file)
    File.open(file,"r+") do |f|
      wpos = 0
      f.each_line do |line|
        if line =~ /LauncherLoaded=(.*)/
          wpos = f.pos - line.size
          break
        end
      end
      f.pos = wpos
      f.write("LauncherLoaded=#{some_code}\n")
    end
  end
This should work. It checks if the line matches the regex. If it is, it sets the read-write position of the file to the start of the line.
After that, you can modify that line however you want with the f.write method. What I wrote there is just an example, you can rewrite that line any way you want.
Note that if the file got no such line, it will rewrite the first line with whatever you want to write on that specific line. You can avoid this by some extra checks to either skip the writing if that happens or add the line to the end of the file instead.

There are other ways of doing this, I just find this way the easiest to read.
Thanks but i already found a method, and it's much way easier:
Code:
File.open("Data/Texts/file01.rb","r+") do |f|
        wpos = 0
        f.each_line do |line|
        if line.include?("LauncherLoaded=true")
          wpos = f.pos - line.size
          break
        end
      end
      f.pos = wpos
      f.write("LauncherLoaded=false")
    end
It's similiar to yours, but i used instead the code u gave me earlier about the lines checking, and i figured it by myself. :thumbsup-right::kaopride: Thanks anyway ^w^
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

The video of a psycho chasing people on the streets with a sword before getting subdued is wild. Apparent screenshots of his Twitter feed before it went private reveal a dangerous delusional mindset. I heard there was a bow person too. The crazies are really coming out of the woodworks.
Stream will be live shortly with some Witcher 3, followed by a session of the Interactive Text Adventure! Feel free to drop by!
Managed to find a way to hang a bed cover over my window. So no more being toasted alive in the afternoon!:kaoswt2:
So, uh, has anyone tried buying any resources from KokoroReflections? Because I've been looking for a 'buy' button on the site for like 10 minutes now and can't find one.
Any also thought it's too cringy to put the word "and you" in your credit as a special thanks?
Cause that's what I thought...

Forum statistics

Threads
97,993
Messages
948,390
Members
129,254
Latest member
Swiftz
Top