RPTools

Personal tools
From MapToolDoc

Working With Two CODE Levels

Contents

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 if I believe that if you use '{}' instead of "{}" it won't work (or the other way round).

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 give 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.

(need to add example later on) --Wolph42 16:22, 19 March 2011 (UTC)