Gamefic Libraries

The Gamefic SDK includes mechanisms for using and distributing shared script libraries. The easiest way to share a library is to publish it as a gem.

A common example of a shared library is gamefic-standard, which is included with the SDK.

How Libraries Work

A Gamefic library, at its core, is just a gem. It contributes to games by adding its own classes, modules, and/or Gamefic scripts.

Libraries published as gems should follow the naming convention gamefic-[name].

Authors import libraries with the require method. The main.rb script in a new game includes gamefic-standard by default:

require 'gamefic'
require 'gamefic-standard'

Additional libraries can be included by installing their gems and requiring them.

A Simple Example

You can experiment with libraries by creating one inside a game project.

  1. Go to a project folder or create a new one with gamefic init.

  2. Create a directory named lib.

  3. Add a file lib/gamefic-example.rb with the following code:

Gamefic.script do
  respond :example do |actor|
    actor.tell "This command was provided by gamefic-example."
  1. Make the following changes to main.rb:
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
require 'gamefic'
require 'gamefic-standard'
require 'gamefic-example'

Gamefic.script do
  introduction do |actor|
    actor.tell "This game includes the gamefic-example library."
  1. Run the game with rake ruby:run. If you enter EXAMPLE at the command prompt, you should get the message:

    This command was provided by gamefic-example.

Creating a Library Gem

The gamefic init command has a --gem option to create a project for a library gem:

$ gamefic init --gem gamefic-example

By default, the gemspec includes gamefic-standard as a dependency. If you don’t want to require the standard library, you can safely remove it.

Distributing the Library

The simplest way to share the library with other authors is to publish it as a gem. See the RubyGems guide for more information.