You are on page 1of 11

Question No.

3:
Research ways in which artificial intelligence is influencing interactive stories? What are some
ways to make an NPC (Non-Playing Character) seem more authentic?

Ans:

In video games, artificial intelligence (AI) is used to generate responsive, adaptive


or intelligent behaviors primarily in non-player characters (NPCs) similar to human-like
intelligence. Artificial intelligence has been an integral part of video games since their inception
in the 1950s.[1] AI in video games is a distinct subfield and differs from academic AI. It serves
to improve the game-player experience rather than machine learning or decision making.

--------------------------------
Question no.4:

T1: Understanding two C# data types: value type and reference type

Part 1:
1) What’s the operator ‘using’ used for? Or what are the UnityEngine and
System.Collections?
Ans:

 using
Using’ imports namespace. Namespaces are a collection of classes and other data
types that are used to categorize the library.

 UnityEngine
UnityEngine we can say that is a collection of all the classes related to Unity that are
imported by ‘using’ keyword.

UnityEngine acts as a namespace in the above statement and if not declared you
would have to write the namespace ‘UnityEngine’ again and again in code while
referring to a specific class in that particular namespace

 System.Collections
System.Collections Contains interfaces and classes that define various collections of
objects, such as lists, queues, bit arrays, hash tables and dictionaries. It is also a
namespace.

2) Which class does the class ‘MonoBehaviour’ inherit? What are the new members
(attributes and functions) defined?
Ans:

When we create a new C# class in Unity, it automatically inherits from


the MonoBehaviour class, which is Unity's base class for components. The new
methods/functions defined in the ‘MonoBehaviour’ class are value_DataType(),
reference_DataType().

a) What is the printout in the Console window? Does the sphere change its position? Why?
Is the Vector3 a value type or reference type? When we amend ‘v1’, does the
‘transform.position’ change as well? Analyze the code and explain.
Ans:

1) The printout of the code is.

2) Sphere do not change the position because due to value type datatype the original
value is not updated it only changes the variable so that’s why it prints out same
position and the sphere’s position does not change.
3) Vector3 is a value type.
4) No, When we amend the value of v1 the value of transform.positon does not change
because vector3 is not a reference type. And when value of vector3 variable is set to
transform.position. what actually happens is that another copy of the data of
transform.position is created to some new location.
5) On amending v1 the original value of transform.position does not change because in
this case it is a value type variable.
b) What is the printout in the Console window? Does the sphere change its position? Why?
Is the variable ‘trans’ with the type ‘Transform’ a value type or reference type (of data)?
When we amend ‘trans’ (by changing its ‘position), does the ‘transform.position’ change
as well? Analyze the code and explain.
Ans:

1) The printout of the code is.

2) Sphere changes the position because due to reference type datatype the original
value is updated it changes the reference so that’s why it prints out updated position
and the sphere’s position is changed.
3) trans with the datatype transform is reference type.
4) Yes, when we amend the value of trans the value of transform changes.
5) On amending trans the reference to the value of transform.position changes because
in this case it is a reference type variable.

--------------------------------
T2: Understanding class hierarchy, accessibility of class members, foreach,
the data structure ‘list’
c) what’s the accessibility of the attribute ‘name’? public, protected or private?
Ans: In the given code the attribute ‘name’ is public by default.

Part 2: Add into the class ‘GameObject2D’ two public methods: setName(string) to
assign a name to an instance of the class; getName() to return the name of the instance.

Ans:
using UnityEngine;
public class GameObject2D
{
string name,value;
void setName()
{
name = value;
}
string getName()
{
return name;
}
}
public class CSharpDemo : MonoBehaviour
{
void Start()
{

}
void Update()
{

}
}

--------------------------------

Part 3: Define a new method in the class ‘CSharpDemo’, called


‘createGameObject2D()’, with the skeleton As below?

Ans:
using UnityEngine;
public class GameObject2D
{
string name,value;
void setName()
{
name = value;
}
string getName()
{
return name;
}
}

