Couldn't add trials mid-experiment #158
-
| Hi, My experiment has a 'training' phase that only ends once the participant has done a few things correctly. I was trying to add more trials on the fly based on one of these conditions (i.e., "rescued_A" == false) but ran into a problem -- I was only able to add one extra trial to the block. When I tried to add another trial after that, I got this error message on Unity instead: "KeyNotFoundException: The given key 'rescued_A' was not present in the dictionary." In summary: The results for 'rescued_A' was logged properly in the results csv file so I can't figure out what went wrong as I copied your code directly from the Wiki: public class AddTrialsOnTheFly : MonoBehaviour
{
    public void AddMoreTrials(Trial trial)
    {    
        bool rescued_A = (bool)trial.result["rescued_A"];
        if (rescued_A == false)
        {
            Trial newTrial = trial.block.CreateTrial();
            newTrial.settings.SetValue("blockType", "bright");
            newTrial.Begin();
        }
        
        else
        {
        
            if (trial == trial.session.LastTrial)
            {
                trial.session.End();
            }
        else
            {
                trial.session.NextTrial.Begin();
            }
        }
    }
}Please could you help me figure out what is wrong? Thank you so much! | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
| Hi, could it be that your method  bool rescued_A = (bool)trial.result["rescued_A"];Will fail because      public void AddMoreTrials(Trial trial)
    {    
        if (trial.number >= 1) return; // skip if beyond trial 1
        bool rescued_A = (bool)trial.result["rescued_A"];
        if (rescued_A == false)
        { | 
Beta Was this translation helpful? Give feedback.

Hi again,
I still couldn't get it to work properly using the simpler implementation because it wouldn't retrieve
trial.result. I tried to find out if my other scripts were causing the problem but it was taking up too much of my time so I kinda gave up on that.However, I have made it work using the coroutines and I also found out what caused the trial skipping problem after adding trials. Turns out that I forgot to remove
BeginNextTrialSafefrom the inspector. Since I also have it in my script, it's being called more than once, causing the trials to skip ahead.Thank you again for your time :)