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 Loading issues...

Status
Not open for further replies.

OS

Sponsor

I need to know what is wrong with this code:

Code:
  def generate_dialogue
    filename = "NPCS/#{@sex}#{@person}.npc"
    if FileTest.exist?(filename)
      file = File.open(filename, 'r')    
      dialogue = file.readlines
      return dialogue
    else
      return ["DIALOGUE ERROR: This NPC's dialogue could not be found.", "To fix the error, create the file \"#{filename}\""]
    end
  end

It tells me there is no file of the name "Male0.npc", even though I have made a folder and placed all the NPCs in it. The second NPC doesn't load either. Please tell me if I did something wrong in my code. I really appreciate any help.

Also, I can't supply the rest of the script, so if you have any questions, just ask.
 

OS

Sponsor

But it does exist. I placed the file in a folder I called NPCS. It is there, and I can prove it. See, it is right here:

http://i64.photobucket.com/albums/h163/akahi_theflame/Showthem.png[/IMG]

Those are the files, and if you check the address in the address bar you might notice they are in folder NPCS in my project folder, 0.0.0 To Billy and John.

Is the file named incorrectly in the script? I tried using \ instead of /, but that didn't work. This is a really annoying little problem...
 
Try begin rescue end instead.

Code:
begin
  return IO.readlines('Filename.txt')
rescue
  return ['File Not Found']
end

But you have to remember, it loads from your exe directory, so if you make a folder in your Game directory, be sure to include that in your call line.


Why are you naming them .npc? Are you hiding file extensions, because since it is a text document, they would either be filename.npc.txt or you did something else. Just name them filename_npc.txt.
 

OS

Sponsor

Oh. I thought that I could save it as anything I wanted in Notepad, and it would act like a new file type...

Well, you were right. I added .txt to that line, and it ran fine. Thanks.

Now for another question:

My class uses these files to generate different dialogues for NPCs, so I need to be able to have a way to call a specific NPC's information, such as Name and Gender. Gender is easy because each file is gender specific. But to get an NPC's name, I tried #{@name}, assuming that the dialogue would except it as the NPC's name because the dialogue is in the same class as the @name variable. But it doesn't show the name. Can someone please show me how to show variables in text files that have been loaded?
 
Status
Not open for further replies.

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