Logging a variable defined in inline_javascript
Hi everyone,
I'm just starting out with OpenSesame and, so far, have been able to customise my (OSWeb-friendly) experiment with some JavaScript. However, I seem to be running into an issue when it comes to logging the values of variables that I define in the inline_javascript environment. As an example, here is the trial routine of my experiment:
Basically, the participant will make a choice between two monetary options ('options' and 'keyboard_response') and I want to store the participant's earnings in an array , so that I can randomly select one of the earnings at the end of the experiment and pay bonus payment based on its value. Because this is a risky choice task (e.g. 100% probability of winning 10, or a 60% probability of winning 20), I need to calculate the actual earning (i.e., whether the 'risky' option yielded a reward of 20 or 0) with custom code, which happens in the 'earning_computation' component:
So far so good, the code seems to work because when I display the feedback with the actual earning ('feedback_risky/'feedback_safe' components), I call the 'earning' variable in the text via [earning] and the correct value is displayed. However, for some reason this value is not getting stored by OpenSesame - I added 'earning' into the 'logger' explicitly and I can see the variable in my output data. However, the value of this variable is always 0 across any and all trials. Moreover, I tried to define the value of the 'earning' variable at the beginning of the experiment, just to see that works:
But that doesn't cut it either, since the output data file still shows that 'earning' is 0 from the very first trial (despite me setting the value to 1 in the Prepare section above). My question is, am I doing something wrong here, or does OpenSesame simply not register the values of inline_javascript-defined variables during logging?
Comments
Hi,
Sounds weird. I guess you always end up in your else statement? Can you share your experiment, so i can have a closer look? What is the script earning_logging doing?
Eduard
Hi eduard, thank you for your quick reply! Please find attached the experiment file in this post.
It does seem as if the experiment ultimately always went to the else part, but the output of the 'earning' variable displayed in the feedback_... components is not always zero, indicating that the correct 'earning' value is passed on - it just doesn't seem to be getting logged correctly, for whatever reason
And the 'earning_logging' is just a script where I wanted to define the array that should store all the earnings (Prepare phase: vars.earning_array = new Array), and where I would append the current trial's earning value (Run phase: vars.earning_array.push(vars.earning) ). I would have incorporated it in the 'earning computation' script, but I will have multiple conditions/loops in my experiment and I only want to run this part of the script in certain ones, which is why I decided to keep it in a separate script (to control this via the routine 'runif' statements)
Hi @ErikS
I was just reading this thread and @eduard's comment about always ending up with the else statement, I had a look at your conditions in that if, else if, else structure. I did some testing and, indeed, this is what is happening. I'm not familiar with the
sometimes
function you're using in your code and couldn't find anything online. I can't see the function being declared in your code either. Could it be that the function is unknown and so the conditions are not met, and so you end up with the else statement?Fabrice.
Hi Erik,
Where do you run the experiment? In the browser or in the GUI? As far as I can see, the sometimes function is not defined when you run it in the GUI, but I don't find the log file when you execute it online in the browser. Do you happen to know where I can find the log file?
@Fab the sometimes function comes with Opensesame ;)
Eduard
Thank you very much for your answer, @Fab . The 'sometimes' function was listed on the OpenSesame website as one of the common functions (https://osdoc.cogsci.nl/3.3/manual/javascript/common/), so I assumed it would be safe to use - I thought it would generate a random variable between 0 and 1, and if it's higher than the p parameter, then the function returns TRUE, otherwise FALSE, so I wrote my if/else loops with this in mind. However, no matter how I rearranged the loops, the variable values would not get stored. So it could be that my assumption was wrong and that this function would need to be explicitly declared/I would need to load a package in order to use it (even though the OS website does not mention anything along those lines). Anyway, my initial problem is solved - I simply used Math.random instead and voila, everything works as intended! Again, thank you very much for helping me narrow down the issue, I appreciate it a lot
Hi @ErikS,
I didn't know the sometimes function. Just tried it out with a basic example and it does work within OpenSesame:
Not sure why it didn't work in the context of your experiment but I might have an idea... In the above example you can see that sometimes(.5) will be true half the time and false have the time (on average, that is). If think it it designed to be used by itself and that it can never be true in conjunction with another variable. I can't quite get my head around the exact logic underpinning my intuition, but I tested it out empirically with just one
inline_javascript
object as a task, nothing else:It basically runs a simple "if...then" loop 100 times using a two arguments condition. The first condition is that a="ok" (which it always is in this code), and the second uses the
sometimes
function. If you run it, you'll see that the condition is never met. In fact, even if you changesometimes(.5)
tosometimes(1)
, the condition is never true.So, conclusion: the sometimes function is indeed included in OpenSesame, but apparently designed to be used by itself (multiple argument conditions using it don't appear to ever be true).
So, you did well using the Math.random alternative!
Good luck with your experiment!
Fabrice.