Creating a variable equal to sum of other variables
Hello!
I'm creating a contribution task where people have to react to contributions of others to reach a certain threshold. If they reach it, they go to a screen saying they win. If not, they go to another screen saying they lose.
I'm trying to create a variable that takes the value (total+contribution) to put a condition on the display of both screens. But I don't know how to create that in the loop element.
I would really appreciate any help. Thanks in advance!
Melie
Comments
Hi Melie,
I don't quite get where these variables are coming from. Could you elaborate more?
Generally though it is rather easy. Under some circumstances you can do the arithmetics directly in the Opensesame items (e.g. on the sketchpad, or in run_if fields). Something like
{total} + {contribution}might do the trick (see here)Apart from that you can also use an
inline_script, in which you can write Python code that is evaluated every time that the experiment passes by that particularinline_script. In it you can compute new variables rather easily:e.g. total = total + contribution`
Which would update the variable total from then on (careful, the variable needs to be initialized earlier to make this work!)
Sorry if this advice is too general, but without more info on what you want to achieve, I can't give you more detailed help.
Eduard
Hello Eduard,
Thanks for your answer. Here are a little more details about my problem.
My participants will "play" against 5 other players, which are fake, I pre-entered their contributions.
The variable {total} is the total contribution of the 5 fake players (again with values pre-entered, I do not use a sum for these ones).
Then participants say how much they want to contribute in a text_form, creating the variable {contribution}.
(Here is a piece of my variables table)
And for the different screens, I did put conditions as follow:
And it works with OpenSesame, but when run with OSWeb the condition is not taken into account. Whatever contribution the participant put, it always goes to the winning screen and never shows the losing one.
I tried with a simplier condition (contribution < 5 for example) and it works with OSWeb. So the problem seems to come from the use of the sum. So I'm trying to bypass it. Hence, creating a variable (ex: {win}) that takes the value total+contribution before the screens are to been shown, in order to write the condition only with it.
And I tried several things to create it, but nothing works (mostly because I'm not sure how to do it): I tried to directly put the formula in the variable table in the loop element but it doesn't work. And for the inline_script, I tried to put it at the end of the text_form, but the line automatically goes at the beginning and then it is not working (I'm probably doing it wrong...).
I don't know if you would need more details?
Melie
Hi Melie,
First of all, you need to use feedback items not sketchpads for the win/lose presentation. Otherwise the information on the participant contribution is not available (see the prepare-run strategy). Not sure, but this might already help with the issue.
If not you can insert an inline_javascript item after the text input and update the total value with the contribution or create a new variable that you can use in the run in field. See here for how to use javascript code in Opensesame: https://osdoc.cogsci.nl/4.0/manual/javascript/about/
Eduard
Hello Eduard,
Thank you for your help. My problem has been solved elsewhere. Apparently it went for the fact that my variable "contribution", been created through text_form, was actually taken as a text and not as a number (makes sense...). So I could deal with the problem by adding a line of code before the win/lose screens.
But I will take a look at what you sent for the next time.
Thank you for your time,
Melie