Checking/Writing a specific word in a ruby file

Discussion in 'RGSSx Script Support' started by Andrea87Sky, Jun 30, 2019.

  1. Andrea87Sky

    Andrea87Sky Ah sh*t, here we go again. Veteran

    Messages:
    31
    Likes Received:
    1
    Location:
    Sicily
    First Language:
    Italian
    Primarily Uses:
    RMXP
    (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: Jun 30, 2019
    #1
  2. Andar

    Andar Veteran Veteran

    Messages:
    28,007
    Likes Received:
    6,330
    Location:
    Germany
    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)
     
    #2
  3. Andrea87Sky

    Andrea87Sky Ah sh*t, here we go again. Veteran

    Messages:
    31
    Likes Received:
    1
    Location:
    Sicily
    First Language:
    Italian
    Primarily Uses:
    RMXP
    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.
     
    #3
  4. Sixth

    Sixth Veteran Veteran

    Messages:
    2,121
    Likes Received:
    789
    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.
     
    #4
  5. Andrea87Sky

    Andrea87Sky Ah sh*t, here we go again. Veteran

    Messages:
    31
    Likes Received:
    1
    Location:
    Sicily
    First Language:
    Italian
    Primarily Uses:
    RMXP
    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
     
    #5
  6. Sixth

    Sixth Veteran Veteran

    Messages:
    2,121
    Likes Received:
    789
    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.
     
    #6
  7. Andrea87Sky

    Andrea87Sky Ah sh*t, here we go again. Veteran

    Messages:
    31
    Likes Received:
    1
    Location:
    Sicily
    First Language:
    Italian
    Primarily Uses:
    RMXP
    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?
     
    #7
  8. Sixth

    Sixth Veteran Veteran

    Messages:
    2,121
    Likes Received:
    789
    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.
     
    #8
  9. Andrea87Sky

    Andrea87Sky Ah sh*t, here we go again. Veteran

    Messages:
    31
    Likes Received:
    1
    Location:
    Sicily
    First Language:
    Italian
    Primarily Uses:
    RMXP
    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^
     
    #9

Share This Page