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.

Reading from file

I'm trying to read some stuff from file. The file is set up in the following format
[SectionA]
DisplayName=
Name=
[SectionB]
DisplayName=
Name=

I'm able to read the file, but its not saving the section name(in the brackets) for the first section, every other detail and other section is fine. If i put the section in twice its fine

This is the code
Code:
 

move = PSMove.new

file = File.new("DataFiles/moves.txt", "r")

while (line = file.gets)

    line.delete! "\n"

    line.delete! "\"\\"

    if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]

        section=$~[1] # The sections name contained in [x]

        if move.getInternalName != section

          moves.push(move)

          move = PSMove.new

        end

        move.setInternalName(section)

    else # Else of if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]

        split = line.split("=")

        if split[0] == "DisplayName"

          move.setDisplayName(split[1])

        end

    end

end

moves.push(move)

 

I think its to do with the "if move.getInternalName != section" bit, but am unsure how to alter it.
What i'm trying to do is when the section changes, push the move into the array, then recreate the move to use again
 
Mind uploading that file somewhere for me to take a look at it? Depending on how its encoded, that first line could easily have a character that you can't see. (Certain formats have a special character sequence to designate what they are, and most editors don't show them.)
 
I just modded your reader to get print tests of every action, and there are two things I've noted. First, line 6 never deletes anything. Second, it's working fine for me if I download your file directly. I'm not sure exactly what the issue is, but I'd imagine it's a mismatch between your getInternalName and the name you use in the config file. (I don't have those to test, so I commented those lines.) Here's the script I tested with:

Code:
file = File.new("DataFiles/moves.txt", "r")

while (line = file.gets)

    line.delete! "\n"

    line.delete! "\"\\"

    if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]

        section=$~[1] # The sections name contained in [x]

        p section

        #if move.getInternalName != section

        #  moves.push(move)

        #  move = PSMove.new

        #end

        #move.setInternalName(section)

    else # Else of if line[/^\s*\[\s*([a-zA-Z]+)\s*\]\s*$/]

        split = line.split("=")

        p split

        #if split[0] == "DisplayName"

        #  move.setDisplayName(split[1])

        #end

    end

end

#moves.push(move)
I've already set it not to report the output of the delete! commands. With this, I was able to successfully read every line as you apparently intended to.
 
I think i've found the issue

I have a class that loads in a text file and its made into an array (the array was previously saved using "save_data(moves,"Data/ms.dat")"

Its being loaded in the class by
Code:
def initialize

    @ms=load_data("Data/ms.dat")

  end # End of def initialize
and the class also has a get method for returning an item from the array
Code:
def getMS(value)

    a = -1

    for i in [email=0..@ms.size]0..@ms.size[/email] - 1

      p "Wanted: #{value} Found: #{@ms[i].getInternalName}"

      if @ms[i].getInternalName == value

        a = @ms[i]

      end # End of if @ms[i].getInternalName == value

    end # End of for i in [email=0..@ms.size]0..@ms.size[/email]

    return a

  end # End of def getMS(value)
Now this is called as such from another class
Code:
m = ms.getMS(split[0])

if m == -1

    raise "#{split[0]} not found"

end # End of if a == -1
But the getMS call is returning -1, and the message shown on screen when I print the "p "Wanted: #{value} Found: #{@ms.getInternalName}"" is
"Wanted: SectionA Found: SectionA"
though if i copy the message using ctrl-c its slightly different, it has a ? before the section name its found, which i believe is the issue.
"Wanted: SectionA Found: ?SectionA"
This doesn't happen though if i have it in the text file like so
SectionA,display name,name
SectionA,display name,name
SectionB,display name,name
If sectionA (or what ever is at the top) is in twice, its fine

While this implies that theres a character being inserted at the beginning of the first line, i can't see where its coming from.
The text files are made using notepad++ which doesn't hide characters, and as far as i know its not being inserted after the file being read
 
If you go to Notepad++, find this option in the menu and make sure it's ticked: View->Show Symbol->Show All Characters. That shows non-display characters like newlines and such. If you're troubled by the character, I'm pretty sure it's a byte order marker. You can find some information on it here.
 
ok, making it show all characters still didn't show anything at the start of the line. However, changing the encoding from UTF-8 to UTF-8 Without BOM sorted it. I've adjusted the settings in notepad++ to use the Without BOM version by default

Thanks for your help
 
I'd recommend that you consider filtering out that byte order marker, since it looks like you're making a configuration file, and you have no guarantee that someone else won't accidentally add it back in. That said, I'm just making an assumption, so if that's not the case then feel free to ignore me.
 
Depending on the type and quality of encryption you use, it can actually be more secure than you would expect. I'm not saying that there aren't other options, just that some kinds of encryption can legitimately be used without having to worry about players screwing things up. (For the most part. With a popular game, people will always try)
 

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