JavaScript global variables are not reset after the patch is reinitialized. Is this a bug? Example included.

Sonoran Music Devices's icon

Per the title, below is a simple patch to demonstrate.

When I reinitialize the patch (Edit->Reinitialize), the global variable, id is not reset. See the reproduction steps below.

Questions:

  1. Is this a bug?

  2. Is deleting and re-adding a M4L device the only way to reset global variables?

Steps to reproduce:

  1. Add the below device to a new Live set.

  2. Ensure the js object references the below JavaScript.

  3. Open the M4L device.

  4. In the console, notice the printout of the id variable before and after it is set. Before, it is -1. After, it is 0.

  5. Reinitialize the patch.

Expected results:

In the console, before id == -1, and after id == 0.

Actual results.

In the console, before id == -0, and after id ==0.

Clearly, the global variable is not being reset after reinitializing.

Here is the device code:

Max Patch
Copy patch and select New From Clipboard in Max.

Here is the JavaScript code.

autowatch = 1;
var id = -1;
function bang()
{
post("Before id: " + id + "\n");
id = 0;
post("After id: " + id + "\n");
}

tyler mazaika's icon

1) I don't know what the expected behavior is. Are you doing your edits via the device edit button in Live, or are you working in Max and then exporting to (overwriting) a .amxd that is currently used in the Live set? I very early on started doing the latter, and I suspect it may sidestep such issues.

2) You may be able to just send "compile" to the JS script.

Sonoran Music Devices's icon

1) Are you doing your edits via the device edit button in Live

Yes, I do not have a full Max license.

2) You may be able to just send "compile" to the JS script.

That appears to work when I send the js script a bang from live.thisdevice and a compile from a separate message object. At first, I was concerned about which message would arrive first, but then I remembered the message order tutorial I did last week, which states the below. So, as long as I arrange my objects properly, I assume I will get consistent behavior. Nice.

To sumarize, the message-ordering rules in Max are:
1. Right-to-left, or bottom-to-top for objects that are vertically aligned.
2. All actions on a branch are completed before the next branch is activated.

Note that, for determining the right-to-left or bottom-to-top ordering, it is the location of the connected inlet, not the path of the patchcords, that determines the message route.

Thanks for your help!