USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,

Slides:



Advertisements
Similar presentations
What have we learned so far… Preprocessor directives Introduction to C++ Variable Declaration Display Messages on Screen Get Information from User Performed.
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
An Introduction to C Programming Geb Thomas. Learning Objectives Learn how to write and compile a C program Learn what C libraries are Understand the.
Chapter 9 Interactive Multimedia Authoring with Flash - Introduction to Programming “Computers and Creativity” Richard D. Webster, COSC 109 Instructor.
Introduction to Python
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
HOMEWORK REVIEW This is an if else statement layout if (condition) { code to be executed if condition is true; } else { code to be executed if condition.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
TUTORIAL 10: PROGRAMMING WITH JAVASCRIPT Session 2: What is JavaScript?
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Coding Design Tools Rachel Gauci. What are Coding Design Tools? IPO charts (Input Process Output) Input- Make a list of what data is required (this generally.
USING UNITY JAVASCRIPT. CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names,
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
CS346 Javascript -3 Module 3 JavaScript Variables.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Applications Development
JavaScript Syntax, how to use it in a HTML document
Basic Java Syntax COMP 401, Spring 2014 Lecture 2 1/14/2014.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
JavaScript, Fourth Edition
Conditional Statements.  Quiz  Hand in your jQuery exercises from last lecture  They don't have to be 100% perfect to get full credit  They do have.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Problem Solving Methodology Rachel Gauci. Problem Solving Methodology Development Design Analysis Evaluation Solution requirements and constraints. Scope.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
ECA 225 Applied Interactive Programming ECA 225 Applied Online Programming control structures, events, objects.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Structured Programming (4 Credits) HNDIT Week 2 – Learning Outcomes Design an algorithmic solution for simple problem such as computation of a factorial,
Debugging, Escape Command, More Math. It’s your birthday!  Write a program that asks the user for their name and their age.  Figure out what their birth.
Python Let’s get started!.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 – Introduction to C# Programming Outline 3.1 Introduction 3.2 Simple Program: Printing a Line.
CST336, Dr. Krzysztof Pietroszek Week 2: PHP. 1.Introduction to PHP 2.Embed PHP code into an HTML web page 3.Generate (output HTML) web page using PHP.
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
BIT116: Scripting Lecture 05
CHAPTER 4 DECISIONS & LOOPS
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 10 Programming Fundamentals with JavaScript
Python Let’s get started!.
Line Continuation, Output Formatting, and Decision Structures
- Standard C Statements
The Selection Structure
Variables, Expressions, and IO
Introduction to Scripting
CHAPTER 4 CLIENT SIDE SCRIPTING PART 2 OF 3
Chapter 10 Programming Fundamentals with JavaScript
Line Continuation, Output Formatting, and Decision Structures
WEB PROGRAMMING JavaScript.
T. Jumana Abu Shmais – AOU - Riyadh
JavaScript What is JavaScript? What can JavaScript do?
JavaScript What is JavaScript? What can JavaScript do?
2.6 The if/else Selection Structure
C Programming Language
Introduction to C Programming
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

USING UNITY JAVASCRIPT

CONVENTIONS AND SYNTAX IN JAVASCRIPT Case Sensitivity All keywords like var or function must be in lowercase. All variable names, function names and identifiers are case sensitive. The following two variables are separate and independent of each other: var Test = 6; var test=4; Test and test will not be classified as the same variable and will cause a Javascript error Whitespace Like most programming and scripting languages, Javascript requires one space between keywords, names and identifiers. Extra spaces are ignored so you can use tabs and spaces wherever you need to make your code easier to read and debug Semi-colons Every line of Javascript must end in a semi-colon ; Blank lines don't need sem-colons.

Comments You can add in comments to help yourself keep track of parts of script. This does not effect the script itself and is not visible // this is a single line comment /* this is a multi-line comment */ Identifiers (Variable names, function names, labels.) The first character in an identifier name must not be a digit. Some key words include: Variables Declare variables using the keyword var. Javascript variables have no explicit data type and can contain data of any type. var mynumber;

EXAMPLE 1- HELLO WORLD This is a basic script. Create a new JavaScript script in Unity. Copy and paste the code and click save. #pragma strict function Start () { Debug.Log (“Hi my name is”); Debug.Log (“Welcome to programming”); } Create a new empty GameObject and attach the script to it. If you get stuck get the link to Tom’s demonstration.

EXAMPLE 2- MATH #pragma strict var num1 : int = 3; var num2 : int = 4; var num3 : int; var displaytext : String; //empty for now function Start () { Debug.Log("Number 1 = " +num1); Debug.Log("Number 2 = " +num2); num3 = num1 + num2; Debug.Log("Number 1 + Number 2 = " +num3); }

EXAMPLE 3- CHANGE COLOUR (IF STATEMENTS) #pragma strict function Start () { } function Update() { if(Input.GetKeyDown(KeyCode.R)) { gameObject.renderer.material.color = Color.red; } if(Input.GetKeyDown(KeyCode.G)) { gameObject.renderer.material.color = Color.green; } if(Input.GetKeyDown(KeyCode.B)) { gameObject.renderer.material.color = Color.blue; }

TYPES OF VARIABLES Optionally, you can also explicitly specify the variable type when you declare a variable. Script = JavaScript var foo : int; // foo is explicitly an integer. var bar : float; // bar is explicitly a floating-point value. var message : String; // message is explicitly a character string.

IF ELSE STATEMENTS When and why would you use IF ELSE statements? We use IF ELSE to make one-time decisions. That is depending on the users response some code will be executed and some code will be ignored. The if statement checks whether the text expression inside ( ) is true or not. If the test expression is true, statement/s inside the body of the IF statement is executed but if test is false, statement/s inside body of the IF is ignored. The if...else statement is used if the programmer wants to execute some statement/s when the test expression is true and execute some other statement/s if the test expression is false. This is the format for an IF ELSE statement if ( condition ) { code to be executed if condition is true; } else { code to be executed if condition is false; }

IF/ELSE COFFEE SCRIPT #pragma strict var coffeeTemperature:float = 85.0f; var hotLimitTemperature:float = 70.0f; var coldLimitTemperature:float = 40.0f; function Update() { if(Input.GetKeyDown(KeyCode.Space)) TemperatureTest(); coffeeTemperature -= Time.deltaTime * 5f; } function TemperatureTest() { // If the coffee's temperature is greater than the hottest drinking temperature... if(coffeeTemperature > hotLimitTemperature) { //... do this. Debug.Log("Coffee is too hot."); } // If it isn't, but the coffee temperature is less than the coldest drinking temperature... else if(coffeeTemperature < coldLimitTemperature) { //... do this. Debug.Log("Coffee is too cold."); } // If it is neither of those then... else { //... do this. Debug.Log("Coffee is just right."); }

IF ELSE STATEMENTS scripting/if-statements

LETS TRY ONE OURSELVES Using Unity JavaScript create an IF ELSE statement regarding age. Pseudocode (coding design tool, helps me visualize what I want in my script) IF AGE >= 18 THEN PRINT “You Qualify” ELSE PRINT “You do not qualify” END IF

MY ANSWER #pragma strict var age= 22; function Start() { if (age >= 18) { Debug.Log ("You Qualify"); } else { Debug.Log("You do not qualify"); } } function Update() { }

YOUR TURN Create script using if else statements so a teacher can calculate whether their student has passed or failed their SAC. Begin with: #pragma strict function start () { } function update () { }

PSEUDOCODE FOR YOUR IF ELSE STATEMENT IF SCORE >= 50 THEN PRINT “You Pass” ELSE PRINT “You failed” END IF

FOR LOOPS FOR LOOPS through a block of code a number of times. Format: (set initial variable; condition; action on the variable) (rules, action)

EXAMPLE 4- LOOPING (FOR) #pragma strict var numEnemies : int = 3; function Start () { for(var i : int = 0 ; i < numEnemies ; i++) { Debug.Log("Creating enemy number: " + i); } (set initial variable; condition; action on the variable)

EXAMPLE 5- ROTATE OBJECT #pragma strict var variableRotationRate = 0.0f; var fixedRotationRate = 0.0f; function Update () { transform.Rotate( Vector3.up * Time.deltaTime * ( variableRotationRate * 360.0f ) ); } function FixedUpdate() { transform.Rotate( Vector3.up * Time.deltaTime * ( fixedRotationRate * 360.0f ) ); }