Add duration to routines through code

Hello everyone,

My visual search task is coming along nicely, but one of the things I’d like to do is add duration to my routine. It consists of a 0.5ms fixation cross and the onto an indefinite Visual Search Task. Normally I’d just alter the duration in the builder, however I’ve created my distractors using code (see below), as this allows me to alter the number easily. What would be the simplest way to add duration to my distractors?

Kind regards,

Rob

Hi Rob,

I think you will first want to assign a custom attribute to each stimulus then monitor this against time when you draw it. So in your begin routine code you show there, before your append statement add t_TextLargeRed.offset = thisOffsetVal then when you draw the stimuli (in the eachframe tab) call something like:

for stim in stimList:
    if t < stim.offset:
        stim.draw()

Hope this helps!

Becca

Hi Becca,

Thank you for the advice, I’ll try it and see if I can get it to work, I may come back asking some some additional help!

Have a good weekend.

Kind regards,

Rob

Hi Becca, I’ve added your code as suggested however I’m getting this error.
if t < letter.offset:
AttributeError: ‘TextStim’ object has no attribute ‘offset’

I have added my code below, possibly I have coded something wrong! The first code block is me adding the offset value as you suggested, and the second is the loop in the eachframe tab.

After some googling, I understand what this means, I cannot apply an offset value to my stimuli within my list T_testlargeRed because they do not have any attribute that is called offset to change. This is slightly strange to me, as it had no issues with setting this before the experiment (see the first code block). Originally I tried adding the offset value to the list, however I got the same error as above. Maybe my TextStim type cannot have an offset value and I need to replicate the code used to create durations in the builder? I had hoped to avoid that as it looks complex

#code to create offset
for letter in T_textlargeRed:   # add offset value to stimuli
            letter.offset = 5
        T_textlargeRed.append(T_textR)
print(T_textlargeRed)

for letter in T_textlargeRed:
        letter.setAutoDraw(True)


#code for eachframe tab
for letter in T_textlargeRed:
    if t < letter.offset:
        letter.setAutoDraw(True)

Hi there, which tab did you put the first code snippet in where you assign offset to each letter? woudl you be able to share the .psyexp file?

Thanks!
Becca

Hi Becca,

I managed to solve the issue actually. I found some code snippets that added background music which served my purpose well, so instead of music I just played tones at certain frequencies. I think this was the simplest solution, than adding durations to my Visual Stimuli.

The original reason why I wanted to add duration to my distractors was so I could match a tone to their length, but then after some posts about adding stimuli that lasts longer than trials, someone informed me, and I should have realised this from the beginning, everything in a routine starts and stops in that routine and cannot be carried over, hence then using code snippets to create a tone than can last beyond just a singular trial!

Kind regards,

Rob