Game Development Reference
In-Depth Information
Implementing the npcInteraction script
The
npcInteraction
class forms an association between a condition and a re-
sponse; it is in fact a container for both a condition to test and a response to invoke if
the condition is true. Remember, it could be any specific condition or response since
those two classes are interfaces.
Perform the following steps to implement the
npcInteraction
script:
1. Create a new class in the editor. Name it
npcInteraction
.
2. Unlike the
npcCondition
,
npcReponse
classes, and most other classes
to date, we will not derive this class from
MonoBehaviour
. Inheriting from
MonoBehaviour
is useful when you want to place your script onto a
GameObject, and in most cases this is desired. For this script, we will not
want to do that (you will see why in a moment). So remove the
MonoBe-
havior
line from the class declaration, and add the
[Sys-
tem.Serializable]
tag so that it resembles the following:
[System.Serializable]
public class npcInteraction {
Adding the
Serializable
attribute is necessary when we don't inherit
from
MonoBehaviour
because serialization is one of many features that
MonoBehaviour
provides to the child classes. We explicitly tag this class as
serializable because we want to be able to save the instances of our class
data in the editor. The difference here is that this script will not be attachable
as a component on a GameObject but rather will appear in place in other
classes as a member variable.