MLMath Plugin
Contents
About
This plugin will allow you to perform basic math operations on MLServer Variables such as Add, Subtract, Multiply, Divide.
The Add function will also do string concatenation. You can have an unlimited number of parameters, processed in order
Supported Commands
Add
{MLServeCmd}Script Usage
MLServeCmd.MLMath|Add~VariableName~value~value~value~etc
(Literal)
MLServeCmd.MLMath|Add~Total~1~2~3~4
- Results: Total = 10
MLServeCmd.MLMath|Add~Total~{{Total}}~1
- Results: Total = 11
MLServeCmd.MLMath|Add~Total~The total value is~{{Total}}
- Results: Total = The total value is 11
Parameters | ||
VariableName | = | Variable Name created or overwritten in MLServer to store the values |
value | = | A string, integer or variable from MLServer. Variables from within MLServer should be surrounded by double curly brackets {{ }} |
Description
Adds values together or performs string concatenation.
Subtract
Divide
Multiply
Miscellaneous Notes
Allows math operations to be performed on an unlimited number of variables. Paired with the MLConditional plugin, I basically have been using it as a counter to track the number of rooms watching the dvd player
Description
Sometimes you may want to perform a command if a particular variable is within a certain range. For example, you might want to show a "Save energy" button only when there were between 3 and 6 pieces of equipment in the On state. Okay, so this is a weird example...
In my case, I have an overlay with 9 buttons on them, and if I only need 6, then I want to clear the 3 I don't need and display the 6 I do.
Dependencies
This structure utilitizes MLConditional and MLMath.
Command structure
If (myVar <= maxValue) And (myVar >= minValue) Then ...
Code:
MLServeCmd.Macro|
SetVariable|testValue~{{myVar}}
MLConditional|IsEqual##{{testValue}}##maxValue##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##maxValue - 1##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##minValue##Command1##ElseCommand1!
MLConditional|IsEqual##{{testValue}}##minValue##Command2##ElseCommand2!
MLConditional|IsEqual##{{testValue}}##minValue##CommandN##ElseCommandN
Of course you don't have to populate all the Else commands.
Sample for "If (myVar >= 2) and (myVar <= 5) Then Set Variable Result to CarPoolOK Else Set Variable Result to NoCarPool"
Code:
MLServeCmd.Macro|
SetVariable|testValue~{{myVar}}
MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##3##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool
So what's really going on in there?
MLServeCmd.Macro|
Yes, it's a macro.
SetVariable|testValue~{{myVar}}
First, we copy {{myVar}}'s value to a temporary variable. If we don't do this, we'll destroy the original value of {{myVar}} and potentially confuse ourselves up later.
If you really don't need to preserve the value of {{myVar}} you can skip this step, but I highly recommend leaving it in for consistency.
MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
Now we check if testValue is equal to the maximum value, which was 5. If it is, then we decrement it by one. You'll see why shortly.
MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
Now we check if testValue is equal to one less than the maximum value, which is 4. Note that if testValue started as 5 (the previous condition was true), then it's already been set to 4, which magically makes this condition true too.
MLConditional|IsEqual##{{testValue}}##3##MLMath|SUBTRACT~testValue~1##!
We continue to check values in the range, essentially looking to see where we "catch" the right value. Once we "catch" the right value, we
"hold on to" it by decrementing it. As soon as one comparison results true, all the comparisons that follow result true as well because we're making testValue "follow" the path of the comparisons.
MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool
At the minimum value, we have "collected" all values from the maximum value down to the minimum value, so now we execute whatever command we wanted to.
If we have multiple commands, we do the same comparison again.
We do not decrement testVal here, because it's unnecessary.
Besides, if we do, we'll "fall into" whatever processing we're doing for minimum value - 1.
If you need to process groups of commands, say A, B and C if the value is 3, A and B if the value is 2, and only A if the value is 1, add another MLConditional before the one that performs the subtraction.
Here is a slightly-more-complex example illustrating per-value and else processing.
Let's assume you have a five "Additional passengers" status buttons, referencing variables {{passenger2}} through {{passenger5}} and you want to display the status of each.
Code:
MLServeCmd.Macro|
SetVariable|testValue~{{myVar}}
MLConditional|IsEqual##{{testValue}}##5##SetVariable|passenger5~Present##SetVariable|passenger5~Not present!
MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##4##SetVariable|passenger4~Present##SetVariable|passenger4~Not present!
MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##3##SetVariable|passenger3~Present##SetVariable|passenger3~Not present!
MLConditional|IsEqual##{{testValue}}##3##MLMath|SUBTRACT~testValue~1##!
MLConditional|IsEqual##{{testValue}}##2##SetVariable|passenger2~Present##SetVariable|passenger2~Not present!
MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool
MLServeCmd.Macro|
Again, it's a macro 'cause macros make the world go 'round.
SetVariable|testValue~{{myVar}}
Make a copy so we don't lose the original value of myVar.
You could copy myVar to testValue, use myVar instead of testValue in the conditionals, then reset myVar to testValue at the end instead if that code reads better to you.
MLConditional|IsEqual##{{testValue}}##5##SetVariable|passenger5~Present##SetVariable|passenger5~Not present!
If we have 5 passengers, indicate Passenger 5 as Present, otherwise indicate Passenger 5 as Not present.
MLConditional|IsEqual##{{testValue}}##5##MLMath|SUBTRACT~testValue~1##!
Decrement the testValue because if we have 5 passengers, we should do the processing for 4 passengers too, otherwise passengers 2-4 won't get listed at all.
MLConditional|IsEqual##{{testValue}}##4##SetVariable|passenger4~Present##SetVariable|passenger4~Not present!
If we have 4 passengers, indicate Passenger 4 as Present, otherwise indicate Passenger 4 as Not present.
MLConditional|IsEqual##{{testValue}}##4##MLMath|SUBTRACT~testValue~1##!
If we have 4 passengers, we also need to "trickle down" to the processing for 3 passengers. By now you should get the idea.
MLConditional|IsEqual##{{testValue}}##2##SetVariable|passenger2~Present##SetVariable|passenger2~Not present!
Here we set or clear the value for Passenger 2.
MLConditional|IsEqual##{{testValue}}##2##SetVariable|Result~CarPoolOK##SetVariable|Result~NoCarPool
And now we set our result value for "Is 2 <= myVar <= 5?" to note whether car pool is OK or not for that number of passengers.
Disclaimer: Car pool requirements vary from region to region.
Additional notes:
You could do the same thing by reversing the process and checking the minimum condition first then incrementing the value.
To make it a strict less-than and/or strict greater-than, just increase the max value by one and/or reduce the min value by one. You can do this manually if you're checking fixed values, or by using MLMath if you're checking against variables.
Well, I hope that helps someone because it took me a while to write!
Brandon