Why does not this work?

0
  

.rb: 2: in 'read': No such file or directory @ rb_sysopen -   GameSettings.json (Errno :: ENOENT)

I'm trying to make a game in ruby using gosu , and to be able to change the name of the game without having to change all the 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 GameSettings.json:

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

And when I run that message I put it in the title.

    
asked by anonymous 04.09.2018 / 03:18

1 answer

1

The GameSettings.json file is apparently not in the same folder as the .rb file. 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 initialize function of the GameWindow object will not be able to read the data_hash variable because it is in a different scopes, so it is necessary that data_hash be initialized within GameWindow initialize function.     

05.09.2018 / 00:10