ID:1843537
 
Code:
var/SpellLevel=1
var/Formula="16*SpellLevel-5"

proc
FormulaAnswer()
return //And it's supposed to return the answer to the given formula which is stored as a string, which includes variables in it. It can't be set without being a text string or else it evalulates immediately based on the current SpellLevel and you'd have to use Formula=16*SpellLevel-5 which defeats the whole purpose.


Problem description:So how would I do this? I want the formula to be stored as a string so it doesn't evaluate immediately, then I will evaluate the formula when I need the answer to it, but how?
When do you actually want it to be evaluated? Just perform the calculation at that time.
I have 10 different if(s) simply to subtract mp cost based on the spell used where-as if I could evaluate the mp cost based on the formula which is a string I could get rid of all those IF statements, evulate the string to get the cost, and subtract it from my MP.
There are easier ways to do that.

If all you're doing is changing the SpellLevel before evaluating the formula, you could create some sort of data structure (objects, or a list) to store the SpellLevel of each Spell, y'know?
var/spells["firey" = 2, "icey"=3]

mob/verb/doTheFirey()
spell("firey")
mob/proc/spell(type)
mp -= 16* spells[type] -5
In response to Super Saiyan X
Except that the formula is different for each spell as well, I already store the level of each spell like that.
In response to Superbike32
Then you might want to make your spells objects with a function that does that calculation.
I've been working on an expression calculator lib that should help with situations like this. It's not quite ready yet.
In response to Lummox JR
Lummox JR wrote:
I've been working on an expression calculator lib that should help with situations like this. It's not quite ready yet.

I believe Yota has a similar library already from a few years ago, his own eval(), basically. What would be the difference (besides probably being more optimized)?
I actually had to work up something like this recently and as long as the calculations don't get too crazy, you can actually use eval(). There's an order of operations issue with subtraction but it can be worked around by using "+-" in the few places that comes up (adding the negative of a number to trick it into subtracting), which is usually when using parenthesis.