Presentation is loading. Please wait.

Presentation is loading. Please wait.

Behavior trees & The C# programming language for Java programmers

Similar presentations


Presentation on theme: "Behavior trees & The C# programming language for Java programmers"— Presentation transcript:

1 Behavior trees & The C# programming language for Java programmers
CS 185C Behavior trees & The C# programming language for Java programmers

2 In this day in Video Games...

3 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

4 State-Based Scripting – Behavior Trees
Cutman

5 State-Based Scripting – Behavior Trees
Each node can be successful, failed, or in progress Different node types (shapes) have different rules

6 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

7 State-Based Scripting – Behavior Trees
Same Batman boss characters

8 Questions?

9 Unity3D Scripting UnityScript JavaScript-inspired
JavaScript is commonly used for web page scripting Internally, everything is using .NET via Mono

10 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

11 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 );

12 C# Overview Almost exactly the same as Java Classes Methods Members
Public vs Private Comments Primitive Types Branching, Loops Interface

13 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) {

14 C# - Comments Note for humans, ignored by the computer
// A single line comment /* A different single line comment */ /* A mulitple line comment */

15 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

16 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

17 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

18 C# - Event Handling Events in Unity are just method calls
Important events: Update FixedUpdate OnTriggerEnter OnCollisionEnter Many other ”OnX”

19 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 );

20 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 );

21 Questions?

22 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 }

23 C# - Operator Overloading
Lets you use math symbols (+, -, *, /) instead of calling methods. Vector3 operator+ operator- operator* operator/ operator== operator!=

24 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; }

25 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();

26 Questions?

27 Let's look at a real example.
C# Let's look at a real example.


Download ppt "Behavior trees & The C# programming language for Java programmers"

Similar presentations


Ads by Google