cFred.FaceCharacter(cWilma);
cFred.Say("Hello Wilma");
cFred.FaceCharacter(cBarney);
cFred.Say("Hello Barney");
cFred.FaceCharacter(cWilma);
cFred.Say("How are you, dear");
Which gets tedious after a while, so you make up a function:
function FaceAndSay(Character *speaker, Character *facing, String what) {
speaker.FaceCharacter(facing);
speaker.Say(what);
}
...
// in script header
import function FaceAndSay(Character *speaker, Character *facing, String what);
...
FaceAndSay(cFred, cWilma, "Hello Wilma");
FaceAndSay(cFred, cBarney, "Hello Barney");
FaceAndSay(cFred, cWilma, "How are you, dear");
But what looks even better is:
function FaceAndSay(this Character *, Character *facing, String what) {
this.FaceCharacter(facing);
this.Say(what);
}
...
// in script header
import function FaceAndSay(this Character *, Character *facing, String what);
...
cFred.FaceAndSay(cWilma, "Hello Wilma");
cFred.FaceAndSay(cBarney, "Hello Barney");
cFred.FaceAndSay(cWilma, "How are you, dear");
So, you are, in effect, adding a new method to the Character class. This is a noddy example, but it can really lead to much nicer code.
No comments:
Post a Comment
Please keep comments clean: foul language means your comment will not get published. Sorry for the captcha, was getting to much spam.