Extending in-game programming in Unity

After creating a basic program or script interpreter in the postCreating in-game programmed components in Unity” and handling simple movement and transport instructions, I’m extending the features to include arithmetic operations and conditional commands. The system previously had input, output, put and get (storage) commands, with the additional commands, we’ll end up with an in-game implementation of the von Neumann architecture — a common topic in computer science and engineering classes and a plot point in book The Three Body Problem.

I added the following commands:

  • add [x] – adds the current carried value to the stored value in storage “x”
  • sub [x] – subtracts the current carried value from the stored value in storage “x”
  • jmp [x] – jumps to the command in line “x” of the program
  • jlt [x] – jumps to the command in line “x” of the program if the carried value is less than zero
  • jgt [x] – jumps to the command in line “x” of the program if the carried value is greater than zero
  • jeq [x] – jumps to the command in line “x” of the program if the carried value is equal zero

The system previously supported:

  • input – retrieves a value from the input list, the list values are pre-defined
  • output – sends the carried value to output
  • put [x] – puts the current carried value to storage “x”
  • get [x] – puts the current carried value from storage “x”

In the sample game scene, we have the “input” defined by the green box,
“output” with the blue box and the storage as the 3 gray boxes designed 0, 1, 2 from the left.

Extending-1-Scene

The following user controls are available

  • RUN – executes the current program
  • STOP – stops program execution
  • LOAD – show the current stored program
  • STORE – store the new program from the text area to execute
  • RESET – reset / reload the scene

Extending-2-Controls

Implementing a program or script interpreter is straightforward, the challenge with this application is managing the interactions and animations without unnecessarily complicating the code.

Each game component in the scene have specific tasks to perform, the arithmetic operation is handled by the storage script while the conditional jumps are managed in the player script being the main actor in the scene. An interesting capability is that each player or actor has its own script and this scene can be further extended with multiple player objects performing tasks — more on that in later posts.

Arithmetic Processing Snippet

IEnumerator OperationCoroutine(Transform operandTransform, Vector3 destination, int opSign)
{
	yield return StartCoroutine(MoveCoroutine(operandTransform, destination));

	otherObjTransform.GetComponent().Value +=
		operandTransform.GetComponent().Value * opSign;
	Destroy(operandObjTransform.gameObject);
	yield return null;
}

Conditional Processing Snippet

case "jmp":
	CurrentCommandIndex = int.Parse(command[1]);
	CurrentState = State.Ready;
	break;
case "jeq":
	if (childCount > 0 && childValue == 0)	
		CurrentCommandIndex = int.Parse(command[1]);                    	
	else	
		CurrentCommandIndex++;	
	CurrentState = State.Ready;
	break;
case "jlt":
	if (childCount > 0 && childValue < 0)	
		CurrentCommandIndex = int.Parse(command[1]);
	else
		CurrentCommandIndex++;
	CurrentState = State.Ready;
	break;
case "jgt":
	if (childCount > 0 && childValue > 0)
		CurrentCommandIndex = int.Parse(command[1]);	
	else	
		CurrentCommandIndex++;
	CurrentState = State.Ready;
	break;

An interactive WebGL demo can be viewed at:

http://orbitalfoundry.com/WebGL/Program2/index.html

Extending-3-Demo

For questions, comments or contact – follow/message me on Twitter @rlozada