The Rubygame Book Part 2

From Kibabase

Jump to: navigation, search
Home Beginning Back Next End
Back to Table of Content Jump to Beginning Back Next Jump to End(Part 7)

In this part of the tutorial, we take a look at how one might write the most neccesary part of the program, the code that load other part of the game.

Contents

The require

You are probably familiar with the require method, which are used to load all sort of files and libraries, especially gems.

  require"rubygems"
  require"rubygame"
  require"app/logic/example.rb"
  #And more such statements

While this methodology of loading programs is fine for its purpose, you'll have to spend time editing this part everytime you create a new file. If you have to generate several files at once, the more time you will have to spend editing the loader file. If you deleted a file, you probably have to spent time editing the loader file as well.

Require on a Directory Scale

But what if you can requires all of a content in a given directory?

It would save you a whole lot of time since the only time you probably need to modify the loading code is to specify a new directory.

However, this solution requires some thought in order to be viable. One such example is that it must understand to require a class that was to be inherited first before the class that want to inherit it or otherwise you'll get an error.

 Demonstration of an error using manual require:
 file: aclass.rb
    class AClass < FirstClass
      def initialize
      end
    end
 file: firstclass.rb
    class FirstClass
      def initialize
      end
    end
 file: run_require.rb
    require"aclass.rb"
    require"firstclass.rb"
 ruby run_require.rb will yield:
 
 ./aclass.rb:1: uninitialized constant FirstClass (NameError)   
       from run_require.rb:1:in `require'                                                        
       from run_require.rb:1                                                                     

A way to solve this problem is to write code that automatically detect the most neccessary file and push it to the front of a list for loading files. For that, you will need a naming schema for your files to tell which one is the most improtant file to load. For our purpose,

For example, when a class that need to be inherited by all other classes, you would add a _level_1 to its name before the extension ending. So let say, mode.rb will become mode_level_1. This way, the loader system load the file with level_1 first. You could concevieably specify lower prority to ensure certain files will get loaded next before other files. Of course, if a filename have no prority attached, it will be loaded after the file loader system exhaust all prority files in the list.

Before We Get Coding the Real Thing

Although we will create a directory loader system, it is not so much neccesary for the small number of files and dependencies, of which we are rarely called on to modify over the course of the project.

All these code, including the eventual directories that will be loaded via the directory loader system will be in config/environment.rb.

file: config/environment.rb

  require"rubygems"
  require"rubygame"
  require"yaml"
  include Rubygame
  TTF.setup()

Consideration of Location for the Loading System

It is probable that the loading system will prove useful no matter which projects you build on and more so as it get extended. As where the environment.rb, with the exception of some very basic code, will be made very specific to a project's need over time.

Thus, the loading system belongs to the lib directory. Create a directory in lib, called dependencity. This is where we will write the loading system.

Conclusion

Now that the theory is finished, we can move on to writing the actual loader system in part 3.

Personal tools