| Module | Rubygame::NamedResource |
| In: |
lib/rubygame/named_resource.rb
|
NamedResource is a mix-in module to implement a globally-available resource table, a @name variable and accessors, and a system for automatically loading resources when they are first needed.
This module is used for Rubygame::Music, Rubygame::Sound, and Rubygame::Surface. You can use it in your own classes this way:
Here‘s an example of how you could use this for a class which loads maps from a file:
class Map
include Rubygame::NamedResource
Map.autoload_dirs = [ File.join("maps","world_1"),
File.join("maps","custom") ]
def autoload( name )
# Searches autoload_dirs for the file
path = find_file( name )
if( path )
return load_map( path )
else
return nil
end
end
def load_map( path )
# Your code to do the real loading, then return
# the created instance of Map class.
# ...
return map_instance
end
end
Here‘s an example of how you could then use the Map class:
map = Map["level_1.map"]
if( map )
start_playing( map )
else
raise "Oops! The map file for Level 1 doesn't exist!"
end
Sets the instance‘s @name to the given String, or nil to unset the name. See also name.
NOTE: This does not automatically store the instance in the class resource table by name. Use the #[]= class method to do that.
The string is dup‘ed and frozen before being stored.
May raise: TypeError, if new_name is not a String or nil.