I have a string that looks something like this:
[dictionary:test]==2&&[playerItem:Apple]!=0[/code:1ijyleqr] 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:1ijyleqr]Dictionary.Get("test")&"==2&&"&player.apple&"!=0"[/code:1ijyleqr] edit: also it should work if the string is this instead (or any other configuration): [code:1ijyleqr][playerItem:Apple]==2&&[playerItem:Pear]==1&&[dictionary:test]!=0[/code:1ijyleqr]
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.
Develop games in your browser. Powerful, performant & highly capable.
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.
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..
Okay, I think I found a suitable way to do it:
although, if anyone knows a better way, let me know.
I went with the idea of using solely dictionaries. Might interest you.
https://dl.dropboxusercontent.com/u/700 ... Regex.capx
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!
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! ep, 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.
ep, 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.
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
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.