Howdy, Stranger!

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

Supported by

[solved] Saving text input variables in a loop for later use outside the loop

edited May 2013 in OpenSesame

Dear Sebastian.

I'm using Opensesame for an experiment in which I let participants play several economic games. (followed by a spatial cueing task).
Participants have to divide money, which occurs through a tekst form (saying: how much money do you want to keep for yourself). There are 4 rounds of games, and each round consists out of 4 different games, 2 of which use a tekst form. So it's a loop within a loop.

In the end, I want to add up all the money they decided to keep. In other words, I want to use the tekst_input variable set in the loop outside the loop. However, when I'm using a feedback form, the tekst_input variable seems to have vanished (or been reset).

So how can I use the tekst_input form's variables outside their specific loops? simply adding an inline script saying exp.get("relevant_variable") doesn't work.

Best
Wouter Wolf
Free University Amsterdam

Comments

  • edited May 2013

    I also realized that variables saved in a loop are a locally stored variables, so I tried to use:
    exp.items["SubmissiveDictator2Round1"].get("form_var").

    with SubmissiveDictator2Round1being the input_text form in which the relevant variable is created. But it didn't work.

    The loop is only there to randomize certain aspects of the game, but every round has its own tekst_input form (conditional of round number). So if it would just save the response variable globally once the problem would be solved, since the variable wouldn't be overwritten by a following trial.

  • edited 11:22PM

    Hi Wouter,

    Welcome to the forum!

    Several things come to mind when you say you can't use your form_text_input variables to provide participants with feedback.

    Firstly, did you rename the response variables in the form_text_input items? The response variable is called 'response' by default, but it's good practice to rename this into something more meaningful. And especially so when you use multiple form_text_input items in one sequence. Otherwise, every proceeding text input will overwrite the previous value. If you give your variables distinctive names, you won't have this problem.

    The name of the response variable can be changed here:

    image

    However, when I'm using a feedback form, the tekst_input variable seems to have vanished (or been reset).

    Just to clarify, do you mean a feedback item? The green icon with the hashtag? In that case, you should be able to display previously-defined (response) variables by using the square-bracket method, like so:

    image

    (given that you named the form_text_input variables 'response1', and 'response2', of course). Did you use this method? And did you place the feedback item somewhere after the text input is collected?

    For more information on providing feedback, please see:

    So how can I use the tekst_input form's variables outside their specific loops? simply adding an inline script saying exp.get("relevant_variable") doesn't work.

    Did you place your code in the Prepare phase tab of your inline_script item? And again, did you place the inline_script item somewhere after the text input is collected? Something like the following should work:

    # Use the experiment function self.get() to obain in-the-GUI-defined variables:
    amount1 = self.get("response1")
    amount2 = self.get("response2")
    
    # Calculate the total:
    total_amount = amount1 + amount2
    
    # Use the experiment function exp.set() to make an in-an-inline-script defined variable
    # availabe in the GUI as well:
    exp.set("total", total_amount)
    

    For more information about setting and getting variables, please see:

    I uploaded a working example experiment here (download, change the extension into '.opensesame' (instead of '.txt' and open as normally):

    Does this help?

    Best,

    Lotje

  • edited May 2013

    Hey Lotje,

    Thanks for the feedback, but my problems are a little more complex than that. :-)
    Working with feedback items is working just fine within the loop (I have several of them programmed there). The only problem is that when I use a feedback item after the loop, using a variable defined within the loop, it won't work.

    Maybe the best way to understand my problem is this:

    If you have an empty open sesame file, put a loop in the main (experiment) sequence, and add a variable to the loop called "Round". Set the loop to 2 cycles: in the first cycle variable "Round" = 1, in the second cycle variable "Round" = 2.
    This loop should already contain a sequence (to be clear, call that sequence "loopseq"). Place two sequences in loopseq, and make the two sequences ("sequence1" and "sequence2") conditional on the variable "Round": so for sequence1 "Round"=1, and for sequence2 "Round"=2.
    Now put a tekst_input file in both sequences. Call the response variable of the tekst form in sequence1 "var1" and the response variable of the tekst form in sequence2 "var2".

    As long as the feedback items are within the loop there is no problem. However, if you put a new sequence after the loop (so in the primary experiment sequence), and put a feedback item containing [var1] and [var2] in that sequence, opensesame gives an error, saying that it is unable to find variable [var1].

    I think the problem is that opensesame somehow resets the variable once the loop starts over: as long as I remain within a loop cycle I can use the variable [var1], however, when the loop starts over (this time running sequence2) [var1] is no longer available.

    Do you know what I should do to make [var1] available outside its own loop cycle?

    Best
    Wouter

  • edited May 2013

    Hi Wouter,

    Working with feedback items is working just fine within the loop (I have several of them programmed there). The only problem is that when I use a feedback item after the loop, using a variable defined within the loop, it won't work.

    In the example experiment I uploaded I'm doing exactly that: I use in-the-previous-sequence-defined variables in both an inline_script item and a feedback item:

    image

    Note that this strategy only works with feedback items and Prepare phase tabs of inline_script items because, in contrast to for example form items and sketchpad items, they are constructed at the moment that they are shown.

    For more information about the prepare-run strategy OpenSesame uses, please read this article:

    I think the problem is that opensesame somehow resets the variable once the loop starts over: as long as I remain within a loop cycle I can use the variable [var1], however, when the loop starts over (this time running sequence2) [var1] is no longer available.

    OpenSesame does not reset or delete any variables, and in-a-given-loop defined variables are not local in the sense that they are not available outside that loop. As soon as a variable (e.g. 'text_input1' is created somewhere during the experiment, its value (e.g. the text input '10') remains available until it's overwritten by the text input (e.g. '5') on the next occurrence.

    However, because of your specific sequences-within-a-sequence structure

    image

    it seems that the following is going on:

    • Even though you use Run-if statements to run only one of the two sequences, both are prepared anyway (see the article mentioned above for more information).
    • During this preparation phase, the text_input_form contains no text yet (because the default text to start with is an empty string, ''), and for one of the two forms it remains that way (because the item is not run).
    • The text_input_form item uses the text it contains as the response variable (in your case 'var1' or 'var2'). In one of the items, the response variable will therefore contain the input text given by the participant (e.g. '10'). The value of the other variable will remain ''.
    • As a consequence, your feedback item will show one empty string and one 'real' string.
    • You mention that "opensesame gives an error, saying that it is unable to find variable [var1]". Because of the prepare-run strategy mentioned before, this cannot happen with a feedback item if it is placed somewhere below your form_text_ input items.

    As a solution to the empty-string problem, I would simply advise you to place the form_text_input items in a loop item rather than in a sequence item. A loop item is not prepared in advance, which is particularly convenient in situations such as yours (again, see the above-mentioned article). So I think changing your experimental structure into this:

    image

    will solve your problem.

    For a new example experiment, see:

    Does this make sense? Don't hesitate to post further questions if something remains unclear.

    Best,

    Lotje

  • edited 11:22PM

    Hey Lotje,

    Thanks, that seems to be what is going on.
    The only problem now is that the games (which would be l1 and l2 in your screen) consist out of multiple screens including sketchpads, tekst forms, and feedback items. This is necessary because it is kind of an interactive design.

    Correct me if I'm wrong, but a loop without a sequence in it can only consist out 1 item right? So can I still use this trick by keeping my sequences of items intact, but replace the tekst input items by loops, containing these tekst input items.

    So for example: suppose I'd have loopsequence with s1 and s2 (just like your first screenshot) and s1 and s2 have for example a sketchpad (sp1/sp2), a tekst input (ti1/ti2)and a feedback item (fb1/fb2). Can I simply replace ti1/ti2 by l1 containing ti1 and l2 containing ti2?

    Thanks in advance!

    Wouter

  • edited May 2013

    Hi Wouter,

    a loop without a sequence in it can only consist out 1 item right?

    Yes, you're absolutely right! So perhaps what you can do is wrap the two loop items around two (or whatever number of different games you have) new sequence items rather than a single form_text_input item (as in the previous example). As long as one of the loop items is not run (depending on the Run-if condition), the potential empty-string problem caused by the sequence within this loop will not occur either.

    Does that make sense?

    So, the new structure will look something like this:

    image

    The new experiment is uploaded here:

    Best wishes,

    Lotje

  • edited 11:22PM

    Yes, now it works.

    Thanks a lot for your feedback!

    Best
    Wouter

Sign In or Register to comment.