Assigning dictionary values to variables makes the values undefined

I have a dictionary x_positions containing the x positions of various visual stimuli, and then tried to set the positions by adding a custom code component to the trial with the code ObjectX= x_positions["ObjectX"]; in the Begin Routine tab, and then putting ObjectX in the Position [x,y] box of my object (stimulus). However, when I tried to run it online both the value of ObjectX, and the value inside the dictionary x_positions["ObjectX"] were undefined. Weirdly, it would already be undefined at the time that I load the dictionary (in a routine that runs before the trial). If I remove ObjectX= x_positions["ObjectX"]; and re-run, then the dictionary values go back to normal.

My workaround was to not define the ObjectX variable, and instead directly put x_positions["infoHX"] into the Position [x,y] box of my object. But I would like to understand what causes this behaviour, could anyone explain?

I suspect that you needed to put ObjectX = in Begin Experiment (or Begin Routine before you set it) to define it with global scope.