I was wondering how can I use formula inside string for damage calculation? Since I want to insert something like formula inside variable through inspector to make it easier in case I want to make quick damage calculation on scriptable object.
Here’s some simple formula I use:
private int hp = 100;
private int atk = 5;
private int def = 3;
public string DMG = "(atk * 2) - def"; //the variable I want to edit on inspector
private void Start()
{
hp = hp - DMG; //how do I use string for formula?
Debug.Log("Current hp is " + hp);
}
how do I use formula inside string for damage calculation?
You include Lua, Python or some math parser in your project, at least in the editor (if then you convert the equations back into C# code on build). http://mathparser.org/
I really don’t recommend this as a strategy for making things easier to edit. It will take a lot of work to set up, make your math a lot slower than normal (though admittedly it still might not be slow enough to care), and introduce an additional dependency into your build (which give one more way things can go wrong).
But possibly the worst bit is that it will remove a lot of safety checking on your math–if you make a syntax error or spelling error, it won’t show up until your game actually tries to evaluate the expression, instead of giving you an error right away when you compile it. This means the mistake could lurk undetected in your game for a long time before suddenly exploding somewhere down the line.
Well, that may be true. But since I am still learning I see no problem in learning stuffs to try to make things simpler while the process to reach it might actually make things more complicated.
You mean the mathparser? I seriously recommend to open up the website and read it through before you even think about it. They have many tutorials, I’m pretty sure they have installation ones as well.
I don’t think this particular task is likely to teach you a lot of foundational skills relevant to other game development, but shrug it’s your time.
Remember to pay close attention to how the math parser handles variables/function arguments, because it won’t automatically know about the variables from your C# code.
Since the mathparser was already mentioned, I’ve written a lightweight expression parser some time ago. I’ve posted it on the wiki, but I’ve posted a more advanced version on github. It’s just a single file and can parse most math and even boolean expressions. The parser builds a custom expression tree that can be created and evaluated at runtime. Like most runtime parsers you can not directly use C# variables. Instead you have to explicitly set / update variables before evaluating the expression. For example:
using B83.LogicExpressionParser;
// [ ... ]
string DMG = "(atk * 2) - def";
NumberExpression dmgExp;
void Awake()
{
Parser exParser = new Parser();
dmgExp = exParser.ParseNumber(DMG);
dmgExp["atk"].Set(()=>(double)atk);
dmgExp["def"].Set(()=>(double)def);
}
private void SomeMethod()
{
hp = hp - (int)Math.Round(dmgExp.GetNumber());
Debug.Log("Current hp is " + hp);
}
The expression parser works only on double values. So if you want to do integer operations you have to round / cast back to int like I did here. Note that in this case I register the “atk” and “def” parameters as closures. So whenever this parameter is used in the expression during evaluation, it will call the provided delegate and automatically reads the current value of the atk / def variable. Instead of using delegates you can also set the value manually before evaluating the expression:
// [ ... ]
private void SomeMethod()
{
dmgExp["atk"].Set((double)atk);
dmgExp["def"].Set((double)def);
hp = hp - (int)Math.Round(dmgExp.GetNumber());
Debug.Log("Current hp is " + hp);
}
In this case the expression will use the newly provided values.
To “install” the parser you just need to copy the “LogicExpressionParser.cs” into your project. I’ve mentioned it in the additional notes of my original expression parser the “unary minus” can not follow another operator. So it needs to be wrapped in brackets. So instead of doing "4 * -2" you have to do "4 * (-2)". Other than that it should be able to parse almost all expressions you can think of. I once made a WebGL example with the old expression parser.