GeoGen API Reference
Setting script parameters

This tutorial demonstrates how to access and set any parameters declared by the script.

Following code sets map dimensions and a single map parameter:

string code = "\n\
metadata \n\
{ \n\
ExampleParameter: {Type: Number, Restriction: Integers } \n\
}; \n\
yield HeightMap.Flat(); \n\
";
compiler::Compiler compiler;
runtime::CompiledScript* compiledScript = compiler.CompileScript(code);
// Create a ScriptParameters object from the script metadata.
runtime::ScriptParameters parameters = compiledScript->CreateScriptParameters();
// The parameters object can now be customized in any way (commonly it would be iterated over and rendered onto a GUI. The values from the GUI would then be set back into the object).
// In this example, we will only adjust render size and set value of the single parameter.
parameters.SetRenderHeight(2000);
parameters.SetRenderHeight(1000);
// Set the value of the parameter (we are assuming that the parameter exists and that it is of type Number).
dynamic_cast<runtime::NumberScriptParameter*>(parameters.GetItem("ExampleParameter"))->SetValue(333);
// Now pass the adjusted parameters to the VM.
runtime::VirtualMachine vm(*compiledScript, parameters);
vm.Run();
renderer::Renderer renderer(vm.GetRenderingSequence());
renderer.Run();
genlib::HeightMap* heightMap = renderer.GetRenderedMapTable().GetItem(renderer::Renderer::MAP_NAME_MAIN);
cout << "I have a height map with width " << heightMap->GetWidth() << " and height " << heightMap << " with data at 0x" << heightMap->GetHeightDataPtr() << "." << std::endl;

Full code

Each parameter can be of one of three types: runtime::BooleanScriptParameter, runtime::NumberScriptParameter and runtime::EnumScriptParameter. The actual type can be determined by calling GetType on the base class.

Next code sample is a bit more complex, iterating over all available script parameters and setting their value interactively using standard input.

runtime::ScriptParameters parameters = compiledScript->CreateScriptParameters();
// Iterate over all available parameters.
for (runtime::ScriptParameters::iterator it = parameters.Begin(); it != parameters.End(); it++)
{
// For each item, a pair of parameter name and parameter object is returned.
// Each type needs different processing.
switch (it->second->GetType())
{
{
runtime::BooleanScriptParameter* booleanParameter = dynamic_cast<runtime::BooleanScriptParameter*>(it->second);
cout << it->first << " (0/1): ";
bool booleanValue;
cin >> booleanValue;
cout << endl;
booleanParameter->SetValue(booleanValue);
break;
}
{
runtime::NumberScriptParameter* numberParameter = dynamic_cast<runtime::NumberScriptParameter*>(it->second);
cout << it->first << " (" << numberParameter->GetMin() << " - " << numberParameter->GetMax() << "): ";
double numberValue;
cin >> numberValue;
cout << endl;
numberParameter->SetValue(numberValue);
break;
}
case runtime::SCRIPT_PARAMETER_TYPE_ENUM:
{
runtime::EnumScriptParameter* enumParameter = dynamic_cast<runtime::EnumScriptParameter*>(it->second);
cout << it->first << " (" << endl;
// Print list of possible values. The list of possible string values an enum can hold can be obtained from its enum type.
for (
corelib::EnumTypeDefinition::ValueDefinitions::const_iterator it2 = enumParameter->GetEnumType()->GetValueDefinitions().begin();
it2 != enumParameter->GetEnumType()->GetValueDefinitions().end();
it2++
)
{
// it2 is a pair of a string value and the corresponding integer value.
cout << it->first << ", ";
}
cout << "): ";
string stringValue;
cin >> stringValue;
enumParameter->SetValue(stringValue);
cout << endl;
break;
}
}
}
// Now pass the adjusted parameters to the VM.
runtime::VirtualMachine vm(*compiledScript, parameters);

Full code

The SetValue method of each of the parameter types performs input validations and attempts to make the value valid if possible (such as number value greater than the maximum will be reduced to the maximum). It that attempt fails, default value is used.