Char is a Javascript actor model implementation for the browser. Use this library for game scripting, agent-based simulations, or any other application that needs concurrency.
Inspired by ZZT-OOP.
-
Embedded scripting language - Actors are programmed with Char's built-in language, which is parsed and compiled at runtime. Scripts are compiled into a JSON serializable "bytcode", which will eventually allow loading/saving of the complete program state. Modules can be created to extend the language and wrap external Javascript libraries.
-
Multitasking - Actor scripts are coroutine-like, and so they can be suspended and resumed at will. Any command can be configured to yield execution of the actor. This makes it easy to program sequential behavior over time.
-
Hierarchical actor structure - All actors live in a World. A World contains Boards, which are the base containers for actor instances. Actors can create and manage child actors. Actors communicate by sending messages composed of a target (parent, children, siblings, etc), label name (eg. mailbox), and data.
npm install
npm run build
var world = new Char.World();
var board = new Char.Board();
board.setup(() => {
object('Person', () => {
label('init')
jump('give_treats')
end()
label('give_treats')
loop(5)
send('Dog', 'receive_treat')
wait(1)
endloop()
end()
})
object('Dog', () => {
label('init')
set('num_eaten', 0)
end()
label('receive_treat')
_if('$num_eaten < 3')
set('num_eaten', expr('$num_eaten + 1'))
print("Yum")
_else()
print("No thanks")
_endif()
end()
})
});
board.run(() => {
label('init')
spawn('Dog')
spawn('Person')
end()
});
world.addBoard("Default", board);
world.startBoard("Default");
> Yum
> Yum
> Yum
> No thanks
> No thanks
Defines a labeled script block. Actors can only be in one label at any given time. The built-in "init" label always gets executed once when an actor is first spawned.
Closes a labeled script block
Starts a conditional block. Its inner block will only execute if the expression evaluates to true
Branch of a conditional block
Branch of a conditional block
Closes a conditional block
Starts a loop block. The inner block will repeatedly execute the given number of times
Closes a loop block
Defines/assigns a variable in the actor's local memory
Gets the value of a variable in the actor's local memory
Alias for the get() command
Executes the given function and reflects its return value
Executes the string as a Javascript expression and reflects its value. Any substring prefixed with '$' will evaluate to the value of the matching actor variable.
Immediately moves the actor's execution to the given label block
Tells the actor(s) in the given scope to jump to the given label block
Prevents the actor from receiving any message sent to it
Allows the actor to receive messages again if it was locked
Instantiates a new instance of an actor as a child
Destroys the actor along with its children
Replaces the actor with a new instance of the given actor. Similar to a die() and subsequence spawn()
Destroys all actors in the world and halts execution
Extends the actor's capabilities with a module
Print the given message to the console
Pauses execution of the actor for the given number of cycles