Yingcai Xiao Event-driven Programming in Game Development Yingcai Xiao.

Slides:



Advertisements
Similar presentations
Chapter 18 Building the user interface. This chapter discusses n Javas graphical user interface. n Swing: an enhancement of a library called the Abstract.
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
Microsoft® Small Basic
Chapter 16 Graphical User Interfaces John Keyser’s Modifications of Slides by Bjarne Stroustrup
Computer and Communication Fundamental Basic web programming Lecture 8 Rina Zviel-Girshin.
Video Game Design Lesson 1. Game Designer Person involved in the development of a video game Person involved in the development of a video game Usually.
INNER WORKINGS OF UNITY 3D. WHAT WE ARE GOING TO COVER Intro to Unity Physics & Game Objects Cameras & Lighting Textures & Materials Quaternions and Rotation.
Game Development with Kinect
Based on slides created by Edward Angel
INTRODUCTION TO SCRATCH. About Me Resources Scratch Website Learn Scratch Washington-Lee Computer.
XP Tutorial 5 Buttons, Behaviors, and Sounds. XP New Perspectives on Macromedia Flash MX Buttons Interactive means that the user has some level.
NetSim ZigBee Simulation Code Walkthrough in 10 steps
Yingcai Xiao EDP Scripting Yingcai Xiao. Why do we need EDP? What is EDP? How to implement EDP? How can we take advantages of EDP in game design and implement?
Welcome to CIS 083 ! Events CIS 068.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
EEC-693/793 Applied Computer Vision with Depth Cameras Lecture 13 Wenbing Zhao
Introduction to Matlab & Data Analysis
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Yingcai Xiao Voxel Game Engine Development. What do we need? What tools do we have? How can we design and implement? We will answer those questions in.
Developing the Game User Interface (UI) Lesson 5.
SE 320 – Introduction to Game Development Lecture 8: Animations, GUIs, Debugging and IDEs Lecturer: Gazihan Alankuş Please look at the last two slides.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Event Driven Programming, The.
Computer Graphics I, Fall 2010 Input and Interaction.
© 2010 Delmar, Cengage Learning Chapter 3: Working with Symbols and Interactivity.
Yingcai Xiao Game Development Interactive Animation.
Programming games Show your version of Bo the dog. Start cannonball Preview: video, audio work session (cannonball) Homework: Cannonball with ball in a.
UFCFS D Technologies for the Web Unity 3D: Review of Topics and Related Concepts.
CSCE 121: Introduction to Program Design and Concepts, Honors Dr. J. Michael Moore Spring 2015 Set 15: GUIs 1.
Learning Unity. Getting Unity
Game Maker Terminology
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Yingcai Xiao Game Development with Unity3D. Outline IDE Engine Assets Tutorial Examples Inside.
CIS 3.5 Lecture 2.2 More programming with "Processing"
UFCEK-20-3Web Games Programming Unity 3D: Review of Topics Publishing for the Web.
More on GLUT Programming Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, September 15, 2003.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Macromedia Flash 8 Revealed WORKING WITH SYMBOLS AND INTERACTIVITY.
Problem Solving Methodology Rachel Gauci. Problem Solving Methodology Development Design Analysis Evaluation Solution requirements and constraints. Scope.
Interactive Computer Graphics
SCRIPT PROGRAMMING WITH FLASH Introductory Level 1.
GameDevClub CODE CHEAT SHEET NOTE: ALL OF THE CODE IS CASE-SENSITIVE AND THE SYNTAX IS STRICT SO A LOT OF YOUR ERRORS WILL PROBABLY COME FROM TYPOS If.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
Yingcai Xiao Interactive Game Design Final Overview Yingcai Xiao.
Yingcai Xiao Game Development with Unity3D Inside/Outside Unity3D.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Programming Logic and Design Seventh Edition Chapter 12 Event-Driven GUI Programming, Multithreading, and Animation.
Behavior trees & The C# programming language for Java programmers
Game Development with Unity3D
Interactive Animation
EEC-693/793 Applied Computer Vision with Depth Cameras
Quick Intro to Unity Lecture 2.
Collision Theory and Logic
Game Development with Unity3D Inside/Outside Unity3D
CompSci 230 S Software Construction
Sai Goud Durgappagari Dr. Yingcai Xiao
EEC-693/793 Applied Computer Vision with Depth Cameras
Collision Theory and Logic
Human Computer Interaction
More on Graphical User Interfaces
Programming HCI Yingcai Xiao Yingcai Xiao.
EEC-693/793 Applied Computer Vision with Depth Cameras
Event-driven Programming
A Prime Example of HCI Application
Week 6: Time and triggers!
Professional Environment
EEC-693/793 Applied Computer Vision with Depth Cameras
Presentation transcript:

Yingcai Xiao Event-driven Programming in Game Development Yingcai Xiao

Why do we need EDP? What is EDP? How to implement EDP? Taking advantages of EDP in gaming development. EDP in Unity3D with examples. How to use C# to implement EDP? How to debug in Unity3D

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

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

Events 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 Event Handler

Key Components of EDP 1.Events 2.Event loop 3.Event mapping 4.Event dispatching 5.Event handlers All, but the handlers, are taken care of by the game engine and its underplaying graphics and OS platform.

Events Notifications of 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.

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.

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.

Event Loop The event loop checks if there is any event (including the idle event) in the queue. If yes, dispatches the event to its handler. If not, waits for input. Or puts an idle event into the queue if there is an idle handler.

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. Names of event handlers are case sensitive.

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.

