Sharing Character Abilities with Other Students
Classes
In Alice, a class can be thought of like a blueprint.
For example, the Person
class might have the following attributes:
- hair colour
- eye colour
- height
- weight
... and so on.
Objects
An object is a particular instance of a class, where each attribute has a specific value:
One object may have different values for its attributes than another object based on the same class.
For example:
- Mr. Gordon has red hair, green eyes, is 180 cm tall, and weighs 81.6 kilograms.
- Mr. Ross has blonde hair, blue eyes, et cetera.
In Alice, classes exist as 3D models in the gallery:
When a class is dragged from the gallery into a scene, that is when an object is created – an object is an instance of a class.
Generalizing and Sharing Procedures
In the Methods with Parameters tutorial you learned, in detail, how to generalize a procedure.
Mr. Gordon's solution to exercise 2 from the Moving Object Sub-joints tutorial looked like this:
The kicking action shown here would be useful in other contexts – with other instances of classes such as TeenPerson
, Person
, ElderPerson
, and so on.
So, we can generalize the procedure for kicking an object toward something.
Since classes inherit capabilities from their parent classes, if we create the new procedure on the Biped
class then all its subclasses will be able to use the new procedure:
Finally, we can export the procedure from one world, and import it into another world.
In this way, procedures written by one person in an Alice world can be used by any other person within their Alice worlds.
Let's look at how to do all of this, step by step.
If you would like to follow along with the next part of this lesson, you can download Mr. Gordon's "kicking code" here.
Generalizing the Kick Procedure
Here is what Mr. Gordon's code looked like immediately after he moved it into a procedure:
After Mr. Gordon generalized by adding parameters, here is what the procedure looked like instead:
this.adultPerson
was replaced withthis
this.soccerBall
was replaced withthisItem
this.soccerGoal
was replaced withtowardItem
Then, Mr. Gordon could replace the code he had in myFirstMethod
:
With this much shorter code instead:
Using the Kick Procedure in the Same World
Since the Biped
class now has a new ability, or procedure, named kick
:
... we can, for example, add a second instance of a subclass of Biped
and have that object kick another ball into the net:
.... which looks like this:
Note that we are not limited to just kicking a ball toward a net.
The kick
procedure allows us to pass an argument (answer) of type SProp
for the thisItem
parameter (question).
The same is true for the towardItem
parameter:
That means we could do the following:
... which looks like this:
Perhaps the barista has just had enough of serving people coffee! 🤷🏽♂️
Using the Kick Procedure in the Another World
Exporting a Procedure
Here is how to export a procedure for use in another Alice world:
- Switch to the class that holds the procedure.
- Choose Save to Class File...
- Save the .a3c file somewhere that you can find it.
That looks like this:
Importing a Procedure
In a different Alice world, we can import procedures written by other people.
So, for example, we could use the kick
procedure on an instance of Biped
to make an animation like this:
Here is how to import procedure(s) from a class file:
- Switch to the class that you want to import the procedure to.
This must match the class the procedure was exported from.
For example, the kick
procedure was exported from the Biped
class.
So, we must import the kick
procedure to the same class, Biped
, in the new Alice world.
-
Choose Add from Class File...
-
Find and select the file you want to import from, then choose Open.
You will then see an interface that lets you select what procedure(s) you want to import.
For example:
Choose Next.
-
You will see a window that lets you preview what the procedure does:
Choose Finish.
Now you will see the new procedure available for use with instances of the
Biped
class:
Here is what the complete import process looks like:
If you would like to try following along with the animation above to practice importing a class-level procedure, you can download this starter world.
Using the New Procedure
To use the procedure, we drag out the tile, and select arguments (answers) for the parameters (questions).
In this case:
- parameter
thisItem
is given the argumentthis.coconut
- parameter
towardItem
is given the argumentthis.fishingBoatCanopy
Exercise
You will now try importing and using a procedure written by Justin Hui, who took this course in 2021-22.
His procedure can be used on Biped
subclasses, and looks like this:
Justin documented how his procedure works, so that future users could determine if it would be suitable for their needs.
Download his class file, like this:
Then import his class file to an Alice world, as explained earlier.
Finally, use his procedure in your Alice world, and make a post on Notion to show that you know how to do all of this.