Modifying default scripts
Now, let's put some finishing touches on the generated scripts:
1. Select finishTime from the list of scripts. Notice that it is a get selector that answers the time the runner finished the race.
2. Select number from the list of scripts. Notice that it is a get selector that answers the number assigned to the runner for the race.
Add two lines to the script so that it looks like the following:
number
"Return the value of number."
number == nil
ifTrue:[ self number: ''].
^number
Select Save from the pop-up menu on the window where your script is displayed.
This is a common way of initializing a part's attribute. When a part is used inside a visual part, its attributes are initially nil. The scripts that reference the attribute usually expect the object not to be nil. This initialization technique is convenient to prevent errors and is generally easier than checking for nil values each time you reference the attribute.
Notice that the initialization technique uses the number: set selector to set the attribute initially to an empty string.
In your scripts, be sure to use an attribute's get selector rather than directly accessing the instance variable. For example, use self number rather than number, because the former will initialize itself if necessary and never evaluate to nil, and the latter will simply evaluate to nil if the instance variable is nil. Also, using an attribute's get selector enables VA Smalltalk to notify other parts that the attribute has changed.
3. Select finishTime: from the list of scripts. Notice that it is a set selector that stores the time that the runner finished the race in the finishTime instance variable. Also, notice that the finishTime event is signaled with aTime as an argument. This tells VA Smalltalk to inform other parts that the finishTime attribute has changed to aTime.
Add two lines to the script so that it looks like the following:
finishTime: aTime
"Save the value of finishTime."
finishTime := aTime.
self signalEvent: #finishTime
with: aTime.
self signalEvent: #runnerChanged
with: self.
The lines that you added ensure that the runnerChanged event gets signaled every time the runner's finish time changes. Because runnerChanged is the Change event symbol for the derived attribute asResultString, the get selector for asResultString is run every time this event is signaled.
4. Make the previous change for the number: script also. Select number: from the list of scripts and add the same two lines to the end of the script. This ensures that the runnerChanged event gets signaled every time the runner's number changes.
We are using the runnerChanged event as a signal that says the entire runner object has changed. Often, you will change the set selector of your parts to signal more than one event, as we have done. Notice that the argument passed with the runnerChanged event is self, indicating that the entire part has changed.
Last modified date: 07/15/2020