[solved] Here's an example of code that only works in the prepare phase (Why is this?)
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
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 asketchpad
cannot depend on variables that are set during the run phase. In your case, the order of events is as follows:Does that clear things up?
The prepare-run strategy is described in more detail here:
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
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.
-- Richard
R
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_loopsketchpad
always lags behind by one cycle. This is because, again, theexp.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
Check out SigmundAI.eu for our OpenSesame AI assistant!
Yes. This makes sense now. Thank you.
R