[SOLVED] Trouble converting string "+" in an operator

T

Taddio

Guest
I'm a bit confused, I have this pretty simple piece of code to create a random math formula, but I'm missing the right function to get the operators working.
Code:
var _num1 = irandom(5):
var _num2 = irandom(10)
var _oper = choose(" + ", " - ");

var _formula = string(_num1)+_oper+string(_num3);

var _answer = _num1 + ?????(_oper) + _num3;
real(_oper) doesn't work (that was expected, as + is not a number), but I just can't find the right function to convert it to an actual operator.
Any ideas are very welcome.
 

chamaeleon

Member
I'm a bit confused, I have this pretty simple piece of code to create a random math formula, but I'm missing the right function to get the operators working.
Code:
var _num1 = irandom(5):
var _num2 = irandom(10)
var _oper = choose(" + ", " - ");

var _formula = string(_num1)+_oper+string(_num3);

var _answer = _num1 + ?????(_oper) + _num3;
real(_oper) doesn't work (that was expected, as + is not a number), but I just can't find the right function to convert it to an actual operator.
Any ideas are very welcome.
GMS does not natively support evaluating expressions from strings, only number conversion. Either write if statements that use the operator to decide what to do with each side, or investigate the use of marketplace offerings like https://marketplace.yoyogames.com/assets/4227/n-string-parser-2 if you don't want to deal with the hassle yourself (if you will have much more complex expressions and this was just an example).
 

KPJ

Member
Taddio if you do not find a way, you could use an if statement to check whether _oper is equal to "+" or "-". Depending on what it is, set _answer equal to num1 + num2 or num1 - num2. Hope this helps :)
 
T

Taddio

Guest
Yeah, I've just been told it's impossible to convert a string in an operator in GM, so...guess I'll just have to deal with it the ugly way:D

@chamaeleon will definately check it out, I may find inspiration in it for sure! Thanks

I won't have more than 3-4 numbers max, and 2-3 operators (+-*/) but that becomes a lot quite fast in terms of possibilities nonetheless.
 

chamaeleon

Member
Yeah, I've just been told it's impossible to convert a string in an operator in GM, so...guess I'll just have to deal with it the ugly way:D

@chamaeleon will definately check it out, I may find inspiration in it for sure! Thanks
Just be aware that using an extension written in GML for this purpose won't be execute nearly as fast as a compiled expression would. It's possible the extension support some kind of caching of work to allow for the execution multiple times with changing variables, but not having used it myself, I couldn't say.. In any case, just something to keep in the back of your head if you try to use it in a heavy computation part of your code.
 
I got it to work by changing your code a little bit:
Code:
var _num1 = irandom(5);
var _num2 = irandom(10);
var _oper = choose("+", "-");

var _formula = string(_num1)+" "+_oper+" "+string(_num2);

var _answer = _num1 + real(string(_oper)+"1")*_num2;

show_debug_message(_formula+" = "+string(_answer));
If you add more operators, just use an if or switch statement to account for the different kind of calculations.
 
T

Taddio

Guest
Well, it took a total of 5 minutes to get working.
I did a script scr_math_question(_num1, _num2, _num3, _oper1, _oper2); used a switch(argument4) within each cases of switch(argument3), and simply used (e.g) return argument0+(argument1*argument2) in the "*" case of arg4 within the "+" case of arg3

Took 75 lines of codes, but mostly copy and pasted, so it's all good (tho I'll remove the division to avoid the div by zero hassle)

I got it to work by changing your code a little bit:
Code:
var _num1 = irandom(5);
var _num2 = irandom(10);
var _oper = choose("+", "-");

var _formula = string(_num1)+" "+_oper+" "+string(_num2);

var _answer = _num1 + real(string(_oper)+"1")*_num2;

show_debug_message(_formula+" = "+string(_answer));
If you add more operators, just use an if or switch statement to account for the different kind of calculations.
Wow, really?!? I will have to check that out! Nicely done
 
Top