Operater as variable

0 favourites
  • 10 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi.

    Got 4 variables

    A : random number

    B : Random string operator (+-*/)

    C : random number

    D : how do I get the calculation of A & B & C ?

    Thanks in advance :)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I could calculate A and C as a random number, then I would have B as a variable choose(0,1,2,3) then have 4 events if B=0 A+B, if B=1 A-B, if B=2 A*B, if B=3 A/B

  • Thats a way to do it, thanks.

    But I would very much like to let the variable hold the operator, to avoid too many IFs.

    mid("+-*/", random(0,4),1) works fine to store the operator.. but perhaps it can't calculate when mixing string and numbers?

  • You could use tokenat to get a random 1-4 position of a string "*,+,-,/" but I'm not sure you can convert that found string back into an operator. Sounds like an unncessary workaround.

  • It’s only 4 ifs. You could get or make a plugin that runs some text as an expression, but that’s probably overkill. You could use the conditional expression to pack the ifs together to do everything in one event.

    Global number a=0

    Global text op=""

    Global number b=0

    Global number result=0

    Every 1 seconds

    — set a to int(random(100))

    — set op to mid("+-*/", int(random(4)), 1)

    — set b to int(random(100))

    — set result to 0

    — set result to (op="+")?a+b:result

    — set result to (op="-")?a-b:result

    — set result to (op="*")?a*b:result

    — set result to (op="/")?a/b:result

    — set text to a&op&b&"="&result

  • Just put it in a Function, so you can call it anytime.

  • Yes. A function would be great here.

    But what If I got 2 or 3 or 10 operators? :)

  • Notice the function doesn't pass any parameters through it so it defines some variables and lists the exact same events I described above. I feel like you could've created those events by now. Not sure much time was saved here.

  • You could just build a text string and evaluate it with the browser plugin

    Set result to browser.execjs(“2+5/3”)

    Or you could make your own parser to interpret an equation. One possible example here:


    You could do something simpler though since you already have the numbers and operators separate.

  • If you just want to perform an operation with 2 parameters it's pretty easy, have the 2 parameters as number variables and the operator as a string variable. Then create a function per operator, called something like "operator_+". Then when you want to execute an operation call the function "operator_" & opString with the 2 parameter variables.

    Here's an example with live evaluation

    Doing something more complicated than that, say taking a piece of text containing any number of operations then it's going to be a pain. You could potentially just execjs like R0J0hound says, but it's a bit of a security risk ( users could evaluate anything... ). You would need a proper expression parser, like what R0J0hound implemented in his calculator project ( which is super impressive BTW ). But that's probably way more complicated than you were hoping. It requires breaking a piece of text into tokens, then creating a recursive parser that respects the precedence of your operators.

    You might be able to implement reverse polish notation instead, which would be simpler to evalutate but users wouldn't understand it. The simplest correct option ( if you know JS ) is to write a small plugin that evaluates a string and returns the output value.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)