Behavior trees & The C# programming language for Java programmers CS 185C Behavior trees & The C# programming language for Java programmers
In this day in Video Games...
State-Based Scripting – Behavior Trees Allows ”choose the highest priority of the following states” Allows ”do the following states in order” Can execute multiple states at a time Each state knows if it is relevant or not Can sequence states
State-Based Scripting – Behavior Trees Cutman
State-Based Scripting – Behavior Trees Each node can be successful, failed, or in progress Different node types (shapes) have different rules
State-Based Scripting – Behavior Trees Harder to write Easier to understand and modify Easy to change different node priorities Easy to break a node up into two steps
State-Based Scripting – Behavior Trees Same Batman boss characters
Questions?
Unity3D Scripting UnityScript JavaScript-inspired JavaScript is commonly used for web page scripting Internally, everything is using .NET via Mono
Unity3D Scripting C# Most popular .NET language Static typed, object oriented ”Microsoft Java” Microsoft has added a lot of nifty new syntax, but Unity's version lags behind
C# Example using UnityEngine; using System.Collections; public class BackAndForthClass : MonoBehaviour { public float speed = 6; // Update is called once per frame void Update() { if( transform.position.x > 40 || transform.position.x < -40 ) { transform.Rotate( 0, 180, 0 ); transform.Translate( -10, 0, 0 ); speed = Random.Range( 6f, 10f ); } transform.Translate( -speed * Time.deltaTime, 0, 0 );
C# Overview Almost exactly the same as Java Classes Methods Members Public vs Private Comments Primitive Types Branching, Loops Interface
C# - Classes Represent a ”behavior” or concept In Unity most classes inherit from MonoBehavior public class CLASSNAME : PARENT { public float MEMBER = 6; int ANOTHER_MEMBER = 0; void METHOD() { // code would go here } public float OTHER_METHOD(float PARAM1, float PARAM2) {
C# - Comments Note for humans, ignored by the computer // A single line comment /* A different single line comment */ /* A mulitple line comment */
C# - Types bool true or false (boolean in Java) int number (1, 2, 3) float numer with a decimal point (2.5, 3.14) string text (String in Java) object an unknown thing (Object in Java) Array a collection of objects Vector3 3D position Quaternion 3D rotation
C# - Branching, Loops Do things conditionally, or multiple times if( CONDITION ) { // Stuff only run if condition is true } else if( CONDITION2 ) { // Stuff only run if condition2 is true, condition is false } else { // Stuff run if condition and condition2 are both false } while( CONDITION ) { // Stuff run until condition becomes false for( INIT; CONDITION; INCREMENT ) { // Stuff run until condition becomes false, useful for arrays
C# - Branching, Loops Do things conditionally, or multiple times if( CONDITION ) { // Stuff only run if condition is true } else if( CONDITION2 ) { // Stuff only run if condition2 is true, condition is false } else { // Stuff run if condition and condition2 are both false } while( CONDITION ) { // Stuff run until condition becomes false for( INIT; CONDITION; INCREMENT ) { // Stuff run until condition becomes false, useful for arrays
C# - Event Handling Events in Unity are just method calls Important events: Update FixedUpdate OnTriggerEnter OnCollisionEnter Many other ”OnX”
C# Example using UnityEngine; using System.Collections; public class BackAndForthClass : MonoBehaviour { public float speed = 6; // Update is called once per frame void Update() { if( transform.position.x > 40 || transform.position.x < -40 ) { transform.Rotate( 0, 180, 0 ); transform.Translate( -10, 0, 0 ); speed = Random.Range( 6f, 10f ); } transform.Translate( -speed * Time.deltaTime, 0, 0 );
C# Example using UnityEngine; using System.Collections; public class BackAndForthClass : MonoBehaviour { public float speed = 6; void Update() { transform.Translate( -speed * Time.deltaTime, 0, 0 ); } void OnTriggerEnter( Collider c ) { transform.Rotate( 0, 180, 0 ); transform.Translate( -10, 0, 0 ); speed = Random.Range( 6f, 10f );
Questions?
C# - struct Looks and acts just like a class, EXCEPT when passed to a function, it is copied Allocated on the stack as opposed to the heap public struct Vector3 { public float x; public float y; public float z; // helper methods public void Normalize() { // code goes here }
C# - Operator Overloading Lets you use math symbols (+, -, *, /) instead of calling methods. Vector3 operator+ operator- operator* operator/ operator== operator!=
C# - Properties Make things look like fields but behave like methods. public float Length { get { return Mathf.Sqrt( x*x + y*y + z*z ); } set { float old = Mathf.Sqrt( x*x + y*y + z*z ); float scale = value / old; x *= scale; y *= scale; z *= scale; }
C# - goto C# supports goto. Goes to a specific named label. public object findAndProcessDuplicate(object[] a) { for( int i = 0; i < a.Length; i++ ) { for( int j = i + 1; j < a.Length; j++ ) { if( a[i] == a[j] ) { Process(a[i]); goto done; } done: afterProcessing();
Questions?
Let's look at a real example. C# Let's look at a real example.