I’m trying to control a sketch through OSC from another application (SuperCollider). I want to be able to pass control data (integers) to various variables in the sketch by naming them in the OSC message.
For example I want to send an OSC message /var/backgroundHue/123 upon which the sketch would directly assign integer 123 to a variable backgroundHue.
I want to avoid specifying each variable like this:
putting aside that there’s no added value in storing incoming data - they are better off being sent immediately to the function that is animating stuff in realtime - but even if stored, how do you interpret the “parameter” string from JSON file, as variable name so that you could assign a value vrom OSCval2 to it? like
Reflection is not an easy topic to implement. An easier solution might be to use a hashmap to create a dictionary of ‘pseudo variables’. The following sketch shows how this might be done with mixed data types. Simply click the mouse to change the pseudo-variable values.
It doesn’t really solve my idea to optimise the flow. I will probably use if sentences like before just to change from numbers to variable names. So instead of
I am not convinced that your approach will be quicker than using a HashMap, especially if you have a large number of variables because the String equals method is slow whereas the HashMap does not perform string comparisons.
You can improve you approach by using else-if like this
if oscVal1 == “screenCleanHue” then in your code there will be 6 comparisons even though it matches the first one. Using else-if there is just 1 comparison the last 5 statements being ignored.
The problem is if oscVal1 == “screenCleanFlickrSpeed” then both codes need to perform 6 comparisons to find the match. This is where the HashMap is likely beat the multiple if and multiple else-if statements.
Yes, in terms of speed this is another question and pretty good answer. If i’ll see things getting slow because of that I might definitely try the HashMap approach. However I’m wondering how does HashMap find the right key and how quickly? Probably there are optimised and memory efficient sorting algorithms there?
In Java the key is passed through a hashing function to create a 32 bit integer. You can see this yourself assume you have created an object of some class (doesn’t matter which one) we can see the hashcode using myobject.hashCode(). A 32bit integer has over 4 billion possible values and the best hashing algorithms provide an even distribution over this range.
OK so we have the original “key-value pair” and the “key-hashcode” these are stored in a tree structure using the “key-hashcode” as the key and the “key-value pair” as the value.
So if we provide a key then the hashmap will generate the hashcode then search the tree* and return with the key-value pair then simply returns the value part.
searching a binary tree is exceptionally fast - O(log2 N) performance where log2 is the logarithm to base 2
If you know the range and maximum number of keys to be stored it is possible to design and implement a hashmap that works even faster - O(1) performance