GeoGen API Reference
Handling text messages from scripts

This tutorial demonstrates how to handle text messages produced by the map scripts (using the Print function).

Text messages are handled using a callback of type runtime::VirtualMachine::ScriptMessageHandler. A simple implementation, which just prints the message to standard output:

void MyMessageHandler(runtime::VirtualMachine* virtualMachine, CodeLocation location, string const& formattedMessage, string const& unformattedMessage, vector<string> arguments)
{
cout << "SCRIPT MESSAGE: " << formattedMessage << std::endl;
};

Full code

The handler is then assigned to a runtime::VirtualMachine using runtime::VirtualMachine::SetScriptMessageHandler:

string code = "Print(\"Lorem ipsum {0}\", 5);yield HeightMap.Flat(1 / 0);";
compiler::Compiler compiler;
runtime::CompiledScript* compiledScript = compiledScript = compiler.CompileScript(code);
runtime::VirtualMachine vm(*compiledScript);
// Before executing any code, assign the text message handler.
vm.SetScriptMessageHandler(MyMessageHandler);
vm.Run();
renderer::Renderer renderer(vm.GetRenderingSequence());
renderer.Run();
genlib::HeightMap* heightMap = renderer.GetRenderedMapTable().GetItem(renderer::Renderer::MAP_NAME_MAIN);
cout << "Finished!" << endl;

Full code

If no custom text message handler is specified, runtime::VirtualMachine::DefaultScriptMessageHandler, which prints the message on standard output, is used. A runtime::VirtualMachine::EmptyScriptMessageHandler, which discards the message instead, is also provided.

In addition to the ready-to-print formatted text, the unformatted text with the collection of formatting arguments is also provided. This can for example be used to replace the unformatted message with its localized variant and then splice the formatting arguments into it using FormatString.

If you need to pass any data to the callback, you can assign any data pointer to the VM using runtime::VirtualMachine::SetCallbackData and then retrieve it in the callback using runtime::VirtualMachine::GetCallbackData.