Detecting user inputs

The steps to control a 3D character through a game-controller are summarized below:

  1. Get the character's pointer

  2. Process the message from the game-controller

  3. Interact with the 3D character

Step 1. Get the character's pointer

The first step is to get a pointer to the astronaut from the View Component.

In the snippet below, the class Earth implements the View component of the MVC. Whereas, the class GameLogic implements the Model component of the MVC.

void GameLogic::init(){

    //1. Get a pointer to the Earth object
    Earth *pEarth=dynamic_cast<Earth*>(getGameWorld());

    //2. Search for the Astronaut object
    pAstronaut=dynamic_cast<U4DGameObject*>(pEarth->searchChild("astronaut"));

}

In line 1, we get a pointer to the View component, that is the Earth class. In line 2, we search for the character of interest by providing its name.

Once you have the pointer to the character, you can now process the message from the controller.

Step 2. Process Message

The next step is to process the messages from the Controller Component and determine which button was pressed.

The message from the controller is handled by the receiveUserInput() method, as shown below.

void GameLogic::receiveUserInputUpdate(void *uData){

    //1. Get the user-input message from the structure
    ControllerInputMessage controllerInputMessage=*(ControllerInputMessage*)uData;

    //2. Determine what was pressed, buttons, keys or joystick
    switch (controllerInputMessage.controllerInputType) {

        //3. Did Button B on a mobile or game controller receive an action from the user (Key D on a Mac)
        case actionButtonB:
        {
            //4. If button was pressed
            if (controllerInputMessage.controllerInputData==buttonPressed) {

                //4a. What action to take if button was pressed
                pAstronaut->translateBy(0.0, 1.0, 0.0);

                //5. If button was released
            }else if(controllerInputMessage.controllerInputData==buttonReleased){


            }
        }

        break;

    }       

    //....

Step 3. Interact with the game character

Finally, depending on the button pressed, we interact with the 3D character. For instance, in the snippet above, whenever the user presses button B, the character is translated ( line 4a).

Last updated