k4kfh / ZephyrCab

Drive a model train with prototypical controls, realistic behavior, and simulated physics; uses JMRI's WebSockets interface for layout connection
http://k4kfh.github.io/ZephyrCab
GNU Affero General Public License v3.0
10 stars 4 forks source link

The elements of the train.all array are undefined at first for sim.js #11

Closed k4kfh closed 8 years ago

k4kfh commented 8 years ago

sim.js is saying that .prototype is undefined. So somehow either i is getting to be an improper number, or sim.js is beginning its cycles before the train.all array is constructed. It's not a catastrophic bug from what I can tell, since it seems to fix itself after one cycle, so I'll deal with it when I can.

k4kfh commented 8 years ago

Upon further testing, I found this has nothing to do with the train.all object itself. Rather, i in sim.accel is getting screwed up somehow. Before the error occurs, i becomes 29 for some reason, which obviously returns undefined in an array with only a few elements.

I never did completely narrow down the source of the i = 29 problem, but I discovered a solution. In the initial declaration of i (in the beginning of the for loop), if I add the var keyword to define it only in that scope, the problem disappears and everything works exactly as normal. So line 78 of sim.js becomes this:

for(var i = 0; i < train.all.length; i++) {

I tested this pretty thoroughly, and the problem seems to be completely gone. All the DCC interfaces work totally as normal, all the previously functional dash gauges work (including RPM, which, since the problem occurred during ignition, is a good sign that nothing is broken as a result of my solution). I'm going to go ahead and close this issue after I commit that change.