E-Prime Script Exercises Script Exercises (1- 10) Code for Experiments E-Prime 2002 Workshop Files on My Documents\My Experiments\Workshop\E-Prime Exercises Code 1 of 69 E-Prime Code Tutorials 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. Code Use of help facility. Use of Code to Set a Property. Program flow control. Use of Code to Set the Stimulus Conditional on a List Attribute. Using Debug.Print to Report Event Times and debugging. Use of code to provide cumulative data and exit block when five correct answers in a row. (Optional). Using code to read a digital IO bit and set digital bit (optional). Use of Onset Synch to control external hardware (optional). Use of loop to present frames (optional). Sample data from a list and delete repeats (optional). 2 of 69 Basic Steps to Writing Code 1. Determine what you want your code to do and at what point in the experimental procedure it is to occur. [e.g., if you want to set the stimulus to a random number from 1-499, “c.SetAttrib "Stim", Random(1,499)”] 2. Declare any variables that maintain information across multiple executions in the User Code area (display the Script window via the View menu, and select the User Tab). If the variables must be initialized to other than the default values, add an Inline object to the start of the experiment procedure for initialization (e.g., declare an array of real numbers Dim Word(10) As String) 3. Add Inline objects at the appropriate procedure (e.g., session, block, trial) and position in the procedure to execute the code, declare local variables not referenced by other objects, and enter your procedural code. 4. Reference any variables in the List objects (in the experimental context) with c.GetAttrib to get the values and c. SetAttrib to set the values of the variables. Set properties of objects (e.g, TextDisplay.x=0), or execute objects (e.g., TextDisplay.run). Query properties of other objects as needed (e.g., localvariable=Bitmap.RT). 5. Reference results of the code by using attributes in other E-Prime objects (e.g., add “[AttributeName]” to the text area of a TextDisplay object). 6. Include any debug related printing using the Debug.Print option to check data. In the View menu, select Output, and select the Debug tab. To report when things happened, add “Cstr(Clock.Read) to get the time in milliseconds since the beginning of the experiment. 7. Run the experiment, checking the debug output. Code 3 of 69 Exercise 1: Using E-Basic Online Help The are over 1000 pages of online help for EBasic. Use the help to find commands, learn their syntax, and get examples on how to use the commands. This short example will show how to find a command (Display.Rectangle) copy an example and run it and how to find things in the E-Basic Help. Code 4 of 69 Opening E-Basic Help 1. Open a new Experiment in E-Studio 2. Create an InLine object for later use in example to copy code to. Click and drag the Inline Object to the SessionProc 3. Open Help, Click the Help Menu select E-Basic Help. 4. Scroll through the list of commands, go to the Display:Graphics Code 5 of 69 Select Command, Examine Help 5. Click on “Canvas.Rectangle (method)” 6. Examine the help. Check the Syntax, Description, Comments, Example, and See Also Code 6 of 69 Copy Example From Help 7. Copy the example into the copy buffer Move cursor to beginning of ‘This sample.. Move cursor to end example Click and drag to the end of the example (it will reverse highlight the text) Copy the example with Ctrl-C or Right click/copy Code 7 of 69 Paste Example Into Experiment 8. Open the Inline object, paste (type Ctrl-V) the text into the inline object 9. Check that all lines copied completely, (note first and last lines should be green comment lines) 10. Run the example it should put up a green square Display Code 8 of 69 Modify Parameters 11. To learn parameters it is good to change some of the parameters to see if they do what is expected. Change: width w= 600 pen color to red pen width to 40 12. Rerun experiment Display Code The example is in the exercises with the file name #CodeExercise1-Help.es 9 of 69 E-Basic Help - Table of Contents The Contents allows you to browse the help. It is particularly helpful for identifying summaries of commands and families of commands. 13. Open E-Basic help 14. Select Help Topics 15. Click Contents tab, look at contents for alphabetic list of commands Code Note if you double click on EBasic and click Welcome you get the command summary Double click on other topics to expand the listings and browse the table of contents of the commands. 10 of 69 E-Basic Help - Index The Index allows you to find specific commands when you know the name of the command or function. 16. Use the Index to get the specific topic, Click Index, type Special Keys double click topic or press display 17. Look at special keys listing of how to enter keys such as the {ANY}. Scroll through to view List of special keys Code 11 of 69 E-Basic Help - Find The Find allows you to find any reference to a string. For example a parameter of a function may not be in the index but a Find will locate the help pages that include that string. 18. Select the Find Help option. 19. Search for the cresp – correct response designation. 20. Open up the specification. Code 12 of 69 Help Exercise Summary Use the online help. When needing a given type of command use the Contents under the Topic E-Basic Welcome. Use the Index for finding commands of interest when you know the name. Use the Find if the topic is not in the index but you know a related term. For new commands it is a good practice to copy the example and run it stand alone before trying to use it in new functions. Code 13 of 69 Code Exercise 1 Study Questions Could you at this point? 1. Use the Help to find how to declare an integer type variable? 2. Find the syntax and an example of the IF command? 3. Find a command that examines a command which would allow you to pick a member of a text string “red,green,blue,black” with an index to pick the correct word? 4. Determine how to exit a subroutine early? Short Answers are included in the Exercise Appendix Code 14 of 69 Code Exercise 2: Use of Code to Set a Property With code you can directly set properties of allowing great flexibility by setting and varying the stimuli. This is a simple example of adding code to display the stimuli in a random location along the x-axis. Code 15 of 69 Use of Code to Set a Property 1. Open #CodeExercise2-SetProperty.es (My Documents\My Experiments\Workshop\E-Prime Exercises\) 2. Run the experiment, notice the random placement of the stimuli on the screen Code 16 of 69 Edit StimPosition Code 3. Open the StimPosition Inline object and edit the program to randomly present stimuli along the y-axis Before After Code 17 of 69 Run And Test Program 4. Run and test the program 5. Save as CodeExample2.es Code 18 of 69 Looking Up Properties Of An Object 6. Examine the properties window of the Probe object in E-Studio. This illustrates most of the properties and how they are set If not open open the properties window Scroll through the properties Determine default value of the font size Code 19 of 69 Getting Help On Object Properties 7. You can get documentation on the properties either in the E-Prime Reference manual or the Help topics Open the Help topics Select Text Display Object Properties Window Code 20 of 69 Examine Properties Set FontSize 8. Examine the properties specification 9. Add a command Set the font size to vary in units of 20 points randomly from 20 to 100 points Code 21 of 69 Use Item Function to Choose Option 10. Setting a property with the Item function. Often there is a need to choose items from a set of items. This is illustrated by selecting the FontName Use Item$ function to choose the font from a list of items Add a command to randomly choose the FontName 11. Run the program examine font size and type Code 22 of 69 Setting Yes/No Explicit List Properties Some properties must be set, not as text but as on/off values or through calls to special functions. For on/off, the value of 1 and 0 are used. The ForeColor property is an example of a property that needs a function to covert names to values (e.g., red, green, blue) for the color. 11. Set the FontItalic function off/on by selecting 0/1 12. Select the color from a list, chose color function ccolor. The function coverts a string containing a color name to the red, green, blue values for the color. (If you click on the ForeColor properties you can see the list of color names). Run the program. Code 23 of 69 Setting 8 Properties of a Text Display 13. Examine this example. It sets 8 parameters representing 57600 (4x4x5x2x2x3x6x10) possible possible variations of the stimulus in 8 lines of code. (Note this is example #CodeExercise2-SetPropertyMulitpleParameters.es) Code 24 of 69 Code Exercise 2 Properties Study Questions Could you at this point set properties of objects? 1. Change the font to bold? 2. Change background colors of the display? 3. Set the X position of the Fixation display to be the same as the Probe? Short Answers are included in the Exercise Appendix Code 25 of 69 Exercise 3 Execution Control Execution flow control allows repetitive actions or conditional actions. In this example we will use a For statement for a repetitive action and an IF statement for conditional actions. If x > 10 For counter = Start to End Action False Action True Loop Action Code 26 of 69 Exercise 3 Execution Control Examine the code note the: For next loop IF conditions For counter = Start to End If x > 10 Action True If x > 10 Action True Loop Action Code The indenting shows the commands in the for loop. 27 of 69 Load and Execute 1. Open #CodeExercise3ProgramControl.es from My Documents\My Experiments\Workshop \E-Prime Exercises\. 2. Examine the InLine1 code. Note it is #CodeExercise1Help.es with a For and If statements. 3. Run the program see the moving box Code 28 of 69 Add an IF Then 4. Add the IF Else statements shown If begins the conditional execution Then is the true action Else the False Action End IF finishes the If Code Note the comments explain the mod function to get a remainder and the CColor to set the color 29 of 69 Run Program With If Then 5. Run the program. Observe the color change. 6. Change the maximum of the For and add ElseIf instead of Else for 3 states. Code 30 of 69 Run Program 7. Run program examine results. 8 Save program as CodeExercise3.ex Code 31 of 69 Code Exercise 3 Program Control Questions Could you at this point? 1. Add a loop to present the square in 2. 3. #CodeExercise3-ProgramControl to make the square move up for 1000 iterations, down and be a circle for 1000 iterations, and move horizontally and be an oval. Find other program control commands and implement a While…When, Do…Loop or Case process. Specify how to leave a For loop early (not covered in exercise but in lecture). Short Answers are included in the Exercise Appendix Code 32 of 69 Code Exercise 4. Use of Code to Set the Stimulus Conditional on a List Attribute This example uses the “Condition” variable to set the “Stim” variable to a random number and the “Resp” variable to the expected key. This also checks for errors in the typing of the attributes. Code 33 of 69 Open Code Examine Get Attribute 1. Open #CodeExercise4ListAttribute.es from My Documents\My Experiments\Workshop\EPrime Exercises\. 2. Examine the code and list and predict what will occur. Code 34 of 69 Run And Check for Error 3. Run the experiment Note you will see a message box appear on the last trial to indicate that a coding error has occurred, hit enter. This is intentional to illustrate self checking code. In this case the Bad Condition = bxlow indicates that a spelling error has been detected in the Condition. Code 35 of 69 Correct Errors 5. Open List1 and edit bxlow to below. Before Code After 36 of 69 Edit RandomNumberGenerate 6. Open RandomNumberGenerate and edit the conditions to select a number for below 1-4, above 6-9. Before After Code 37 of 69 Edit Instructions 7. Edit display instructions located on the Fixation display. Before After Code 38 of 69 Run and Test Program 8. Save as CodeExample4.es. 9. Run to test the program, note the random numbers selected are now between 1-9. Code 39 of 69 Code Exercise 4 Attributes Questions Could you at this point? 1. Use Code to set the attribute of font color based on the Condition? 2. Explain the difference in using code to set an attribute and setting an attribute and setting the property of the attribute using the [attribute] syntax? 3. Predict what would happen if an attribute is set multiple times in the same procedure? Short Answers are included in the Exercise Appendix Code 40 of 69 Exercise 5: Using Debug.Print to Report Event Times Use the Debug.Print command to write debug data during the running of the experiment to monitor when fast actions occur (e.g., OnsetTime of an object). Note an Object.OnsetTime records when an output event takes place. The results are shown in the Output window available on the View menu. Code 41 of 69 Run Debug.Print Exercise 1. Open #CodeExercise5-DebugPrint.es (My Documents\My Experiments\Workshop\E-Prime Exercises\). 2. Run the experiment, note the use of the RandomNumberGenerate code. Code 42 of 69 View Output Window 3. View Output window located at the bottom of the screen. Click on View menu and select Output. Code 43 of 69 Copy and Clear Debug Output 4. You can copy the output to the copy buffer with a right click in the window and copy. You can clear it also with the right click (otherwise the output is appended). Copy the output and paste it into Excel. This is useful for using Excel to check timing issues such as the time between stimuli. Select all the output with click start and drag to end Copy Clear the information 5. Open Excel Select first cell Paste the copied items Calculate onset time to onset time Onset time enter in Cell F1 = A2-A1 Code 44 of 69 Edit Debug.Print 6. Examine and edit DebugPrint note: The “\t” puts a tab in the string (which causes tab to new column in Excel spreadsheet in copy The & concatenates strings The _ at the end of a line continues a line Before 7. Add an output of a Probe.RT to report the response time in the output window. After Code 45 of 69 View Output Window 5. Run program and view Output window, note that the Prob.OnsetTime is now included in the Output window. Before After 6. Save as CodeExample4.es. Code 46 of 69 Code Exercise 5 Debug.Print Study Questions Could you at this point? 1. Output the Condition Attribute? 2. If you had a fast loop presenting stimuli every 14 ms how might you debug.print to see what the stimuli were? 3. If you had a 2 monitor computer and you ran the experiment on the primary monitor and the Output window on the second monitor, what do you think you would see during the run? Short Answers are included in the Exercise Appendix Code 47 of 69 Code Example 6. Use of code to provide cumulative data about the trial number to the subject Code that maintains static variables typically must be declared in the User script, initiated in the beginning of the experiment, and updated during the trial procedure. Code 48 of 69 Run Trial Counter Program 1. Open #CodeExercise6-CumulativeData.es (My Documents\My Experiments\Workshop\E-Prime Exercises\). 2. Run the experiment, note the TrialCounter on the Probe display. Code 49 of 69 Declare a Global Variable 3. Open the Script window via the View menu. 4. Declare variable in the User Script window. Before After Code 50 of 69 Initialize Variable 5. Initialize the number of correct responses in a row by setting the variable to 0 as was done for the total number of trials. Before After Code 51 of 69 Add Code to Check Corrects in Row And Set Count 6. Open the TotalTrialCounterCode 7. Set the Attribute TotalCurrentRun to the g_NTotalCorrectRun 8. Add Code that if g_nTotalCorrectRun >= 5 Then Exit Sub (This will exit the TrialProc whenever the number correct is 5 or more). Code 52 of 69 Add Display Of Corrects In A Row 7. Open Fixation object. Add Total Correct in Row [TotalCorrectRun] To show number correct in a row. Code 53 of 69 Calculate Correct Run 8. Add a InLine object after Probe 9. Rename It CalculateRunCorrects 10. Enter If statement, increment if correct, set to zero if wrong. Code 54 of 69 Run Program 10. Examine the structure. 11. Run to test the program. Watch the correct run conter Make 2 correct, then an error, then 5 in a correct in a row (the program should stop) 12. Save as CodeExample5.es. Code 55 of 69 Code Exercise 6 Cumulative Data Collection Could you at this point? 1. Add a cumulative trial counter informing the subject that this is trial [trialcounter] of the maximum number of trials? 2. Have a practice block where the task is easier and subjects continue till they reach 90% accuracy before moving on? Short Answers are included in the Exercise Appendix Code 56 of 69 Exercise 7. Port Input Output If you need to read a digital input output bit or write one that can be done by writing code to operate with a port command then program CodeExercise1PortIO.ex provides an example. It reads from a port displaying the contents and writes patterns of all bits on and off. This program requires special hardware to respond to the bits. The joystick can be used for input and printer port devices for input output. Code 57 of 69 Specification of Port Address 1. Load #CodeExercise7PortIO.es 2. Examine the declaration of the Port/IO Code 58 of 69 Reading a Digital Port 3. Examine code to read the port Code 59 of 69 Writing to a Digital Port 4. Examine code to write data to a port 5. Run the program if you have a device to show the data of the port read. For joy stick change the Codeport address code 60 of 69 Code Exercise 7 Port I/O Questions Could you at this point? 1. Turn on the lights differentially reporting accuracy green on correct and red on wrong? 2. Terminate a block when the black box button is pushed? 3. Output a string of bits one every 20 ms. Short Answers are included in the Exercise Appendix Code 61 of 69 Exercise 8 Optional - OnsetSignal Add OnsetSignalCondition code to signal the condition on the parallel Port to #BasicExercise1Lex.es or load #CodeExercise8OnsetSignal.ebs Note code in violet rectangle code is usually in initialization section Code 62 of 69 Code Exercise 8 Onset Synch Could you at this point? 1. Set the onset synch to report when the fixation occurred with a 1 code, when the stimulus occurred with a 2 code? 2. Change the data signal to reflect what stimulus was presented? Short Answers are included in the Exercise Appendix Code 63 of 69 Exercise 9 Optional - Loop 1. Load and run #CodeExercise9Loop.es 2. Change the Probe.x and Probe.y position after For command and rerun. Probe.x = 4 * counter + 10 Probe.y = 4 * counter + 10 Code 64 of 69 Code Exercise 9 Frame Loop Questions Could you at this point? 1. Add a fixation stimulus to the loop in #CodeExercise9Loop? 2. Add code to exit the loop when the subject makes a correct response? Short Answers are included in the Exercise Appendix Code 65 of 69 Exercise 10 (Optional) ReSampling This exercise uses code to resample a list to sample a list and reject any repeat conditions in the list. It uses a subroutine to do the sampling. To use a user defined subroutine it must be in the User section. This will manually force a sampling from the ConditionList putting all of those conditions in the context. If this is put in a loop (e g., While) this can sample skipping unwanted runs of the same condition. Code 66 of 69 Check Out Program 1. Load and run #CodeExercise10-Resample.es 2. Examine the code. Note use of LastCondition to verify no repeats. 3. Run the program note no runs Code 67 of 69 Examine ManualSample Subroutine 4. Examine the User code with the subroutine for sampling. Note this subroutine uses sophisticated techniques to pass in the list name. For now, just note that this code must be placed in the User code section so you can refer to the ManuallySampleList function. Code 68 of 69 Code Exercise 10 ReSampling Questions Could you at this point? 1. Modify #CodeExercise10-Resample to have four conditions and make sure there are no repetitions? 2. Copy the subroutine from #CodeExercise10Resample to another program so you could use the ManuallySampleList in another program. Short Answers are included in the Exercise Appendix Code 69 of 69