playMaker

Author Topic: Universal simple math operator (with conversion) action?  (Read 5849 times)

realrobo

  • Playmaker Newbie
  • *
  • Posts: 23
Universal simple math operator (with conversion) action?
« on: February 26, 2018, 04:24:21 PM »
Is there any action that can tackle simple math actions universally?
I love PM, it saves countless hours of complex stuffs for me, but one of the most painful point is actually about doing some trivial math operations with some conversions.

As of right now, doing some simple operations like
myIntResult =  (myIntA + myIntB) * myFloatA / (myIntC - myFloatB) + myFloatC . . .

takes ton of additional intermediary conversion variables and actions to manage that, while in C#, it's a simple one liner (with conversions implicitly done).  I was looking for an action that can handle these type of math operations, and I can't seem to find one. 

As the project gets more and more refined, I find one of the hardest thing is to keep everything tight and organized, and I find myself struggle with having to make so many intermediary variables. 

Any help of course, would be sincerely appreciated.  Thank you




tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: Universal simple math operator (with conversion) action?
« Reply #1 on: February 26, 2018, 09:21:34 PM »
There is none. I was investigating the idea about a year ago. To execute code in this way is unfortunately not easy. Basically you would need to parse a string into c# code. Either you need to do it the old playmaker way, or create a c# script with a function (and then you can run that function in playmaker if you want).

While it is technically possible to do, the action to do it, needs to be a dll file, and not just source c#.

realrobo

  • Playmaker Newbie
  • *
  • Posts: 23
Re: Universal simple math operator (with conversion) action?
« Reply #2 on: February 27, 2018, 01:09:05 AM »
Aw, that is quite unfortunate to hear.  I was really hoping there was a simple action.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Universal simple math operator (with conversion) action?
« Reply #3 on: February 27, 2018, 01:52:28 AM »
Hi,

 making a custom action for a particular math equation is actually by far the preferred way, both in terms of performances and reliability.

Can you show an example of the equation you want to perform, and I'll do an action that does this, then you can use this as a base to do more.

 Bye,

 Jean

realrobo

  • Playmaker Newbie
  • *
  • Posts: 23
Re: Universal simple math operator (with conversion) action?
« Reply #4 on: March 05, 2018, 06:55:52 PM »
Ah, sorry for not replying fast enough, I just got back from a trip.

I do make a few simple actions for very specific calculation within my particular game.  For an example, I had a need of logarithmic calculation, so I made an action of that with whatever the base, and the offset I choose.  Those, I do not mind making them, because they are for such specific cases.  In fact, in general, I don't even mind doing multiple actions of int calculations or multiple float calculations in chains. 
The problem is when I need a lot of conversion even to do some chain of simple maths, because that means I have to create whole bunch of intermediary variables.

Example would be if I need to do the following maths in a game.
1) ((myIntA + myIntB) * myFloatA) + ((myIntC * myFloatB) /myIntD)
2) ((myFloatA - myIntA) / (myIntB + myFloatB)) + (myIntC * myFloatC)
3) . . .
. . .

If I have to make a custom action for each of those case by case, then it might get too bloated to manage.  Also it's too much hassle.

I would much prefer if there was a universal math operator, similar to maybe the conditional expression action.  (https://hutonggames.fogbugz.com/default.asp?W1226)
On the new expression, we would simply write the operation with the variables, and the outcome is stored in either int format or float format. 
That would save me a lot from creating ton of intermediary conversion variables, and also from making ton of custom actions that's only specific to that particular state of that particular game.
 

 
« Last Edit: March 05, 2018, 07:02:29 PM by realrobo »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Universal simple math operator (with conversion) action?
« Reply #5 on: March 06, 2018, 02:02:41 AM »
Hi,

Yes, this would be a good addition indeed :)

have you tried this: https://assetstore.unity.com/packages/tools/visual-scripting/c-eval-56706

 if that works for you in code, I can can make an action for this where you plug your variables and then use this, or even better, we could team up with the author so that he can add PlayMaker support right out of the box.

 Bye,

 Jean

 

Fat Pug Studio

  • Beta Group
  • Hero Member
  • *
  • Posts: 1294
    • Fat Pug Studio
