Working With Two CODE Levels: Difference between revisions
m (Typographical tweaks) |
|||
Line 1: | Line 1: | ||
=Working With Two CODE Levels = | =Working With Two CODE Levels = | ||
One of the biggest limitations of the MT macro script is that the script parser can't handle more that 2 levels of {}. So | One of the biggest limitations of the MT macro script is that the script parser can't handle more that 2 levels of {{code|<nowiki>{}</nowiki>}}. So this will work: | ||
[If(condition), CODE:{ | [If(condition), CODE:{ | ||
[if(another_condition), CODE:{ | [if(another_condition), CODE:{ | ||
Line 16: | Line 16: | ||
};{}] | };{}] | ||
};{}] | };{}] | ||
*Note that this is an example, there are other occasions where one uses {} e.g. {myVar} instead of [r:myVar] | *Note that this is an example, there are other occasions where one uses {{code|<nowiki>{}</nowiki>}}, e.g. {{code|<nowiki>{myVar}</nowiki>}} instead of {{code|[r:myVar]}}. This is subject to the same problem. The only exception I've encountered is with json objects: {{code|<nowiki>myVar = json.set("{}", "someKey", someVar)</nowiki>}}. This is NOT subject to this problem. However I believe that if you use {{code|'{}'}} instead of {{code|"{}"}} it won't work (or the other way around). | ||
==So what to do when you do need to go deeper?== | ==So what to do when you do need to go deeper?== | ||
Line 22: | Line 22: | ||
===Trick 1: Create another UDF=== | ===Trick 1: Create another UDF=== | ||
One of the most common 'tricks' is to create a User Defined Function (UDF) and call this in the nested level. | One of the most common ''tricks'' is to create a User Defined Function (UDF) and call this in the nested level. | ||
Within this UDF you can yet again go 2 CODE levels deep. | Within this UDF you can yet again go 2 CODE levels deep. | ||
===Trick 2: Code smarter=== | ===Trick 2: Code smarter=== | ||
Many many examples can be | Many many examples can be given here, but you can achieve a lot by using roll options e.g.: | ||
[if(condition): if(another_condition, "show this", "else show this"); if(yet_another_condition, "show this"; "else show this")] | [if(condition): if(another_condition, "show this", "else show this"); if(yet_another_condition, "show this"; "else show this")] | ||
You can also work with multiple roll options, but this 'should' not work, but does sometimes work e.g. | You can also work with multiple roll options, but this ''should'' not work, but does sometimes work e.g.: | ||
[foreach(item,items), if(item == someVar), CODE:{};{}] | [foreach(item,items), if(item == someVar), CODE:{};{}] | ||
works while: | |||
[if(listCount(items)>2), foreach(item,items), CODE:{};{}] | [if(listCount(items)>2), foreach(item,items), CODE:{};{}] | ||
won't work. | |||
A lot can be achieved by restructuring your code in this manner. | A lot can be achieved by restructuring your code in this manner. | ||
Line 38: | Line 38: | ||
===Trick 3: Store commands=== | ===Trick 3: Store commands=== | ||
One final trick I recently learned from Ahzrei is a rather dirty trick but can be used in certain circumstances. In my case I have code that needs to be executed that is already two levels deep and then I ALSO want to execute this for certain selected tokens. | One final trick I recently learned from Ahzrei is a rather dirty trick but can be used in certain circumstances. In my case I have code that needs to be executed that is already two levels deep and then I ALSO want to execute this for certain selected tokens. | ||
In this case you can first store the to-execute-commands in a json object and then exit the two loops. Now you have a json object containing all code that needs to be executed onto certain tokens. | In this case you can first store the to-execute-commands in a json object and then exit the two loops. Now you have a json object containing all code that needs to be executed onto certain tokens. For this you can start a new loop that uses {{func|json.evaluate}} per json object per token id. | ||
(need to add example later on) | {{Clarify|(need to add example later on)}} | ||
===Trick 4: Very Dirty Coding - More then 2 CODE levels === | ===Trick 4: Very Dirty Coding - More then 2 CODE levels === | ||
Line 60: | Line 60: | ||
</source> | </source> | ||
What | What it boils down to is to add two single quotes {{code|<nowiki>''</nowiki>}} at the END of EACH code block. This will fool the MT parser and it will accept this nesting. I've tried it upto 9 nestings without a problem (more is probly also no issue). Do keep in mind that this will SERIOUSLY impact the stack though, so don't go writing huge code blocks this way. I mainly use it for cases where you only have a few lines of code but are required to go 3 or 4 nestings deep and its just annoying to create a separate macro for one or two lines of code. | ||
Note that if in the above example the <nowiki>''</nowiki> will show up in the chat. You can also use <nowiki>[h:'']</nowiki> instead to prevent this. | Note that if in the above example the {{code|<nowiki>''</nowiki>}} will show up in the chat. You can also use {{code|<nowiki>[h:'']</nowiki>}} instead to prevent this. | ||
[[Category:Cookbook]] | [[Category:Cookbook]] | ||
--[[User:Wolph42|Wolph42]] 16:22, 19 March 2011 (UTC) | --[[User:Wolph42|Wolph42]] 16:22, 19 March 2011 (UTC) |
Revision as of 14:45, 30 October 2013
Working With Two CODE Levels
One of the biggest limitations of the MT macro script is that the script parser can't handle more that 2 levels of {}
. So this will work:
[If(condition), CODE:{ [if(another_condition), CODE:{ [code to execute when true] };{}] };{}]
And this won't:
[If(condition), CODE:{ [if(another_condition), CODE:{ [if(another_condition), CODE:{ [code to execute when true] };{}] };{}] };{}]
- Note that this is an example, there are other occasions where one uses
{}
, e.g.{myVar}
instead of[r:myVar]
. This is subject to the same problem. The only exception I've encountered is with json objects:myVar = json.set("{}", "someKey", someVar)
. This is NOT subject to this problem. However I believe that if you use'{}'
instead of"{}"
it won't work (or the other way around).
So what to do when you do need to go deeper?
Basically there are 3 general tricks you can use if you need to go deeper:
Trick 1: Create another UDF
One of the most common tricks is to create a User Defined Function (UDF) and call this in the nested level. Within this UDF you can yet again go 2 CODE levels deep.
Trick 2: Code smarter
Many many examples can be given here, but you can achieve a lot by using roll options e.g.:
[if(condition): if(another_condition, "show this", "else show this"); if(yet_another_condition, "show this"; "else show this")]
You can also work with multiple roll options, but this should not work, but does sometimes work e.g.:
[foreach(item,items), if(item == someVar), CODE:{};{}]
works while:
[if(listCount(items)>2), foreach(item,items), CODE:{};{}]
won't work.
A lot can be achieved by restructuring your code in this manner.
Trick 3: Store commands
One final trick I recently learned from Ahzrei is a rather dirty trick but can be used in certain circumstances. In my case I have code that needs to be executed that is already two levels deep and then I ALSO want to execute this for certain selected tokens. In this case you can first store the to-execute-commands in a json object and then exit the two loops. Now you have a json object containing all code that needs to be executed onto certain tokens. For this you can start a new loop that uses json.evaluate() per json object per token id.
Needs Clarification:
(need to add example later on)
Trick 4: Very Dirty Coding - More then 2 CODE levels
Basically you should not do this as it is NOT supported in MT and thus it could break your code at any given moment or simply not work in a newer version... That said, I use it sometimes and have not yet encountered any issue. Still this is at your own risk.
As it turns out it IS possible to have more then 2 nested code levels but in order to do that you have to mislead the parser. This is done as follows:
[if(1), CODE:{
[if(1), CODE:{
[if(1), CODE:{
you should never see this in the chat...but you do!
''
};{}]
''
};{}]
''
};{}]
What it boils down to is to add two single quotes ''
at the END of EACH code block. This will fool the MT parser and it will accept this nesting. I've tried it upto 9 nestings without a problem (more is probly also no issue). Do keep in mind that this will SERIOUSLY impact the stack though, so don't go writing huge code blocks this way. I mainly use it for cases where you only have a few lines of code but are required to go 3 or 4 nestings deep and its just annoying to create a separate macro for one or two lines of code.
Note that if in the above example the ''
will show up in the chat. You can also use [h:'']
instead to prevent this.
--Wolph42 16:22, 19 March 2011 (UTC)