Game Development Reference
In-Depth Information
With all the helper functions defined, we can now actually complete the initialization of
the CommandBar class. To do this, add the following function:
void InitCommandButtons()
{
commandButtons = new CommandButton[(int)buttonRows *
(int)buttonColumns];
for (int i = 0; i < commandButtons.Length; i++)
{
var newButton = CreateButton();
if (i < GameState.currentPlayer.Inventory.Count)
{
newButton.AddInventoryItem(GameState.currentPlayer.Inventory[i]);
}
commandButtons[i] = newButton;
}
InitButtonPositions();
}
Here, we simply set up the button array based on the configured rows and columns, and
then added a new empty command bar button to each element using the preceding
CreateButton function. Once all the buttons have been created, we then use the In-
itButtonPositions function to position them correctly within the command bar.
Then, if the player has any weapons in their inventory, it will add that item to the button
using a simple helper function (which we'll add in the next section).
All that is left is to ensure that the command bar is positioned correctly on the screen ac-
cording to the anchor point. To do this, we first need another helper function to set the po-
sition:
void SetPosition(float x, float y)
{
Search WWH ::




Custom Search