public class CSharpDemo : MonoBehaviour {


void createGameObject2D()
{

GameObject2D Peter, David, John;

List<string> c = new List<string>(10);


string[] names = { "Peter", "David", "John"};

foreach (string item in names)


{
Debug.Log(item);
}

--------------------------------

Part 4: In the method Start() call the method above, i.e. CreatGameObject2D(). Save the
script and back to Unity editor for playtest. You are expected to see the names of the three
instances of GameObject2D printed out to the Console window?

Ans:
using UnityEngine;
public class GameObject2D
{
string name,value;
void setName()
{
name = value;
}
string getName()
{
return name;
}
}

public class CSharpDemo : MonoBehaviour {


void Start()
{
createGameObject2D();
}
void createGameObject2D()
{
GameObject2D Peter, David, John;
List<string> c = new List<string>(10);
string[] names = { "Peter", "David", "John"};
foreach (string item in names)
{
Debug.Log(item);
}
}
}

--------------------------------

Part 5: Now, define a new class called ‘Rectangle’, which derives from the class
‘GameObject2D’:
ClassRectange : GameObject2D { … }
(1) Add two new private members into the class, width and height, whose type is ‘int’.
(2) Add a constructor with 3 parameters: “public Rectangle (string, int, int) { … }”.
Complete this constructor by initializing the attributes ‘width’, ‘height’ and ‘name’. Note that the
‘name’ is defined in the base/super/parent class.

Ans:
using UnityEngine;
public class GameObject2D
{
string name,value;
void setName()
{
name = value;
}
string getName()
{
return name;
}
}
class Rectangle : GameObject2D
{
int width, height;
string name;
public Rectangle (string n, int w, int h)
{
name = n;
width = w;
height = h;
}
}
public class CSharpDemo : MonoBehaviour {

--------------------------------

Part 6: Define a new method in the class ‘CSharpDemo’, called ‘createRectangle()’. Similar to
the step 4, but instead of creating 3 instances of the class ‘GameObject2D’, you are required to create 3
instances of the class ‘Rectangle’. And to do so, you need to use the constructor you defined in the
previous step. Also, instead of using the data structure ‘List’, this time you are required to use
‘ArrayList’. In the method Start() call the method above, i.e. CreatRectangle(). Save the script and back
to Unity editor for playtest. You are expected to see the names, width and height of the three instances of
Rectangle printed out to the Console window.

Ans:
using UnityEngine;
public class GameObject2D
{
string name,value;
void setName()
{
name = value;
}
string getName()
{
return name;
}
}
class Rectangle : GameObject2D
{
int width, height;
string name;
public Rectangle (string n, int w, int h)
{
name = n;
width = w;
height = h;
}

Rectangle Rec1 = new Rectangle("Rectangle1", 60, 30);


Rectangle Rec2 = new Rectangle("Rectangle2", 20, 40);
Rectangle Rec3 = new Rectangle("Rectangle3", 90, 50);
}

public class CSharpDemo : MonoBehaviour {

void Start()
{
createRectangle();
}

void createRectangle()
{
var arraylist1 = new ArrayList()
{"Rectangle1",60, 30};

var arraylist2 = new ArrayList()


{ "Rectangle2",20, 40};

var arraylist3 = new ArrayList()


{"Rectangle3",90, 50 };

foreach (var item in arraylist1)


{ Debug.Log(item); }

foreach (var item in arraylist2)


{ Debug.Log(item);}

foreach (var item in arraylist3)


{ Debug.Log(item);}
}
}

--------------------------------

Part 7: In the method ‘reference_DataType()’, create an instance of Rectangle with the reference,
say ‘rec1’ with certain attributes (names, width, height), then assign it to another variable like ‘Rectangle
rec2 = rec1’. Assign different attributes to rec2. Playtest to see if the attributes of rec1 has changed or not.
Expalin the results.
a)
Ans:
using UnityEngine;
public class GameObject2D
{
string name,value;
void setName()
{
name = value;
}
string getName()
{
return name;
}
}
class Rectangle : GameObject2D
{
public
int width, height;
string name;
public Rectangle (string n, int w, int h)
{
name = n;
width = w;
height = h;
}

public class CSharpDemo : MonoBehaviour {

void Start()
{
reference_DataType();}

void reference_DataType()
{
Rectangle Rec1= new Rectangle("Rec1",30,50);
Rectangle Rec2= Rec1;
Debug.Log(“Rectangle one:”);
Debug.Log(Rec1.name);
Debug.Log(Rec1.width);
Debug.Log(Rec1.height);
Debug.Log(“Rectangle two:”);
Debug.Log(Rec2.name);
Debug.Log(Rec2.width);
Debug.Log(Rec2.height);
}
}
b)

Ans:

using UnityEngine;
public class GameObject2D
{
string name,value;
void setName()
{
name = value;
}
string getName()
{
return name;
}
}
class Rectangle : GameObject2D
{
public
int width, height;
string name;
public Rectangle (string n, int w, int h)
{
name = n;
width = w;
height = h;
}

public class CSharpDemo : MonoBehaviour {

void Start()
{

reference_DataType();
}

void reference_DataType()
{
Rectangle Rec1= new Rectangle("Rec1",80,20);
Rectangle Rec2= new Rectangle("Rec2",40,50);
Debug.Log(“Rectangle one:”);
Debug.Log(Rec1.name);
Debug.Log(Rec1.width);
Debug.Log(Rec1.height);
Debug.Log(“Rectangle two:”);
Debug.Log(Rec2.name);
Debug.Log(Rec2.width);
Debug.Log(Rec2.height);
}
}

--------------------------------
THE END

You might also like