EDP in Unity3D

Unity game engine supports EDP. EDP Programming in Unity needs to (1) Identify “Event Generators” (2) Identify “Events” to process (3) Create “Game Objects” to generate events (4) Create “Data Structures” and “Algorithm” in “Event Handlers” to process events. (5) Map “Events” to “Event Handlers”

Event Generators Events can be generated by hardware and software. Hardware: CLI (Coomand Line Interface): Keyboard GUI (Graphical User Interface): Mouse, Touch Pad, Tilting NUI (Natural User Interface); Kinect, Leap Motion, Software: Collisions between objects Key points in animations

Unity Input Events Hardware Events: need device interface to get the “messages” of corresponding events. e.g. OpenNI for NUI Software Events: created by game engines at specific time selected by the programmers.

Input Events Edit-> Project Setting-> Input

Unity Animation Events (Software Events) Allows you to call functions in the object’s script at specified points in the timeline of an animation. Add a new Animation Event by double- clicking the Event Line or by using the Event button.

GO Event Generation Collision Events: Need to enable collider for the GO to handle collision events. Need to enable capsule collider for GOs of complex shapes. Animation Events: Need to generate the event in the animation timeline. Need to implement a script and connect it to the event.

Game Object – Animation - Events 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.

Animation Event When you add an event, a dialog box will appear to prompt you for the name of the function and the value of the parameter you want to pass to it.

Scripting in Event Handlers 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)

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 */} }

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.

Unity3D EDP Demo #1 IGD5 (for Unity4)

Unity3D EDP Demo #1 (IGD5) 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.

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.

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

Unity C# Code Example: IGD5.cs (in EDP-Unity4/Assets)

C# Code Example: IGD5.cs 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 //colision detection void OnCollisionEnter (Collision collision) { if (collision.gameObject.tag == "terrain") { collisionCount++; score.GetComponent ().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 void Update () { if (!isReady) { if (time >= threshold) { isReady = true; time = 0; } else { time += Time.deltaTime; } } else

C# Code Example: IGD5.cs { if (Input.GetKey (KeyCode.G)) { if (gameObject.GetComponent ()) { gameObject.rigidbody.useGravity = !gameObject.rigidbody.useGravity; } else { gameObject.AddComponent (); } 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 else if (Input.GetKey (KeyCode.S)) { ///simulation isSimulate = !isSimulate; //on-off isReady = false; } } //Animation control if (isSimulate) simulationControl(); } }

Unity3D EDP Demo #2 EDP-Unity5 (source code in EDP-Unity5/Assets executable in EDP-Unity5/SecondBuild)

EDP in Unity5 Three types of events in this example: Keyboard (HW) Collision (SW) Animation Events (SW) Events are counted and displayed.

Keyboard Events (HW) Left/right & (a/d) keys for horizontal movements Up/Down & (w/s) keys for vertical movements Hold “g” to experience gravity Toggle “t” to start/stop animation in a triangular path

Collision (SW) Each collision increases the event count by 1. GO color changes as it collides with walls. Needs to enable collision detection for GO.

Animation Events (SW) Event added on the animation timeline when the GO reaches the top of the triangular path. (Using the Unity Animation GUI.) Need to provide a function (the event handler) when creating an animation event.

Communications with the Game Engine There are three parts of a game value, for example, collision count. An internal private variable in the event handler to keep track of the collision: “ private int count;” A GO that displays the value of count. A public variable in the handler linked to the GO (through Unity Inspector): “public Text countText;”

using UnityEngine; using UnityEngine.UI; using System.Collections; public class Player : MonoBehaviour { public float MoveSpeed = 10f; public Text countText; private Rigidbody rb; private Renderer rend; private Light mylight; private Animation myanimation; private int count; void Start () // Use this for initialization { rb = GetComponent (); rend = GetComponent (); mylight = GetComponent (); myanimation = GetComponent (); count = 0; SetCountText(); }

void Update () { // Update is called once per frame if (Input.GetKey(KeyCode.G)) { print("g for Gravity"); rb.useGravity = !rb.useGravity; } else if (Input.GetKey(KeyCode.T)) { print("T for play/stop triangle movement"); if (myanimation.isPlaying) { myanimation.Stop(); } else { myanimation.Play(); } float inputX = Input.GetAxis("Horizontal"); float inputZ = Input.GetAxis("Vertical"); float moveX = inputX*MoveSpeed*Time.deltaTime; float moveZ = inputZ*MoveSpeed*Time.deltaTime; rb.AddForce(moveX, 0f, moveZ); }

void OnCollisionEnter(Collision col) { //collosion detection print(col.collider.name); if(col.collider.name == "FrontWall") { rend.material.color = Color.blue; mylight.color = Color.blue; } else if(col.collider.name == "LeftWall") { rend.material.color = Color.red; mylight.color = Color.red; } else if(col.collider.name == "RightWall") { rend.material.color = Color.yellow; mylight.color = Color.yellow; } else if(col.collider.name == "BackWall") { rend.material.color = Color.green; mylight.color = Color.green; }

else if(col.collider.name == "Floor") { rend.material.color = Color.magenta; mylight.color = Color.magenta; } SetCountText(); } void SetCountText() { count ++; countText.text = "Count:" + count.ToString(); }

EDP with OpenGL Unity3D and Unreal game engines are based on graphics engines: OpenGL or ActiveX. 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.