### Home Classes Methods

### In Files

- lib/rubygame/named_resource.rb

### Namespace

- MODULE Rubygame::NamedResource::NamedResourceClassMethods

### Methods

- #name
- #name=

### Files

- CREDITS
- LICENSE
- NEWS
- README
- ROADMAP
- custom_sdl_load_paths.rdoc
- getting_started.rdoc
- keyboard_symbols.rdoc
- macosx_install.rdoc
- managing_framerate.rdoc
- surface_palettes.rdoc
- windows_install.rdoc

### Class/Module Index

Quicksearch

- Rubygame
- Rubygame::ActiveEvent
- Rubygame::Clock
- Rubygame::Color
- Rubygame::Color::ColorBase
- Rubygame::Color::ColorHSL
- Rubygame::Color::ColorHSV
- Rubygame::Color::ColorRGB
- Rubygame::Color::Palette
- Rubygame::Event
- Rubygame::EventActions
- Rubygame::EventActions::BlockAction
- Rubygame::EventActions::MethodAction
- Rubygame::EventActions::MultiAction
- Rubygame::EventHandler
- Rubygame::EventHandler::HasEventHandler
- Rubygame::EventHook
- Rubygame::EventQueue
- Rubygame::EventTriggers
- Rubygame::EventTriggers::AndTrigger
- Rubygame::EventTriggers::AttrTrigger
- Rubygame::EventTriggers::BlockTrigger
- Rubygame::EventTriggers::InstanceOfTrigger
- Rubygame::EventTriggers::KeyPressTrigger
- Rubygame::EventTriggers::KeyReleaseTrigger
- Rubygame::EventTriggers::KindOfTrigger
- Rubygame::EventTriggers::MouseMoveTrigger
- Rubygame::EventTriggers::MousePressTrigger
- Rubygame::EventTriggers::MouseReleaseTrigger
- Rubygame::EventTriggers::OrTrigger
- Rubygame::EventTriggers::TickTrigger
- Rubygame::EventTriggers::YesTrigger
- Rubygame::Events
- Rubygame::Events::ClockTicked
- Rubygame::Events::InputFocusGained
- Rubygame::Events::InputFocusLost
- Rubygame::Events::JoystickAxisMoved
- Rubygame::Events::JoystickBallMoved
- Rubygame::Events::JoystickButtonEvent
- Rubygame::Events::JoystickButtonPressed
- Rubygame::Events::JoystickButtonReleased
- Rubygame::Events::JoystickHatMoved
- Rubygame::Events::KeyPressed
- Rubygame::Events::KeyReleased
- Rubygame::Events::KeyboardEvent
- Rubygame::Events::MouseButtonEvent
- Rubygame::Events::MouseFocusGained
- Rubygame::Events::MouseFocusLost
- Rubygame::Events::MouseMoved
- Rubygame::Events::MousePressed
- Rubygame::Events::MouseReleased
- Rubygame::Events::QuitRequested
- Rubygame::Events::WindowExposed
- Rubygame::Events::WindowMinimized
- Rubygame::Events::WindowResized
- Rubygame::Events::WindowUnminimized
- Rubygame::ExposeEvent
- Rubygame::Ftor
- Rubygame::GL
- Rubygame::Hotspot
- Rubygame::JoyAxisEvent
- Rubygame::JoyBallEvent
- Rubygame::JoyDownEvent
- Rubygame::JoyHatEvent
- Rubygame::JoyUpEvent
- Rubygame::Joystick
- Rubygame::Key
- Rubygame::KeyDownEvent
- Rubygame::KeyUpEvent
- Rubygame::MailQueue
- Rubygame::MediaBag
- Rubygame::Mixer
- Rubygame::Mixer::Music
- Rubygame::Mixer::Sample
- Rubygame::Mouse
- Rubygame::MouseDownEvent
- Rubygame::MouseMotionEvent
- Rubygame::MouseUpEvent
- Rubygame::Music
- Rubygame::NamedResource
- Rubygame::NamedResource::NamedResourceClassMethods
- Rubygame::QuitEvent
- Rubygame::Rect
- Rubygame::ResizeEvent
- Rubygame::SDLError
- Rubygame::SFont
- Rubygame::Screen
- Rubygame::Sound
- Rubygame::Sprites
- Rubygame::Sprites::DepthSortGroup
- Rubygame::Sprites::Group
- Rubygame::Sprites::LimitGroup
- Rubygame::Sprites::Sprite
- Rubygame::Sprites::UpdateGroup
- Rubygame::Surface
- Rubygame::TTF

No matching classes.

# Rubygame::NamedResource

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:

1. Do 'include Rubygame::NamedResource' in your class definition.

2. Set MyClass.autoload_dirs to an Array of directories to look for files when autoloading. Tip: use File.join to create paths that work on any operating system.

3. Define autoload to implement the behavior for your class, or leave it as the default if you don't need autoloading. See the docs for autoload for more information.

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

### Public Instance Methods

name() click to toggle source

Returns the instance's @name. See also name=.

    # File lib/rubygame/named_resource.rb, line 222
    def name
      @name
    end

name=( new_name ) click to toggle source

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.

    # File lib/rubygame/named_resource.rb, line 238
    def name=( new_name )
      if new_name.nil?
        return @name = nil
      end

      unless new_name.kind_of? String
        raise TypeError, "name must be a String (got #{new_name.class})"
      end

      @name = new_name.dup
      @name.freeze
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.