Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

[solved] Here's an example of code that only works in the prepare phase (Why is this?)

edited October 2013 in OpenSesame

Greetings.

The script below doesn't work. However, if I move

 exp.set('TimeAtSessionStart',self.time())

from the run phase to the prepare phase, or even if a copy it so that it's in both the prepare and run phase, then it works. Can anyone provide clarification on why this is, and what the more general implications might be? Thanks.

Richard

# Generated by OpenSesame 0.27.4 (Frisky Freud)
# Mon Oct 14 13:56:19 2013 (nt)
# <http://www.cogsci.nl/opensesame>

set foreground "white"
set subject_parity "even"
set description "Default description"
set title "New experiment"
set sampler_backend "legacy"
set coordinates "relative"
set height "768"
set mouse_backend "xpyriment"
set width "1024"
set compensation "0"
set keyboard_backend "legacy"
set background "black"
set subject_nr "0"
set canvas_backend "xpyriment"
set start "experiment"
set synth_backend "legacy"

define inline_script inline_script
    set _run "exp.set('TimeAtSessionStart',self.time())"
    set _prepare ""
    set description "Executes Python code"

define sequence experiment
    run inline_script "always"
    run welcome "always"

define sketchpad welcome
    set duration "keypress"
    set start_response_interval "no"
    set description "Displays stimuli"
    draw textline 0 0 "Time:  [TimeAtSessionStart]" center=1 color=white font_family="serif" font_size=32 font_italic=no font_bold=no show_if="always" html="yes"

R

Comments

  • edited 2:09PM

    Hi Richard,

    This is because a sketchpad's canvas is constructed during the prepare phase, which occurs before the run phase. Therefore, the contents of a sketchpad cannot depend on variables that are set during the run phase. In your case, the order of events is as follows:

    • Run sequence experiment
      • Prepare inline_script
      • Prepare welcome sketchpad (i.e. create the canvas)
      • Run inline_script
      • Run welcome sketchpad (i.e. show the canvas)

    Does that clear things up?

    The prepare-run strategy is described in more detail here:

    Cheers,
    Sebastiaan

  • edited October 2013

    Thanks Sebastiaan,

    This helps some, but I think I'm still missing a concept. The slightly expanded script, below, properly includes the exp.set function in the prepare rather than the run phase of "inline_script". However, for "inline_script_in_loop", the same exp.set command is used in the run rather than in the prepare phase, and yet the entire script works. (Moreover, the script seems to work the same way whether inline_script_in_loop includes the exp.set command in the prepare or the run phase). With respect to sketchpad usage, it appears exp.set does work in the run phase, but only if exp.set has also been used previously in the prepare phase. But I don't have a sufficient grasp of the prepare/run architecture to understand why this is.

    # Generated by OpenSesame 0.27.4 (Frisky Freud)
    # Wed Oct 16 03:15:19 2013 (nt)
    # <http://www.cogsci.nl/opensesame>
    
    set foreground "white"
    set subject_parity "even"
    set description "Default description"
    set title "New experiment"
    set compensation "0"
    set coordinates "relative"
    set height "768"
    set mouse_backend "xpyriment"
    set width "1024"
    set sampler_backend "legacy"
    set keyboard_backend "legacy"
    set background "black"
    set subject_nr "0"
    set canvas_backend "xpyriment"
    set start "experiment"
    set synth_backend "legacy"
    
    define sequence sequence
        run inline_script_in_loop "always"
        run message_in_loop "always"
    
    define sketchpad message_in_loop
        set duration "keypress"
        set description "Displays stimuli"
        draw textline 0 0 "Looped Time:  [TimeNow]" center=1 color=white font_family="mono" font_size=18 font_italic=no font_bold=no show_if="always" html="yes"
    
    define inline_script inline_script_in_loop
        set _run "exp.set('TimeNow',self.time())"
        set _prepare ""
        set description "Executes Python code"
    
    define sequence experiment
        run inline_script "always"
        run message "always"
        run loop "always"
    
    define inline_script inline_script
        set _run ""
        set _prepare "exp.set('TimeNow',self.time())"
        set description "Executes Python code"
    
    define sketchpad message
        set duration "keypress"
        set start_response_interval "no"
        set description "Displays stimuli"
        draw textline 0 0 "Time:  [TimeNow]" center=1 color=white font_family="serif" font_size=32 font_italic=no font_bold=no show_if="always" html="yes"
    
    define loop loop
        set repeat "1"
        set description "Repeatedly runs another item"
        set item "sequence"
        set column_order ""
        set cycles "5"
        set order "random"
        run sequence
    
    

    -- Richard

    R

  • edited 2:09PM

    Hi Richard,

    Once you have set a variable, it will remain set. (Unless you call exp.unset(), but that is rare.) But even though you therefore don't get a 'Variable [x] is not set' error, the behavior of your script is probably not what you want. More specifically, you will see that the timestamp shown in the message_in_loop sketchpad always lags behind by one cycle. This is because, again, the exp.set() statement is in the run phase of inline_script_in_loop and is therefore executed after message_in_loop has been prepared.

    Does that make sense? It can be a bit tricky, but if you have a hard time following the order of events, try to draw them out for yourself following the simple rule: Within a single sequence all items are first prepared and then run.

    Cheers,
    Sebastiaan

  • edited 2:09PM

    Yes. This makes sense now. Thank you.

    R

Sign In or Register to comment.