0 Favourites

How do I Parse and Process a formula from a string

  • Thinking down the road for what I want to do with my game, I will need to implement a way to take a string like this

    "(x+y)*0.3"

    and turn it into an actual expression that pulls data from local variables. In regular code I would create a custom list data structure that had an operator and two links. Turn the string into a linked list. Then use a recursive function that would traverse the list and if a node links were only numbers then process that node.

    Any suggestions on how to do something like this in C2? I don't even see a way to parse the string.

  • You can create javascript plugins using JS SDK. If you're a good coder, and you sound it, you could create a plugin to interface in your events. You can even create custom expressions. All you would do is store your text in a variable, array, or dictionary, then you can feed it to your plugin and do all the processing you want with JS.

    https://www.scirra.com/manual/15/sdk

    Other than that, you'll definitely need a more advanced user to help with this one.

  • Yeah, that is likely where I'm going to have to go with this. Was hoping for an existing plugin or some way to do it in the editor.

  • Ah, I'm not gonna say there isn't one. Just wait for another more advanced user, or shoot support and e-mail. They might be able to help.

    You've likely seen this:

    c2-plugins-and-behaviors-list_t65170

    This guy has a load of plugins, but doesn't sound like there's anything in there that will help:

    http://c2rexplugins.weebly.com/

  • A cheap and quick way to tackle the problem would be using a Regex to replace variable with their actual value and then pass the expression to javascript's eval function.

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • You can actually do it in the same way with event as you can with code. I've done it a few times. The last one was here:

    r0j0-s-experiments_p955579?#p955579

    Most of it was syntax error checking though.

    Basically it grabs a token at a time from the string while skipping over spaces. regex could have been used to assist with that but I opted not to use it. After that it gives me a list of tokens which i then feed into this:

    https://en.wikipedia.org/wiki/Shunting-yard_algorithm

    Which converts it to reverse polish notation, while preserving the order of operations. Then it becomes simple to eval from left to right. I didn't get around to implementing variables here, but it was on paper.

    The main drawback is it can be a lot of events.

    Magistross' suggestion would be by far the simplest. Although you wouldn't have control over what js code was run.

  • Magistross Thank you for turning me onto the eval function. I did some looking and the CSV plugin by rex has an eval mode that uses that function. So, I think I can do a string replace as you suggest, assign the result to a cell in the CSV object and read it back out to get the evaluated version. I was planning on using the CSV plugin anyhow to manage an item database in game.

  • FYI, the Browser object has direct access to Javascript via the "Execute Javascript" action and the "ExecJS" expression. You could make use of the "ExecJS" instead of taking the extra step of using the CSV plugin.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
99 10,152
karshinkoff's avatar
karshinkoff
Unread hot topic
78 10,777
Josek5494's avatar
Josek5494
Unread hot topic
56 4,735
MPPlantOfficial's avatar
MPPlantOfficial