How do I regex or parse this?

Get help using Construct 2

Post » Mon Nov 02, 2015 10:07 pm

I have a string that looks something like this:
Code: Select all
[dictionary:test]==2&&[playerItem:Apple]!=0

I need help figuring out how to replace the stuff within the [ ] with a value from a dictionary, and a value from a sprite object.

So I need it to become
Code: Select all
Dictionary.Get("test")&"==2&&"&player.apple&"!=0"


edit: also it should work if the string is this instead (or any other configuration):
Code: Select all
[playerItem:Apple]==2&&[playerItem:Pear]==1&&[dictionary:test]!=0
B
42
S
19
G
65
Posts: 1,097
Reputation: 37,906

Post » Mon Nov 02, 2015 10:18 pm

Replacing with values from a dictionary is quite easy and straightforward. However, it is not really possible to retrieve instance variables by name, unless you hard-code them all.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Mon Nov 02, 2015 10:26 pm

Okay, then something else other than instance variable is fine. I'm just stuck on the parsing- how to differentiate between [dictionary and [player for example, and then substitute those spaces with other appropriate data.
B
42
S
19
G
65
Posts: 1,097
Reputation: 37,906

Post » Mon Nov 02, 2015 10:32 pm

Did you read the system expressions regarding text/strings?

https://www.scirra.com/manual/126/system-expressions

a simple replace() expression might do the trick..
I told my dentist I had trouble with my teeth and asked her to fix it without looking in my mouth..
B
54
S
16
G
8
Posts: 6,160
Reputation: 19,775

Post » Mon Nov 02, 2015 11:20 pm

Okay, I think I found a suitable way to do it:
although, if anyone knows a better way, let me know.
Image
B
42
S
19
G
65
Posts: 1,097
Reputation: 37,906

Post » Mon Nov 02, 2015 11:49 pm

I went with the idea of using solely dictionaries. Might interest you.

https://dl.dropboxusercontent.com/u/700 ... Regex.capx
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Tue Nov 03, 2015 12:22 am

@Magistross , yes that is very interesting/insightful! Using a dictionary for each sprite is something I was thinking about already, but now it makes more sense. I assume the dictionaries for the sprites should have the global value turned off, otherwise they'd continue to spawn new dictionaries if layout is reset? Thanks!
B
42
S
19
G
65
Posts: 1,097
Reputation: 37,906

Post » Tue Nov 03, 2015 12:48 am

Prominent wrote:@Magistross , yes that is very interesting/insightful! Using a dictionary for each sprite is something I was thinking about already, but now it makes more sense. I assume the dictionaries for the sprites should have the global value turned off, otherwise they'd continue to spawn new dictionaries if layout is reset? Thanks!
Yep, I didn't think of changing it to non-global, you're absolutely right on that account. Although, I believe that the fact that the sprites Dictionaries are tied to a non-global object makes them non-global too in the end.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Tue Nov 03, 2015 7:52 am

Here's a non-regex way to do it. It just loops through the input a character at a time. It's longer but I do find it more readable.
https://dl.dropboxusercontent.com/u/542 ... parse.capx
B
92
S
32
G
106
Posts: 5,272
Reputation: 69,455

Post » Tue Nov 03, 2015 4:58 pm

Ah, cool. It's interesting seeing the different ways. I like the simplicity of not using regex, but regex also makes it simpler in a different way. Now I need to decide which way I like more. :)
B
42
S
19
G
65
Posts: 1,097
Reputation: 37,906


Return to How do I....?

Who is online

Users browsing this forum: eochranek, Unconnected and 18 guests