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.
-
Go to a project folder or create a new one with
gamefic init
. -
Create a directory named
lib
. -
Add a file
lib/gamefic-example.rb
with the following code:
module Example
extend Gamefic::Scriptable
script do
respond :example do |actor|
actor.tell "This command was provided by gamefic-example."
end
end
end
- Make the following changes to
plot.rb
:
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), 'lib')
require 'gamefic'
require 'gamefic-standard'
require 'gamefic-example'
class Mygame::Plot < Gamefic::Plot
include Gamefic::Standard
include Example
script do
introduction do |actor|
actor.tell "This game includes the gamefic-example library."
end
end
end
-
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.