Game Development Reference
In-Depth Information
}
else
{
m_autocompleteText.clear();
}
}
We store the result of a query to the trie in m_currentResult using the text that the user
has typed into the text box m_text. If there is a valid result, we will clear the auto comple-
tion text and attempt to get a new result using get_from_prefix passing in the next child of
m_currentResult, each call to AutoComplete will cycle the next available entry in the trie
that matches the prefix, unless the reset parameter is true, we use this when we know that
the prefix has changed, for example when backspace is pressed.
To wrap up the runtime variable database, we need to implement the function that registers
runtime variables, it needs to add the name of the variable into the trie, and add keep a
pointer to it. However, it is not enough to keep a pointer to it, should the variable be re-
leased(anditwillduringthegame'sshutdown)wewouldhaveproblems.Mostofthetime
our runtime variables will be static objects, and here we are using shared_ptrs to them, this
means that if at some point during execution the runtime variables are released, the under-
lying object will be destroyed, and we would be hanging on to an invalid pointer.
Fortunately, std::shared_ptr allows us to specify a callback function to be invoked should
theunderlyingobjectbedestroyed,wetakeadvantageofthistoprovideafunctionthatwill
unregister the pointer from the database and prevent any catastrophes.
void runtime_variable_database::Register(runtime_variable* variable)
{
m_trie->add(variable->Name());
m_variables.push_back(std::shared_ptr<runtime_variable>(variable, OnRuntimeVariableDeletion));
}
void runtime_variable_database::OnRuntimeVariableDeletion(runtime_variable* variable)
{
variable->GetDatabase().Unregister(variable);
}
Text Entry
There does not need to be any custom text entry done for the console, we just need to add
a ui::textbox control to the console class and let it do its magic. As mentioned before, the
important requirement is that when constructing the textbox control we provide it with the
trie that we have created in the runtime variable database.
m_textBox = std::unique_ptr<textbox>(new textbox(m_core, m_font, m_spriteBatch));
Search WWH ::




Custom Search