Game Development Reference
In-Depth Information
Aswithallruntimevariablesthefirstparameteristhenameofthefunction,thesecondone
is a description, the difference is the value, in this case we provide a lambda that uses the
functionsignaturerequiredforall runtime_variable_function objects,andinternallyitcalls
the console's Clear() function which does the actual work of clearing it.
At this point, if the player were to bring down the game console and type the text “clear”
and press enter, the lambda we provided in the constructor would be called and the console
will be cleared.
It is also possible to bind a runtime_variable_function to a class' member function, we can
do this using std::bind .
// .h
runtime_variable_function m_memberFunction;
bool OnMemberFunction(std::vector<std::wstring> parameters);
// .cpp
ctor()
: m_memberFunction(L"test", L"function to test parsing arguments", std::bind(&example_game:: OnMemberFunction, this,
std::placeholders::_1))
History
It is very useful to allow users to recall their last typed commands without requiring them
to type them out again, it allows for quick iterations while tweaking the value of a runtime
variable and also can be useful to correct mistakes we may have made while typing the
name ofthevariable. Wecanimplement ahistoryfunction that allows ustocycle backand
forth between anything that has been typed. We will keep a list of strings, and anytime a
new string is typed, we will push the text to the front of this list, this way, whatever is at
the front of the list will be the most recently typed text, while the back of the list will have
the oldest text entered. Having a list we can also keep an iterator from this list, if the user
presses the up key, we will decrement the iterator, grab the text from the history at this po-
sition and insert it into the console's text box.
Recall that ui::textbox has an event handler that we can access with OnTextEntered , the
first part of this system is to provide a function to call when this event happens, and we do
this during the initialization of the text box.
m_textBox->OnTextEntered() += [this](void*, const std::wstring& text) { Execute(text); };
For simplicity we provide a lambda function that captures this object to allow us to call the
Execute function directly within. The Execute function will attempt to execute the runtime
Search WWH ::




Custom Search