weird macro bug

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
wolph42
Deity
Posts: 9831
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

weird macro bug

Post by wolph42 »

Can someone explain me why this code:

Code: Select all

1
[h:Skills = '{"Awareness": {"bonus":0,"char":"Per"}}']
2
[h:tokenType = "Space Marine"]
3
[r,switch(tokenType), CODE:
    case "Space Marine"    : {
4
    
[h:Skills = '{"Awareness": {"bonus":0,"char":"Per"}}']
5
    
};
]

6


results in this
1 2 3 4 [h kills = '{"Awareness": {"bonus":0,"char":"Per"} 6


I was debugging some code and wondered why a part of the switch wasn't executed (in this case the string copy and '5'), I finally narrowed it down to this. It looks like its "{" nesting problem, however in this case they're inside a string. Is the fact that its a string completely ignored by MT? If so how do I solve this, or is there another solution.

Edit: I've tried encode/decode, but (roughly) same result

What I want to do is set a couple of token (json) properties to certain values depending on the choice of the user. I know I should code the json structures, however in this case I have a couple of default tokens from which I take their values and plain copy them on others. Thats a lot easier that rebuilding a complete json structure (note that its A LOT bigger then just this awareness skills and there are A LOT more properties involved. This is just a bare bone to show the bug)

User avatar
patoace
Dragon
Posts: 313
Joined: Mon Sep 24, 2007 6:10 pm
Location: Rancagua - Chile

Re: weird macro bug

Post by patoace »

May be it's an expression of the "no more than 2 nested { }" problem.

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: weird macro bug

Post by CoveredInFish »

+1

Since the nesting-limit is due to a reg-ex it probably doesnt help if they are in string literals. Encode/Decode do replace the curly brackets, so I do not understand why it shouldnt work with encoded strings. And as far as my testing goes it does work...

Code: Select all

[h:Skills ='']
[
hskillSpaceMarine =  '{"Awareness": {"bonus":0,"char":"Per"}}']
[
h:tokenType "Space Marine"]

[
h,switch(tokenType), CODE:
    case 
"Space Marine"    : {

<!-- 
this works -->
    [
rSkills skillSpaceMarine]

<!-- and 
this to you have to decode this again -->
    [
r:Skills ="%7B%22Awareness%22%3A%7B%22bonus%22%3A0%2C%22char%22%3A%22Per%22%7D%7D"]

    }]
 

User avatar
aliasmask
Deity
Posts: 8651
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: weird macro bug

Post by aliasmask »

wolph42 wrote:Can someone explain me why this code:

Code: Select all

1
[h:Skills = '{"Awareness": {"bonus":0,"char":"Per"}}']
2
[h:tokenType = "Space Marine"]
3
[r,switch(tokenType), CODE:
    case "Space Marine"    : {
4
    
[h:Skills = '{"Awareness": {"bonus":0,"char":"Per"}}']
5
    
};
]

6


results in this
1 2 3 4 [h kills = '{"Awareness": {"bonus":0,"char":"Per"} 6


I was debugging some code and wondered why a part of the switch wasn't executed (in this case the string copy and '5'), I finally narrowed it down to this. It looks like its "{" nesting problem, however in this case they're inside a string. Is the fact that its a string completely ignored by MT? If so how do I solve this, or is there another solution.

Edit: I've tried encode/decode, but (roughly) same result

What I want to do is set a couple of token (json) properties to certain values depending on the choice of the user. I know I should code the json structures, however in this case I have a couple of default tokens from which I take their values and plain copy them on others. Thats a lot easier that rebuilding a complete json structure (note that its A LOT bigger then just this awareness skills and there are A LOT more properties involved. This is just a bare bone to show the bug)

This is a horrible practice, so you shouldn't do it. Use json.set instead. But, the above can work if you switch the quotes and single quotes.

Code: Select all

1
[h: Skills = '{"Awareness": {"bonus":0,"char":"Per"}}']
2
[h:tokenType = "Space Marine"]
3
[r,switch(tokenType), CODE:
    case "Space Marine"    : {
4
    [h: Skills = "{'Awareness': {'bonus':0,'char':'Per'}}"]
5
    };
]

6

My guess is the parser checks for quotes but not single quotes when counting nested braces.

User avatar
wolph42
Deity
Posts: 9831
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: weird macro bug

Post by wolph42 »

thanks for the many replies and suggestions... none of them helps unfortunately...

- yes its horrible practice, but the json structures are build the correct way but instead of 'retrieving' them from a token property I just cram them straight away in a macro, TBH I don't see the difference (save the fact that the latter won't work... :( ). Note that it requires roughly 500 lines of code to create the actual JSON objects I want to use for this, its not the way to go.
- I did also switch the quotes as I know this sometimes is an issue, same error as with the encode. Note though that this error is different in that the method in the OP shows that the switch breaks but the macro continues, while the other two (encode and switch quotes) give a different error report.
- nice to have it confirmed that it is indeed a nesting problem.
- one final trick I can think of is to define all the variables in the start of the macro and set those needed in the switch, it costs more processing but at least that will work (I hope)

Its ofcourse possible that I made a typo with the encode and/or quotes trick, though I copy pasted the line both outside and inside the switch and one worked the other not. Ill test again and let you know

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: weird macro bug

Post by CoveredInFish »

You could - maybe - set the actual json objects to temporary variables outside of the code block and use those variables inside.

User avatar
wolph42
Deity
Posts: 9831
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: weird macro bug

Post by wolph42 »

CoveredInFish wrote:You could - maybe - set the actual json objects to temporary variables outside of the code block and use those variables inside.


Wolh42 wrote:- one final trick I can think of is to define all the variables in the start of the macro and set those needed in the switch, it costs more processing but at least that will work (I hope)


:roll:

but thanks anyway for confirming my brainfart :mrgreen:

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: weird macro bug

Post by CoveredInFish »

Aaaaah ... so that was what you meant. :oops:

User avatar
Azhrei
Site Admin
Posts: 12058
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: weird macro bug

Post by Azhrei »

Did you really look at the output?

Your original text was something like "[h:Skills ..." and your output showed "[h kills ..." So my first question to you is, "What's different?"

Notice how the :S is missing? I'll bet there's some kind of weird smiley interaction going on. Add a space after the colon and see if that fixes it.


User avatar
Azhrei
Site Admin
Posts: 12058
Joined: Mon Jun 12, 2006 1:20 pm
Location: Tampa, FL

Re: weird macro bug

Post by Azhrei »

Heh-heh, I take it that worked. 8)

It really shouldn't be necessary to do that for macros. :cry: Unfortunately, the way macros were grafted onto the parser this is one of those "live with it" moments. :(

User avatar
wolph42
Deity
Posts: 9831
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: weird macro bug

Post by wolph42 »

Azhrei wrote:Heh-heh, I take it that worked. 8)

It really shouldn't be necessary to do that for macros. :cry: Unfortunately, the way macros were grafted onto the parser this is one of those "live with it" moments. :(


well to be exact, no it did not work, however the trick that Alias mentioned and of which I was also aware but didn't work before, does work now. Its far from ideal as it requires to do a search and replace first which makes the macro less useable then I hoped. Still it works.
Not tested, but my guess is that the encode decode method will now work as well.

Post Reply

Return to “Macros”