Checking elements of an array in the "if" operator
-
Hi! I want to check whether the variables "currentSampleMapId1" or "currentSampleMapId2" contain any of the elements from the "PadSampleMaps" array. If they do, a panel should be displayed. However, my code doesn't seem to work.
const var PadSampleMaps = ["Pad 1", "Pad 2", "Pad 3", "Pad 4"]; const var pnlKeyboard1 = Content.getComponent("pnlKeyboard1"); const var currentSampleMapId1 = Sampler1.getCurrentSampleMapId(); const var currentSampleMapId2 = Sampler2.getCurrentSampleMapId(); inline function updateKeyboardPanelVisibility() { if (value) { currentSampleMapId1.contains(PadSampleMaps) || currentSampleMapId1.contains(PadSampleMaps); pnlKeyboard1.showControl(true); } else { pnlKeyboard1.showControl(false); } }
-
@bendurso You can't check if a string contains an array. So you need to use a loop, or convert the array to a string and use regex.
Can you give me an example of one of your sample map names?
-
@d-healey Oh, my sample maps are named like "Pad 1", "Pad 2", and so.
-
@bendurso In that case you just need to check if the array contains the sample map id. So the inverse of what you've done already.
-
@d-healey Like this?
inline function updateKeyboardPanelVisibility() { if (value) { PadSampleMaps.contains(currentSampleMapId1) || PadSampleMaps.contains(currentSampleMapId2); pnlKeyboard1.showControl(true); } else { pnlKeyboard1.showControl(false); } }
For some reason is not working. I'm calling the function updateKeyboardPanelVisibility() inside the Sample Map Selector too.
-
Well you're just checking
if (value)
which is the same as sayingif the value is 1 do whatever is in the curly braces
. But I don't think this is what you want and I don't even see wherevalue
is coming from. -
@d-healey Oh, yes, my code was weird.
Something like this maybe should check if PadSampleMaps contains currentSampleMapId1 or 2? It doesn't work :(
inline function updateKeyboardPanelVisibility() { if (PadSampleMaps.contains(currentSampleMapId1) || PadSampleMaps.contains(currentSampleMap2)) { pnlKeyboard1.showControl(true); } else { pnlKeyboard1.showControl(false); } }
-
@bendurso What is the exact value of
currentSampleMapId1
? -
@d-healey Is the ID of the SampleMap. The console print is showing it correctly.
const var currentSampleMapId1 = Sampler1.getCurrentSampleMapId();
-
@bendurso Yeah but is it "Pad 1" or "Pad 1.xml" or something different?
-
@d-healey Oh, it's just "Pad 1".
I just noticed that when I hit "compile" the panel shows or hides as it should. But it doesn't happen when I change the samplemap. I think placing "updateKeyboardPanelVisibility()" inside the samplemap selectors doesn't work because they can't check each other's state.
-
@bendurso Yeah you'll need to trigger the function from a callback or a broadcaster otherwise it won't know when to change.
-
@d-healey Oh thanks, that's new for me. I'll investigate how to implement it :)
-
@d-healey I have simplified the code to only check the SampleMap of one sampler. I also changed the constant variables to local variables within the inline function updateKeyboardPanelVisibility(). After making these changes, I called the function "updateKeyboardPanelVisibility()" on the sample selector, and it started working. However, I noticed that the panels only update after changing the sample map twice. I mean, when one panel is active and the other has to be activated, the change only takes effect after two switches. Any insights into why this might be happening?
const var pnlKeyboard1 = Content.getComponent("pnlKeyboard1"); const var pnlKeyboard2 = Content.getComponent("pnlKeyboard2"); inline function updateKeyboardPanelVisibility() { local currentSampleMapId1 = Sampler1.getCurrentSampleMapId(); local PadSampleMaps = ["Pad 1", "Pad 2", "Pad 3", "Pad 4"]; if (PadSampleMaps.contains(currentSampleMapId1)) { pnlKeyboard1.showControl(true); pnlKeyboard2.showControl(false); } else { pnlKeyboard1.showControl(false); pnlKeyboard2.showControl(true); } } inline function onpnlSampleSelector1Control(component, value) { if (value) { local array = component.get("popupMenuItems").split("\n"); local t = array[value - 1]; local t2 = t.substring(t.indexOf(":") + 2, t.length); lblSampler1.set("text", t2); Sampler1.loadSampleMap(t2); updateKeyboardPanelVisibility(); } };
-
@bendurso I'm not sure it's a good idea to call your variable
array
as that is a type, but perhaps it won't hurt...When you load a sample map it doesn't happen instantaneously, but your call to
updateKeyboardPanelVisibility()
does happen straight after yourloadSampleMap
call. But at that point the sample map ID will still be for the old one, because the new one hasn't finished loading. You need to handle this in the preload callback.I showed how to do this last month in my auto colouring the keyboard video on Patreon.
By the way, why are you hiding/showing keyboards?
-
@d-healey The auto colorouing keyboard should be a perfect solution hehe. I placed two colored panels above the real (floating tile) keyboard to show the range of each selected sample map in a different color. (I have two samplers, so I'll duplicate this function)
Ohh, I just tried and it worked! Thank you so much!
pnlSampleSelector1.setLoadingCallback(function(isPreloading) { updateKeyboardPanelVisibility(); });
-
to show the range of each selected sample map in a different color.
Why not just colour the keys?
-
@d-healey Is it possible to color each sampler with a different color? Like this:
I should have started there hehe.
-
@bendurso Go watch my video, that's exactly what I show how to do :p