Why doesn’t it work?

Asked

Viewed 92 times

0

.Rb:2:in `read': No such file or directory @rb_sysopen - Gamesettings.json (Errno::ENOENT)


I’m trying to play a game on ruby using gosu, and in order to change the name of the game without having to change all variables related to name in my code and other things, I decided to use .json. Here is the script code ruby:

require 'gosu'
require 'json'

data_hash = JSON.parse(File.read('GameSettings.json'))

largura = 360
altura = 360
class GameWindow < Gosu::Window
    def initialize(width , height , fullscreen = false)
        super
        self.caption = data_hash['name']
        @message = Gosu::Image.from_text(self, data_hash['Author'], Gosu.default_font_name,30)
    end
    def draw
       @message.draw(10,10,0)
    end
end

window = GameWindow.new(largura, altura, false)
window.show

and that of Gamesettings.json:

{
    "name"  :   "Heart Afeathered",
    "Author"    :   "Davi Martins Guedes",
    "Nacionality"   : "Brazil"
}

and when I run comes this message that I put in the title.

  • ah, I forgot to tell you, the two work separately, but when I come along comes this error message

1 answer

1


Apparently the file Gamesettings.json is not in the same file folder .Rb. Try to pass the full path to the file, as in the example:

_ jogo (pasta)
    _ config (pasta)
        GameSettings.json (arquivo)
    Jogo.rb (arquivo)

data_hash = JSON.parse(File.read('config/GameSettings.json'))

In addition, the function initialize of the object Gamewindow you won’t be able to read the variable data_hash, because they are in a different scope, therefore it is necessary that data_hash is initialized within the function initialize of Gamewindow.

  • they are in the same folder, I forgot to mention this, Mb, even so, the code that reads the json works normally, but when I match something within the Gamewindow class to one of its data_hash[] the program simply sends this error message.

  • The error that’s in the question up there, is that ruby is not finding the file, but the way you’re talking now, the problem is another. What is happening is that the function initialize of the object Gamewindow, cannot see the variable data_hash, is a matter of scope. What you have to do is add the declaration of data_hash within the function initialize of Gamewindow.

  • I did it and it worked, thank you very much man^^

  • @Davimartins if Pedro’s reply was helpful to you, please mark it as accepted by clicking on the mark in the upper left corner of it - and welcome to sopt! : p

  • Sorry, I almost forgot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.