Howdy, Stranger!

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

Supported by

[open] Updating variables

edited December 2012 in OpenSesame

I feel I fail to do something very basic, namely, to create a variable and update its value during the experiment. I think I have figured out how to update the variable, but I am struggling to create the variable. Can anyone help?

I would like to do something along the following lines: (1) have the participant press a number key, (2) add the amount associated to the chosen key to a variable called 'CurrentValue', (3) display the value of this variable in a SketchPad, (4) save this value in the csv file.

The screenshot shows what I have built so far.

Image and video hosting by TinyPic

I have a sketchpad asking for a number, I had a keyboard item, I have inline code to update the variable, and I have another sketchpad to show the updated variable.

The error message I get is "Description: Variable 'CurrentValue' is not set in item 'feedbackTotal'. You are trying to use a variable that does not exist."

Comments

  • edited December 2012

    If you want a variable to be global (accessible anywhere in the experiment) you need to define the variable in the loop immediately after the experiment sequence above.

    I'm in class at the moment but can give you more detail later on this as I've recently done something similar.

    Boo.

  • edited December 2012

    As Boo says (edit: that's not really what he wrote, I see now, but it's still true), you can declare a variable global in order to access it from within other inline_scripts. However, if you want to use a variable as part of a sketchpad, or more generally whenever you want to use it with the [square brackets] notation, you have to use exp.set(). exp.set() will store the variable in a special way, so that it is recognized by OpenSesame as an experimental variable.

    Your error is simply that your variable does not have starting value. It's analogous to what happens when you type the following in a Python terminal (provided you have not used the name a before).

    a = a + 1

    Python will say

    NameError: name 'a' is not defined

    because you are using a to give a value, which is not logically possible. Does that make sense? You are doing the same thing, but using exp.set() and self.get():

    exp.set('CurrentValue', self.get('CurrentValue')+1)

    The solution is simply to give CurrentValue a starting value (such as 0) at the beginning of your experiment:

    exp.set('CurrentValue', 0)

    Cheers!

  • edited 11:47AM

    Actually, that rose was supposed to go to your response in another thread, but, I guess you can't take back a gift given :p

  • edited 11:47AM

    New problem: When I type in
    exp.set("CurrentValue",0)
    into the loop or experiment code and try to save it, it disappears.

  • edited 11:47AM

    Hi Frouke,

    The exp.set() statement is Python syntax, which you can use only in inline_script items. The OpenSesame script that you see when you click on the Edit script buttons uses a different, very simple syntax, which is described here.

    So I suspect that you are typing Python script where OpenSesame script is expected. If this doesn't help, could you perhaps provide some more details and explain what you are trying to do?

    Cheers!
    Sebastiaan

Sign In or Register to comment.