How do I Parse and Process a formula from a string

Get help using Construct 2

Post » Tue Sep 08, 2015 12:23 pm

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.
B
6
S
1
Posts: 14
Reputation: 330

Post » Tue Sep 08, 2015 12:56 pm

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.
B
7
S
2
Posts: 92
Reputation: 663

Post » Tue Sep 08, 2015 1:36 pm

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.
B
6
S
1
Posts: 14
Reputation: 330

Post » Tue Sep 08, 2015 2:02 pm

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/
B
7
S
2
Posts: 92
Reputation: 663

Post » Tue Sep 08, 2015 2:12 pm

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.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Tue Sep 08, 2015 5:49 pm

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.
B
91
S
31
G
103
Posts: 5,235
Reputation: 67,756

Post » Wed Sep 09, 2015 3:17 pm

@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.
B
6
S
1
Posts: 14
Reputation: 330

Post » Wed Sep 09, 2015 4:17 pm

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.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675


Return to How do I....?

Who is online

Users browsing this forum: FlyingRam, jkmalau and 11 guests