Using Character States to Handle Data
by Gamefic on April 1, 2017
Most of my recent Gamefic work has concentrated on the Web platform, i.e., games designed to run in browsers. I like the potential for creative interfaces and wide distribution. Since browsers provide the capability to send and receive structured data, updating the client with basic HTML seems like an unnecessary limitation. To that end, I've added a new feature: character state.
The most common way of sending text to the client is through tells. This example says hello to the player when the game starts:
introduction do |player|
player.tell "Hello!"
end
Game engines read the output from a buffer and display it to the user. That's fine for traditional text games, but what if we want to provide extra data for the client to use in some other manner? One example is displaying the player's health as a percentage in a progress bar. My original solution was to add the data to the text in special tags and parse it out in the client. Character states provide a simpler and more robust solution.
Instead of reading plain text from a buffer, the engine retrieves a JSON object containing any properties that the game's author chooses to provide. The default properties are output, prompt, and scene. Output is the text that a basic engine would read from the user's buffer. Prompt is the text to display when requesting user input (e.g., the ubiquitous > we all know and love from Inform games). Scene is the type of scene the player's character is currently performing: Active, MultipleChoice, etc.
Authors can add more properties by extending the Character#state method. The recommended way to extend it is with a module:
module Score
def score
@score ||= 0
end
def state
super.merge(score: score)
end
end
class Gamefic::Character
include Score
end
The browser engine receives an object with all of the state data on every turn. Here's an example of how a custom HTML interface might use it:
Gamefic.handleResponse('Active', function(response) {
$('#output').append(response.output);
$('#score').html('Your score is ' + response.score);
});
This feature opens up a wide range of new possibilities for games. One idea I'm exploring is providing clients with geometric data for dynamic graphical representations of the game world.