# Mwel help - range\_replicator and stimulus\_group

**URL:** https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068
**Category:** Support
**Created:** [December 23, 2024, 11:37pm UTC](https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068 "2024-12-23T23:37:29Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![skmar](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@skmar](https://mworks.discourse.group/u/skmar)
#### Post date: [December 23, 2024, 11:37pm UTC](https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068/1 "2024-12-23T23:37:29Z")

</div>

Hi Chris,

I was wondering if I could get your help understanding some behavior of my mwel code (attached).  
For context, I’m new to mwel (trying to re-code my task, previously coded in Python (moog) to run purely with mwel). The code isn’t finished, I’ve just implemented the trial initialization so far.

I defined the tubes, occluders, and balls stimulus groups using a range\_replicator (all reusing the local variable ‘i’, which I declared once on line 47).  
In the protocol state ‘Initial’, I then queue the stimuli using while loops to iterate through the individual stimuli in the group.  
This code works as expected, but when I replace the while loop on lines 171-175 with:  
queue\_stimulus(tubes[0])  
queue\_stimulus(tubes[1])  
the behavior is different, specifically the tubes are drawn with the wrong x position.

Why does that happen? I don’t think I understand the order of events regarding the stimulus\_group definition and then queuing and displaying. Does it have something to do with how I’m re-using the index i? How can I correct my code so that later if I want to index individual stimuli within the group (e.g. dequeue just one stimulus), the behavior is as expected?

Thanks for your help, and happy holidays!  
Sol

> **(attachments)**
>
> [balldrop.mwel](https://mworks.discourse.group/uploads/short-url/nBOtU2EbjIU0SuqVKvAcyrn71On.mwel) (6.07 KB)

---

<div class="post-metadata">

### Author: ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)
#### Post date: [December 24, 2024, 10:06pm UTC](https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068/2 "2024-12-24T22:06:25Z")

</div>



---

<div class="post-metadata">

### Author: ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)
#### Post date: [December 24, 2024, 10:06pm UTC](https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068/3 "2024-12-24T22:06:37Z")

</div>



---

<div class="post-metadata">

### Author: ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)
#### Post date: [December 24, 2024, 10:17pm UTC](https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068/4 "2024-12-24T22:17:14Z")

</div>

Hi Sol,

In the expression defining each `x_position` parameter, you need to put `$` before `i`, e.g.

```
x_position = (2 * $i - 1) * display_width / 6 

```

`${i}` also works. If you just use plain `i`, it won’t be replaced by the replicator, and the stimulus will instead use the _run time_ value of `i`. Since `i` is a local variable, the value it uses will probably be zero.

FYI, you really should use MWorks’ local variables _only_ as the `variable` parameter of a replicator. They don’t work like local variables in programming languages and are generally not very useful. So, instead of using `i` as the loop index for `while`, define another, normal variable for that purpose.

Cheers,  
Chris

---

<div class="post-metadata">

### Author: ![skmar](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@skmar](https://mworks.discourse.group/u/skmar)
#### Post date: [December 24, 2024, 10:34pm UTC](https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068/5 "2024-12-24T22:34:31Z")

</div>

Thanks Chris!

Best,  
Sol

---

<div class="post-metadata">

### Author: ![cstawarz](https://yyz1.discourse-cdn.com/flex031/user_avatar/mworks.discourse.group/cstawarz/32/3_2.png) [@cstawarz](https://mworks.discourse.group/u/cstawarz)
#### Post date: [January 2, 2025, 1:56pm UTC](https://mworks.discourse.group/t/mwel-help-range-replicator-and-stimulus-group/1068/6 "2025-01-02T13:56:02Z")

</div>


