DMA 126
Behaviors & Scripting
Announcements
Schedule For Today
C# (Unity scripting language)
How would you describe what scripting is for?
How would you describe what scripting is for?
Scripting = getting the computer to do work for us.
Some Uses of Scripts
OK, a bit of dumb question.
Most of the time we want the game to ‘do’ something, or to ‘happen’, or we want a ’system’, ‘mechanic’, that usually involves some amount of scripting, programming.
(Aside) Scripting vs. Programming?
Scripting = programming
scripting implies you are programming at a higher level of abstraction, and interacting with a more complicated underlying system (like a game engine).
Scripts we’ve already made
Rules for script names:
*your code will still work even if you break this last rule, but it is highly recommended that you follow.
Rules for script names:
✅
❌
Rules for script names:
Try to come up with a name that succinctly and clearly describes what you hope your script will actually do.
Rotator 👍(yup!)
PlayerController 😑… does a bunch of stuff besides “Control” the player.
Maybe would be better to separate out into…?
Anatomy of a Script
‘using’ statements
Comments ‘//’ or ‘/* .. */’
COMMENT SHORTCUTS:
Comment�CTRL+(K then C) (Visual Studio) �CTRL+/ (Visual Studio Code)
Uncomment�CTRL+ (K then U)�CTRL+SHIFT+/
public class <class name> : MonoBehaviour {
MonoBehaviour is a sort of template that provides various functionality, and allows us to attach the script to objects in our scenes. We build our scripts on top MonoBehaviour
Class name
Class we’re building on top of (base class, or super class)
Class / Function / Variable
Methods / Functions��“Behavior”
Fields / Global Variables��“state, characteristics”
Class
contains…
Start() { ... }
Update() { ... }
FixedUpdate ? LateUpdate ?
FixedUpdate()
tied to the physics system updates on separate cycle from regular Update()
LateUpdate()
In case we need to make sure some code is executed after some other scripts regular Update()
Fields/Variables
‘Containers’ to hold data, or information about the object.
Always have
Fields/Variables
Fields/Variables
1: ‘public’ or ‘private’�optional
3: Name
4: = initial value optional
2: Type
4: semicolon
Fields/Variables
Set the value of a variable with ‘=’, followed by the value you want set
Here were are assigning the attached rigid body into field ‘rb’ (from PlayerController.cs)
Here were setting count to be its starting value plus 1 (increasing it by one)� (from PlayerController.cs)
Fields/Variables
How do variables help us?
Fields/Variables:��Local Variables
Methods/Functions
Methods/Functions
6: Code goes here
1: Return type
Often void (nothing)
2: Name
3: parentheses
5: Curly braces
4: Argument Type + Argument name
Optional,
Any number of pairs
Each pair comma separated,
public/private
optional
Methods/Functions
Executing ‘SetCountText’ in PlayerController.cs to update the score display text
Methods/Functions
Calling ‘SetActive’ on the collided pickup when collided.
Passing ‘false’ to turns off the game object
The primary things we do to achieve stuff in C#
How do we access a particular field or method?
How do we access a particular field or method?
For fields and methods we make ourselves, we can just type their name
Class / Function / Variable
Methods / Functions��“Behavior”
Fields / Global Variables��“state, characteristics”
Class
contains…
Class / Function / Variable
Methods / Functions
…
Fields / Global Variables:
GameObject
contains…
Class / Function / Variable
Methods / Functions
… and more
Fields / Variables:
… and more�
GameObject
contains…
Class / Function / Variable
Methods / Functions
…
Fields / Global Variables:
GameObject
contains…
Methods / Functions
…
Fields / Global Variables:
Transform
contains…
Also a class
Class / Function / Variable
Methods / Functions
…
Fields / Global Variables:
GameObject
contains…
Methods / Functions
…
Transform
contains…
Also a class
Fields / Global Variables:
Class / Function / Variable
Methods / Functions
…
Fields / Global Variables:
Transform
contains…
Also a class
Class / Function / Variable
Fields / Global Variables:
Methods / Functions
…
Fields / Variables:
Vector3
contains…
Also a class*
Methods / Functions
…
Transform
contains…
*well, technically a struct
Class / Function / Variable
Methods / Functions
…
Fields / Global Variables:
Transform
contains…
Also a class
Class / Function / Variable
Fields / Global Variables:
Methods / Functions
…
Fields / Global Variables:
String (text)
contains…
Also a class
Methods / Functions
…
Transform
contains…
Class / Function / Variable
Methods / Functions
…
Fields / Global Variables:
OurFancyNewScript
contains…
Green = Stuff we get to make up.
Making a new script
Either use the ‘Add Component Button’ or right click in the ‘Project’ tab.
Accessing these in practice with ‘.’ (period)
Starting from ‘other’ type of ‘Collider’, we access its ‘gameObject’ (type GameObject), �and execute ‘SetActive(false)’ on it.
other(Collider) → gameObject (GameObject) → SetActive()
‘this’
AutoComplete (aka intellisense)
After typing a ‘.’ the auto complete menu should pop-up, showing everything available for access, and often some documentation.
You can also force it to appear with CTRL+SPACE
Having AutoComplete issues on lab computers?
If your autocomplete isn’t working on a lab computer…
Change to ‘Visual Studio 2019’
Close visual studio and re-open
Let’s Try Making a new script using these concepts
How about a player-launcher that flings the player in a direction when collided with
Let’s Try Making a new script using these concepts
Let’s try to make something together (off the cuff)
Any ideas?
Can we make something even more general purpose?
Instructor Provided Modules
NOTES:
Helper Modules
LINK TO DOWNLOAD / INSTRUCTIONS
Depending on time, can demo and explain the scripts
Open Work Time + Checking in on sketches