Overriding the game input

At this point, we have a complete testing system in place; we just need to complete the last parts of the injection. This bit of surgery can require a keen eye and a little digging through code. Fortunately, there are some good, clear indicators that you can use to spot places for injection. Open the editor and follow the next steps to complete the injection:

  1. Select the Control object in the Hierarchy window.
  2. Locate the MS Scene Controller Free component in the Inspector window and use the Context menu to open the script in your code editor.
  3. Locate the following block of code, around line 286 (about halfway in), as follows:
case ControlTypeFree.windows:
verticalInput = Input.GetAxis (_verticalInput);
horizontalInput = Input.GetAxis (_horizontalInput);
mouseXInput = Input.GetAxis (_mouseXInput);
mouseYInput = Input.GetAxis (_mouseYInput);
mouseScrollWheelInput = Input.GetAxis (_mouseScrollWheelInput);
break;
}
  1. This is where the game is querying the GetAxis method, in order to return the values of the respective input axis. As we have discussed, we are only interested in the vertical and horizontal axes for this example. You can, of course, override other axes, as you see fit.
  2. Modify the lines where the verticalInput and horizontalInput are being set, as follows:
verticalInput = TestingInput.Instance.getAxis(_verticalInput);
horizontalInput = TestingInput.Instance.getAxis(_horizontalInput);
  1. Notice that we call TestingInput.Instance, in order to access the singleton instance of our class. This allows us to query that class for the current input values. The TestingInput object can now be the source of truth (as far as this class is concerned), with respect to the input.
  1. Previously, we quickly went over the agent code that sets the input, but here it is again for reference:
public void MoveAgent(float[] act)
{
for(int i=0;i<act.Length;i++)
{
var val = Mathf.Clamp(act[i], -1f, 1f);
TestingInput.Instance.setAxis(val,axisAction[i]);
}
}

 

  1. Notice the highlighted line in the TestingAgent MoveAgent method. This is where we override the input by the agent and inject the values back into the game.
  2. Save the code and return to the editor. Make sure to fix any compiler issues now.

Unfortunately, we are still unable to run the scene, as we have one last configuration step to tend to. In the next section, we will complete the configuration by setting up the brains.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset