Please Note: This section is under construction. Please check back regularly for updates.
In order to use the Table Realms functionality within the Unity development engine, you need to do the following:
But First:
Please Note: This tutorial is based off the Roll a Ball tutorial available from Unity. The PlayerController.cs script referred to in the tutorial is from the Roll a Ball tutorial, and adapted to fit Table Realms.
We need to create a script that extends the TableRealms TableRealmsPlayerActionBehavior class to account for the commands issued by our player controller. Create a new C# script in your Assets\Scripts folder called TableRealmsDevice.cs:
Before editing the script, let’s complete the outstanding information for the TableRealms prefab. Open the TableRealms prefab in the Inspector and type the name of the TableRealmsDevice script into the Player Command Script field:
Complete the outstanding information for the Table Realms prefab
base.Start ();
> When this class is instantiated, we need to attach it to the PlayerController script that drives the control of the Player object in a way that identifies this device as the interpreter for player behaviour on the controller.add the following statements to the Start () function:
playerController = GameObject.Find ("Player").GetComponent<PlayerController> ();
playerController.device = this;
Interaction between the controller and the game can be facilitated in one of two ways. * Firing a command from the controller to the game that will then trigger an in-game action * Continuously polling the controller from the game to see if any player actions have been taken
The polling method for thumbsticks and buttons will be discussed first.
To facilitate the polling of the thumbstick, add the following to the script, after the Start() function:
// Joystick behaviour.
public Vector2 PollThumbStick () {
return new Vector2(TableRealmsModel.instance.GetData<float>(gameObject.name + ".ThumbStick.T1.x"), TableRealmsModel.instance.GetData<float>(gameObject.name + ".ThumbStick.T1.y"));
}
When called, this function will return a Vector2 object that contains the horizontal and vertical thumbstick coordinates (as float values) for the ThumbStick.T1 component gameObject as identified by its name, which in this case is the controller object that has been attached to the scene at start-up. The first part of ThumbStick.T1 identifies the type of object that we are polling, and the second part identifies the name of the specific object as specified in the Table Realms IDE:
We could also use polling to track player action through the Jump and Stop buttons, but this is less efficient that firing the action from the controller and affecting the game directly and immediately. Setting up an action method consists of two parts. Firstly, we need to create the action method in our TableRealmsDevice script:
// Jump button behaviour.
public void ActionJump () {
playerController.Jump ();
}
This method will execute the Jump() function of the playerController script that we’ve attached to the Player object.
Secondly, we need to link this method to our button on the controller. Open the Designer and navigate to the Jump button in the tree-view. In the Button’s Script field, add the following:
The SendAction(model.id .. “.ActionJump”) statement will perform the button’s SendAction method to trigger the ActionJump method in the TableRealmsDevice.cs script.
Now that we can start polling the components on the controller to see what the player is doing, we need to update the PlayerController script to interpret and run the results.
Open the PlayerController script and amend the FixedUpdate() method as follows:
void FixedUpdate () {
if (!(device == null)) {
Vector2 stick = device.PollThumbStick ();
moveHorizontal = stick.x;
moveVertical = stick.y;
Move ();
} else {
moveHorizontal = Input.GetAxis ("Horizontal");
moveVertical = Input.GetAxis ("Vertical");
Move ();
if (Input.GetButtonDown ("Jump")) {
Jump ();
}
if (Input.GetButtonDown ("Fire1")) {
Stop ();
}
}
}
You’ll notice the only real changes are that we only execute the previous movement code if there is no device attached to the scene and if there is a device, we poll the thumb-stick to check for movement. You will recall that the Jump and Stop actions are executed directly by the controller through the associated TabeRealmsDevice methods.
Let’s take a closer look at the new code:
Vector2 thumbStickMovement = device.PollThumbStick ();
Here we call the PollThumbStick() method that we wrote in the TableRealmsDevice script. The result is returned to the thumbStickMovement Vector2 object as the x and y movement values.
moveHorizontal = thumbStickMovement.x;
moveVertical = thumbStickMovement.y;
We now assign the x and y values to the moveHorizontal and moveVertical variables so that we can apply movement to our ball through the Move() method.