1 / 36

Yingcai Xiao

Yingcai Xiao. Yingcai Xiao. EDP Scripting. Why do we need EDP? What is EDP? How to implement EDP? How can we take advantages of EDP in game design and implement? How to use C# to implement EDP?. Interaction => events (software notifications of hardware status changes.). Display Device

Download Presentation

Yingcai Xiao

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Yingcai Xiao • Yingcai Xiao EDP Scripting

  2. Why do we need EDP? What is EDP? How to implement EDP? How can we take advantages of EDP in game design and implement? How to use C# to implement EDP?

  3. Interaction => events (software notifications of hardware status changes.) Display Device Driver (GDI) Input Device Driver Game (Software)

  4. EDP: Event-driven programmingApplication awaits in an idle state and responds to user requests by calling the corresponding even handlers. EDP • Yingcai Xiao

  5. A menu in C++: char c; bool done = false; while(!done) { cout << “Please make your selection, q to end:” cin >> c; switch(c) { case “+”: add( ); break; case “-”: sub( ); break; case “q”: done = true; break; } } Event Loop Event Mapping & Event Dispatching Event Events Event Handler

  6. Key Components of EDP • Events • Event loop • Event mapping • Event dispatching • Event handlers • All, but the handlers, are taken care of by the game engine and its underplaying graphics and OS platform.

  7. Events • Software notifications of hardware status changes. • Can be represented in a char, string, encoded number, object oriented message. • Generated by the OS when users interact with input devices. • Sent to the active application (the application owns the active window.) • Queued at the active application’s event queue. • Removed by the application one by one. • Dispatched to the event handlers according to event mapping.

  8. Events • All other events and new incoming events await until the current event is being processed. • The idle “event” is critical event for drawing background animations. • The idle “event” is internally generated when the event queue is empty. • The idle “event” handler draws the the background animation when there is no other pending events. • The idle “event” should be processed promptly in order to avoid delaying the processing of possible upcoming events.

  9. Event handlers • Programs which process the corresponding events. • Usually a call-back function. • e.g. OnMouseMove • Most likely has input arguments, e.g., cursor location. • Note: no new event can be processed when an event handler is running. • Any long-time running animation should be broken into a sequence of short background animations. • Another challenge is to pass a computational value to another event handler, e.g., the idle event handler.

  10. Event Loop • The event loop checks if there is any event (including the idle event) in the queue. • If yes, dispatch the event to its handler. • If not, wait for input. • Or put an idle event into the queue if there is an idle handler.

  11. Event Mapping • Event mapping is done when the program register an event handler to an event. • Usually at compile time of constructing the game. • Each handler can be registered to multiple events. • More than one handlers can be registered to each event. • Make sure the handlers define proper arguments for the events.

  12. Event Dispatching • Event dispatching is done at runtime. • The dispatcher invokes the corresponding handlers one by one. • Usually in the order of registration. • Most of the time, only one handler is registered for each event.

  13. EDP in Unity3D • Unity game engine supports EDP. • Download the EDP-Scripting project. • Open it with Unity3D and select scene1. • Object Cylinder and Terrain. • Note: animation1 was created using Unity GUI. • IGD5 script was created to do simulation. • Simulation is animation obeying certain rules, e.g., trajectory of a projectile.

  14. EDP-Scripting Demo • “a” for animation on/off. • “s” for simulation on/off. • “g” for gravitation on/off. • Animation is different when “g” is on and off. • Hit “s” to stop simulation quickly otherwise the object will run off the screen. • Hit “s” to bring the object back to view if it run away. • Score is increased by one when the cylinder hits the terrain.

  15. EDP-Scripting Demo Coding • Need to enable collider for the terrain for score counting. • Need to enable capsule collider for the cylinder too. • Need to create an animation and connect it to the object. • Need to create a script and connect it to the object

  16. EDP-Scripting Demo Coding • To create animation: • Window->Animation • Click on the object to be animated. • Component->Miscellaneous->Animation • Name it. • Start recording (the red button in the Animation Window) • Add Curve->Transform->Position • Stop and save.

  17. EDP-Scripting Demo Coding • To create scripts: • Assets->Create->C# Script • Name it in the Assets Pane. public class Test:MonoBehaviour{ void Start () { /* initialization at the start of the application */} void Update ( ) { /* invoked at every frame of drawing */} }

  18. EDP-Scripting Demo Coding • To link the script: • Select the object • Component->Add->Scripts->Name (e.g.test) • The script box will be added to the inspector of the object. • Note all “public” variables will be displayed in the script box. • To link the “Score” viable, drag “Hierarchy->Canvas->scoreText” to the “Score” box.

  19. EDP-Scripting Demo Coding The scoreText object needs to be created beforehand. GameObject->UI->Text

  20. Scripting • Writing code to control interaction and animation. • Unity supports: • C# for heavy duty programs • JavaScripts for simple interactions • Boo: “compiled Python”, CLI, .NET, Mono compatible • Unreal supports: • C++ • UnrealScript (discontinued)

  21. C# • The de facto programming language for .NET • OS platform independent • Needs CLR (Common Language Runtime): .NET, Mono. Unity uses Mono. • Supports: Class, Struct,Interface,Enum, Delegates • Allow users to define events.

  22. C# Classes Class: a group of code and data to be instantiated to form objects. Four categories of class members: Fields: member variables Methods: member functions Properties: fields exposed using accessor (get and set) methods Events: notifications a class is capable of firing

  23. Example: How to define a class in C# class Rectangle { // Fields protected int width = 1; protected int height = 1; // Methods public Rectangle () { } public Rectangle (int cx, int cy) { width = cx; height = cy; }

  24. Example: How to define a class (user-defined data type) // Accessor Methods public void setWidth(int w) { width = w; } public int getWidth() { return width; } public void setHeight(int h) { height = h; } public int getHeight() { return height; } } // End of Rectangle class // No “;” at the end of class definition.

  25. Example: How to use a class in C# Rectangle rect = new Rectangle(2,4); rect.setHeight(8); rect.setWidth(rect.getWidth() * 2); double darea = (double) (rect.getWidth() * rect.getHeight() ); Note: (1) “Rectangle rect” creats a reference, not object. (2) Treat “Rectangle rect = new Rectangle();” in C# as “Rectangle rect;” in C++. (3) Use “rect.” to dereference in C# instead of “rect->” as in C++.

  26. CLR controls garbage collection. • No need to free memory dynamically allocated with “new” on the heap in C#, hence, no destructors in C#. • CLR uses a reference counter to keep track the number of references pointing the memory. • CLR will free the memory when the reference counter becomes 0. • The Finalize() method (inherited from Object class) is called during garbage collection. • CLR decides when to perform garbage collection. • Force a garbage collection by calling GC.Collect (). (expensive) • Implement a Dispose method and call it when needed to cleanup when an object is not needed any more. Garbage Collection

  27. CLR’s Exception Handling Mechanism: try, catch, finally, and throw File file = null; // Do not define it in the try block. Why? try { file = new File ("Readme.txt"); if(file != null) { /* Do the work here. */} … } catch (FileNotFoundException e) { Console.WriteLine (e.Message); } catch ( … ) { … } catch ( … ) { … } } finally { // Always come here even if there were exceptions. if (file != null) file.Close (); } Example of Exception Handling

  28. C# vs. C++

  29. http://www.tutorialspoint.com/csharp/ http://www.cs.uakron.edu/~xiao/windows/syllabus.html C# Books and Tutorials

  30. using UnityEngine;using UnityEngine.UI;using System.Collections;public class IGD5 : MonoBehaviour {    float time = 0;    float threshold = 0.5f;    bool isReady = true;    bool isSimulate = false;    int collisionCount = 0;    public Vector3 speed = new Vector3(5,5,0);    public GameObject score; // the text object for displaying score     // Use this for initialization    void Start () {  } C# Code Example: IGD5.cs

  31.     //colision detection    void OnCollisionEnter (Collision collision) {        if (collision.gameObject.tag == "terrain") {            collisionCount++;            score.GetComponent<Text>().text = "Score : " + collisionCount;        }    }    void simulationControl () {        transform.position = new Vector3 (    //colision detection    void OnCollisionEnter (Collision collision) {        if (collision.gameObject.tag == "terrain") {            collisionCount++;            score.GetComponent<Text>().text = "Score : " + collisionCount;        }    }    void simulationControl () {        transform.position = new Vector3 ( transform.position.x + speed.x*Time.deltaTime,  transform.position.y + speed.y*Time.deltaTime,  transform.position.z);    } C# Code Example: IGD5.cs

  32.         void Update () {        if (!isReady) {            if (time >= threshold) {                isReady = true;                time = 0;            }            else {                time += Time.deltaTime;            }        } else               void Update () {        if (!isReady) {            if (time >= threshold) {                isReady = true;                time = 0;            }            else {                time += Time.deltaTime;            }        } else        C# Code Example: IGD5.cs

  33.          { if (Input.GetKey (KeyCode.G)) {                if (gameObject.GetComponent<Rigidbody> ()) {                    gameObject.rigidbody.useGravity = !gameObject.rigidbody.useGravity;                } else {                    gameObject.AddComponent<Rigidbody> ();                }         isReady = false;             }             else if (Input.GetKey (KeyCode.A)) {                if (animation.isPlaying)   gameObject.animation.Stop();                else gameObject.animation.Play ();                isReady = false;  }  C# Code Example: IGD5.cs

  34.                 else if (Input.GetKey (KeyCode.S)) {  ///simulation                isSimulate = !isSimulate; //on-off         isReady = false;            }        }         //Animation control        if (isSimulate)  simulationControl();     }} C# Code Example: IGD5.cs

  35. EDP with OpenGL • Unity3D and Unreal game engines are based on graphics engines: OpenGL or . • The dispatcher invokes the corresponding handlers one by one. • Usually in the order of registration. • Most of the time, only one handler is registered for each event.

  36. Geometric Transformation and Parametric Curves

More Related