Skip to main content

Using addons/redscribe/mrblib

In the previous chapter, we built a DSL from scratch. However you don't always need to start from zero. ReDScribe provides a set of general-purpose components in advance, located in the addons/redscribe/mrblib folder.

Here are some reprentative examples.

coroutine

This DSL provides a simple interface for working with Fiber.

  • ___? is a shorthand alias for Fiber.yield.
  • ___ retrives the value passed when the Fiber is resumed.
require 'addons/redscribe/mrblib/coroutine'

coroutine 'Foo' do
while ___?
emit! :given, ___
end
emit! :finished, true
end
  • To start a Fiber, call the start method.
  • To resume a Fiber, use the resume method.
start :all

resume 'Foo'
resume 'Foo', 'bar'
resume 'Foo', false
Output
=> true

[ given ] signal emitted: true
[ given ] signal emitted: "bar"
[ finished ] signal emitted: true

For implementation details, see addons/redscribe/mrblib/coroutine.rb.

By using coroutine.rb, you can build interactive structures such as diaglogue systems.

See: https://github.com/tkmfujise/ReDScribe/blob/main/doc/examples/4_coroutine.md

actor

This DSL allows you to define actors.

  • Use --> {} to describe the default behavior the actor performs when ticked.
  • Use :key --> {} to define how the actor reacts when receiving a specific message (:key).
require 'addons/redscribe/mrblib/actor'

actor 'Counter' do
@count = 0
--> { @count += 1 }
:reset --> { @count = 0 }
end
  • To invoke an actor's behavior, call the tick method.
  • To send a message to an actor, use the tell method.
tick
tick
tick

tell 'Counter', :reset
tick
Output
[ Counter ] signal emitted: { &"name": "Counter", &"count": 1 }
[ Counter ] signal emitted: { &"name": "Counter", &"count": 2 }
[ Counter ] signal emitted: { &"name": "Counter", &"count": 3 }

[ Counter ] signal emitted: { &"name": "Counter", &"count": 1 }

For implementation details, see addons/redscribe/mrblib/actor.rb.

By using actor.rb, you can build interactive systems as mini-games with race mechanics.

See: https://github.com/tkmfujise/ReDScribe/blob/main/doc/examples/3_actor.md

resource

This DSL allows you to define resources.

  • Use resource :key to define the schema for a single resource.
  • Use resources :key => :keys to define the schema for multiple resources.
require 'addons/redscribe/mrblib/resource'

resource :character do
resources :skill => :skills
end
  • To emit a signal, invoke a method with the same name as the schema, passing a block to describe its contents.
character 'Alice' do
job :magician

skill 'Fire' do
damage 2
end

skill 'Care' do
restore 3
end
end
Output
[ character ] signal emitted: {
&"job": &"magician",
&"name": "Alice",
&"skills":
{ &"damage": 2, &"name": "Fire" },
{ &"restore": 3, &"name": "Care" }
]
}

For implementation details, see addons/redscribe/mrblib/resource.rb.

By using resource.rb, you can generate Godot resource files directly from your DSL definitions.

See: https://github.com/tkmfujise/ReDScribe/blob/main/doc/examples/2_resource_generator.md

Others

The addons/redscribe/mrblib folder also includes additional components such as:

  • math
  • shell

For more details, see: https://github.com/tkmfujise/ReDScribe/blob/main/doc/addons/mrblib.md

In the next chapter, we'll use resource.rb to build a program that generates Godot resource files.