Howdy, Stranger!

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

Supported by

[solved] passing an inline script variable to a sketchpad

edited February 2016 in OpenSesame

I'm very new to both OS and Python and I'm trying to code a spatial cuing experiment with four possible cue/target locations. I define a cue and target position randomly on each trial (with some restrictions determined by the "val" variable) with the following inline script appearing as the first item in the trial sequence:

import random
positions = list(range(4))
random.shuffle(positions)
var.target_pos = positions[0]
if var.val == 1:
    var.cue_pos = var.target_pos
else:
    var.cue_pos = positions[1]
xcoord = [160, 160, -160, -160]
ycoord = [-160, 160, 160, -160]
trialxcoord = xcoord[var.cue_pos]
trialycoord = ycoord[var.target_pos]

That works great at selecting the appropriate x,y coordinates as confirmed when I print the variables in the debug window. However, when I plug trialxcoord and trialycoord into a subsequent sketchpad script like this:

draw image center=1 file="cue.png" scale=1.5 show_if=always x=trialxcoord y=trialycoord z_index=0

I get the following error:

TypeError: coercing to Unicode: need string or buffer, int found

I'm sure I'm doing something incredibly stupid/basic, but any help would be appreciated!

Comments

  • edited February 2016

    Hi Chip,

    To indicate that a string is a variable name, rather than a literal string, you put square brackets around it. Like so:

    draw image center=1 file="cue.png" scale=1.5 show_if=always x=[trialxcoord] y=[trialycoord] z_index=0
    

    In addition, this only works for experimental variables, that is, the ones that are prefixed with var. in Python code. So this doesn't work:

    trialxcoord = xcoord[var.cue_pos]
    trialycoord = ycoord[var.target_pos]
    

    ... But this would:

    var.trialxcoord = xcoord[var.cue_pos]
    var.trialycoord = ycoord[var.target_pos]
    

    Does that make sense?

    See also:

    Cheers,
    Sebastiaan

    PS. We should really add a contingent-capture experiment to the examples. ;-)

  • edited 1:40PM

    Hi Sebastiaan,
    Thanks for the quick response . . . that fixed it! I knew it had to be something about the syntax. And yes, adding a contingent capture example would be nice (although you should check with the boss first) ;) Great program btw!!!!

    Thanks again,
    Chip

Sign In or Register to comment.