Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

rathersilly

1
Posts
1
Topics
A member registered Apr 09, 2021

Recent community posts

Hey, I found a strange bug and also a workaround.

If I define a constant (class or variable) in another file and require that file in main.rb, accessing that constant will throw an uninitialized constant error, UNLESS one of the following:

  • the constant is accessed in a method (like tick)
  • the constant is forward declared ( (suspiciously) as though it was C - see the Init module below)

Hopefully the code included explains what I mean:

####### main.rb #######
require '/app/init.rb'
module Init          # if Init is forward declared, it works as otherwise expected
end
class Game
  include Init       # this throws error unless the forward declaration exists
end
$game = Game.new
puts Color           # error: uninitialized constant
def tick
  puts Color         # works fine
  $game.fun          # works fine, as long as Init is forward declared.
end
###### init.rb #######
Color = [100,100,100]
module Init
  def fun
    puts "sup"
  end
end

I'm a total noob so forgive me if I missed something, but hopefully this helps someone else who was baffled by this behavior.