Professional Documents
Culture Documents
Autumn 2016
2. Scripting games, and
C# (.Net/Mono)
Chapter 11 "Scripting
"Scripting languages and file formats"
formats"
[Madhav, 2014, pp. 224-
224-242].
Section 15.8 "Scripting
"Scripting"" in [Gregory, 2014, pp. 954-
954-978]
Bytecode design pattern [Game
[Game Programming Patterns]
Patterns]
Juha Vihavainen
University of Helsinki
Outline
Scripting languages
the tradeoffs of using scripts
different types of scripting languages
Implementing a scripting language (very briefly)
the phases of processing a programming language
interpretation by a VM vs vs.. native execution
Bytecode design pattern
"give behavior the flexibility of data by encoding it as
instructions for a virtual machine"
~ metaprogramming, managing code as resource
Advantages Disadvantages
it's typically much easier runtime performance is
and faster to make and typically substantially slower
test changes in script than C/C++
than with, say, C/C++ use multiple languages (system,
unlike in C/C++, bad script)
code is unlikely to crash language tools may be less
the entire game mature, limited, or non-
non-existing
QuakeC
Visual scripting systems
SCUMM
flowchart
flowchart--like scripting system Blueprint
used for level, AI scripting UnityScript
to hook up game-
game-level events and behaviour
e.g., enemies spawn when the player opens a door
inheritance
inheritance,, generics
logical properties (accessors)
accessors)
many other features:
features: lambda expressions,
expressions, reflection. .
CIL =
bytecode
~ Java
.class file
[Illustrated C# 2012,
p. 9.]
Defining a class
In C# , a single file can contain
Simple example multiple public class definitions
Passing arguments in C#
Values are usually passed by value (of course)
Heap--allocated objects are passed by reference
Heap
Variables can be passed by-
by-reference with keyword "ref
"ref""
public static void Add (int i, ref int result) {
result += i;
} ...
int total = 20; passes the address of "total"
Add (10, ref total); // note obligatory keyword "ref ref""
Console.WriteLine ("Value after Add () call: {0}", total);
resembles C++ reference types ("&")
Also reference out parameters (assigned within the method)
Can also pass an object-
object-reference variable by reference:
reference: passes the
address of the local variable itself (of course)
6.12.2016 Juha Vihavainen / University of Helsinki 20
Inheritance
Use ":
":" to indicate inheritance (vs. "extends
"extends"" in Java)
C++
C++--like notation (but without C++ public
public//private clauses)
Constructors can invoke base-
base-class constructor by special base-
base-class
constructor call:
public Derived (int
(int x, int y) : base (x, y) . . // note base keyword
Casting up and down as in Java (usually run-
run-time check needed)
Must use "virtual
"virtual"" keyword to indicate virtual functions
Must use "override
"override"" when redefining a virtual method
can call the base-
base-class implementations via ""base
base""
Also: abstract class concept (with "abstract
abstract"" keyword)
C# property example
public class GameInfo {
...Name = "...";
private string name;
public string Name { // can have access modifiers
get { return name; }
set { name = value;
value; } // contextual keyword value
}
}
get actually a method, returning a value of the specified type
Automatic properties
Often, a property just reads and writes one variable (why
(why..?)
..?)
Such get and set can be automatically created (by the compiler)
public class GameInfo {
public string Name { get; set; } // automatic property
}
here, a hidden "private
"private string"
string" member is created and used
Behaves the same way as the previous Name property
GameInfo g = new GameInfo ();
g.Name = "Radiant Silvergun"; // calls "set
set""
System.Console.Write (g.Name); // calls "get
get""
Here too, can omit one of the two, or specify restricted accessibility
public string Name { get; protected set; }