[open] Updating variables
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.
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
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.
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).
Python will say
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!
Check out SigmundAI.eu for our OpenSesame AI assistant!
Actually, that rose was supposed to go to your response in another thread, but, I guess you can't take back a gift given
New problem: When I type in
exp.set("CurrentValue",0)
into the loop or experiment code and try to save it, it disappears.
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
Check out SigmundAI.eu for our OpenSesame AI assistant!