Re: Universal simple math operator (with conversion) action?
« Reply #6 on: March 06, 2018, 03:13:01 AM »
+1 for this guys, i believe most of us are tired for using 10 actions in a state for equations.
Available for Playmaker work

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: Universal simple math operator (with conversion) action?
« Reply #7 on: March 06, 2018, 04:08:44 AM »
Id be interested in investigating some integration with that asset and making some samples for playmaker. I just purchased a copy to play with.

tcmeric

  • Beta Group
  • Hero Member
  • *
  • Posts: 768
Re: Universal simple math operator (with conversion) action?
« Reply #8 on: March 07, 2018, 12:38:16 AM »
I have purchased the asset. Had a look through its single example. (Wish there was more). I have reached out to the asset creator for a little assistance on parsing string to math equations. I will update here as I know more.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Universal simple math operator (with conversion) action?
« Reply #9 on: March 07, 2018, 04:49:30 AM »
Hi,

 excellent! Thanks Eric :)

Bye,

Jean

realrobo

  • Playmaker Newbie
  • *
  • Posts: 23
Re: Universal simple math operator (with conversion) action?
« Reply #10 on: March 07, 2018, 03:11:44 PM »
Oh wow.  Thank you Eric. 
Would love to see how if it could work. :]

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3998
  • Official Playmaker Support
    • LinkedIn
Re: Universal simple math operator (with conversion) action?
« Reply #11 on: March 07, 2018, 04:15:48 PM »
I've been playing around with a Math Expression action that uses the Mathos Parser: https://github.com/MathosProject/Mathos-Parser

The parser was pretty easy to setup as a Playmaker action (attached), but I haven't done a lot of testing yet - consider it alpha software!! Please post bugs/feedback and we'll see if it might be a viable solution to release...

Also, the parser is very extensible - we can easily add custom functions and variables e.g., to access things like Time.deltaTime in the expression.

I think it's worth exploring other options as well to compare performance, usability etc.

realrobo

  • Playmaker Newbie
  • *
  • Posts: 23
Re: Universal simple math operator (with conversion) action?
« Reply #12 on: March 08, 2018, 03:22:53 AM »
Holy Cow!  This is *EXACTLY* what I was looking for !!!  Plus more !!!
I just briefly spent time testing it real quick, and so far it works perfectly on all the simple math equations, as well as other added prefixes of functions. 

I haven't tested it thoroughly just yet, but I just had to come back here and post this.  Thank you so so much.  The usability and the extendability are just perfect.

This action, once it's thoroughly tested (sincerely hope it's performant), would pretty much eliminate the PM users from most common cases of intermediary variables and extra actions for math.  Man, this made my day. :]
« Last Edit: March 08, 2018, 03:38:44 AM by realrobo »

realrobo

  • Playmaker Newbie
  • *
  • Posts: 23
Re: Universal simple math operator (with conversion) action?
« Reply #13 on: March 09, 2018, 01:47:12 PM »
I've had little more time to test the action. (Thank you again, Guys).
In normal uses, it's fantastic so far.  It's processing anything I throw at it.  And it shines more when the calculation is longer or more complex.


I also tried to do some stress test. 
Set Up:
Two identical FSM to process the same math operations 999 times, but one with the MathExpression, and the other with typical math and conversion stacks.  I extracted out how long it took for the operation to complete in real time.

 
Assumption:
- The operation loop will happen in the single frame, so I just need to compare the time it took to complete the loop.
- Parsing strings will cause some GC spikes in the profiler.


The followings are the results.
1. For whatever reasons, I couldn't get the MathExpression loop to process all in the same frame.  Action does process, but it seems like the state that include the MathExpression action process it again in the next frame, rather.  (The identical FSM with the simple math and conversion operation stacks loop through the operation in a single frame).  I’m not sure if it’s because of parsing, or something else.  That’s beyond my ability.
2. The GC size is pretty big.  For simple math equation loop, the GC allocation was about 250kb every frame.



To sum up my finding so far,
1.  It's a beautiful action to process complex math action with many mix of floats and ints.  The more complex the equation is, the more it will save you from needing to make conversion variables, and stacks of math actions.  It helps keeping the FSM clean and organized.
2.  Not meant to be running too frequently due to some overhead such as GC.
3. Obviously, I could've made some mistakes with the set up somewhere, that's why I made the identical FSM for the two (except the state where it does the calculation).  What I was after was the comparison purpose only.  Nevertheless, I wouldn't call my testing a deep dive thorough test, so hope someone more technical than me can also test it out.
« Last Edit: March 09, 2018, 02:01:57 PM by realrobo »