Targeting Macro Issues MT 1.4.5.4

Developer discussion regarding MapTool 1.4

Moderators: dorpond, trevor, Azhrei

Forum rules
Posting now open to all registered forum users.
Post Reply
semada
Kobold
Posts: 7
Joined: Wed Feb 27, 2013 12:23 am

Targeting Macro Issues MT 1.4.5.4

Post by semada »

I have a macro that selects the exposed, player visible tokens on the map and displays them in a list for targetting selection on an attack macro. Everything functions exactly as it should but there are two cosmetic issues.

The first is that 3 of the 6 tokens have the same image.
The second is that Andromeda and Witt both have extra characters in their names that shouldnt be there.

I trimmed the code to the relevant parts of the macro.

Anybody have any suggestions?

Image

Code: Select all

[h:tokExposed= json.sort(json.intersection(getTokenNames("json",'{layer:["TOKEN"]}'), getExposedTokenNames("json")),"a")]
[H: imgList = tokExposed]
[H: Num = listCount(imgList)]
 
[h,COUNT(Num),CODE:
	{
		[h:tokenName = listGet(imgList,roll.count)]
		[h,token(tokenName): image = getTokenImage()]
		[h:imgList = listReplace(imgList,roll.count,tokenName+" "+image)]
	}
]
[h: status=input("target|"+imgList+"|Select Target|LIST|SELECT=0 ICON=TRUE ")]


User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Targeting Macro Issues MT 1.4.5.4

Post by aliasmask »

Well, you shouldn't use getList because a json is not a list.

I recommend using

Code: Select all

[H: tokExposed= json.sort(getExposedTokenNames("json"),"a")]
[H: imgList = ""]
[H, foreach(tokenName,tokExposed), code: {
   [H: image = getImage(tokenName)]
   [H: imgList = listAppend(imgList,strformat("%{tokenName} %{image}-30"))]
}]
[H: abort(input(strformat("target|%{imgList}|Select Target|LIST|ICON=TRUE ")))]
I don't think getExposedTokenNames includes tokens not on Token layer so you don't need the getTokens part (I believe). You can probably use getVisibleTokenNames as well. That way it won't include tokens currently unseen but previously exposed.

semada
Kobold
Posts: 7
Joined: Wed Feb 27, 2013 12:23 am

Re: Targeting Macro Issues MT 1.4.5.4

Post by semada »

Thanks Aliasmask.

I was using the example code from https://lmwcs.com/rptools/wiki/getExposedTokenNames which says the code would return only the exposed tokens in the token layer.

I will try out your code and see how it works.

semada
Kobold
Posts: 7
Joined: Wed Feb 27, 2013 12:23 am

Re: Targeting Macro Issues MT 1.4.5.4

Post by semada »

Your code shows all exposed tokens, even those not on the token layer. I needed it to only get the token layer tokens. I was also using the status=input code at the end for the selection because i had several checkboxes that also appear on the popup. Like so:
Image

semada
Kobold
Posts: 7
Joined: Wed Feb 27, 2013 12:23 am

Re: Targeting Macro Issues MT 1.4.5.4

Post by semada »

Here is the beginning top of my code that gathers the tokens and displays the input window:

Code: Select all

[h:tokExposed= json.sort(json.intersection(getTokenNames("json",'{layer:["TOKEN"]}'), getExposedTokenNames("json")),"a")]
[H: imgList = tokExposed]
[H: Num = listCount(imgList)]
 
[h,COUNT(Num),CODE:
	{
		[h:tokenName = listGet(imgList,roll.count)]
		[h,token(tokenName): image = getTokenImage()]
		[h:imgList = listReplace(imgList,roll.count,tokenName+" "+image)]
	}
]

[h: status=input(
	"target|"+imgList+"|Select Target|LIST|SELECT=0 ICON=TRUE ",
	"feature2|None, EvilOutsiderDragonUndead, OtherEvil |Smite Evil|LIST|SELECT=0 VALUE=STRING",
	"haste|0|Hasted?|CHECK",
	"isPowerAttack|0|PowerAttack?|CHECK",
	"isCharging|0|Charging?|CHECK"
	)
]

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Targeting Macro Issues MT 1.4.5.4

Post by aliasmask »

Weird, I can't get getExposedTokenNames() to work. Always returns blank eventhough I have vision on, fow and exposed tokens.

Here's a variation on a function I wrote awhile ago which I use for all my targeting which works for PC and NPC tokens. It will only show the tokens seen by source token. I put a bunch of extra comments in there for you, but typically you can remove those without the *. Tested the code to make sure it works.

Code: Select all

<!-- Macro should have apply to selected checked -->
[H: source = currentToken()]
<!-- get all the tokens on the token layer -->
[H: tokenIds = getTokens("json",json.set("{}","layer","token"))]
<!-- we stop if there are no targets or if there is no current token. remove self from list -->
[H, if(! json.isEmpty(tokenIds) && ! json.isEmpty(source)): tokenIds = json.difference(tokenIds,json.append("",source)); abort(0)]

<!-- reduce list to all tokens to only seen tokens -->
[H: targets = "[]"]
<!-- do this so it works for npc tokens who normally don't have sight on -->
[H: hasSight = hasSight()]
[H, if(! hasSight()): setHasSight(1)]
<!-- add to target list is the source can see the token -->
[H, foreach(tokenId,tokenIds), code: {
   [H: canSee = ! json.isEmpty(canSeeToken(tokenId))]
   [H, if(canSee): targets = json.append(targets,tokenId)]
}]
<!-- turn npc sight back off it was off before -->
[H, if(hasSight != hasSight()): setHasSight(hasSight)]

<!-- generate the list for the input and sort by name -->
[H: inputList = ""]
[H, foreach(tokenId,targets), code: {
   [H: switchToken(tokenId)]
   [H: image = getTokenImage()]
   [H: inputList = json.append(inputList,strformat("%{token.name} %{image}-30"))]
}]
[H: inputList = json.toList(json.sort(inputList,"a"))]

<!-- INPUT -->
[H: abort(input(
   strformat("target|%{inputList}|Select Target|LIST|ICON=TRUE"),
	"feature2|None, EvilOutsiderDragonUndead, OtherEvil |Smite Evil|LIST|SELECT=0 VALUE=STRING",
	"haste|0|Hasted?|CHECK",
	"isPowerAttack|0|PowerAttack?|CHECK",
	"isCharging|0|Charging?|CHECK"
))]
keywords: amsave targeting

semada
Kobold
Posts: 7
Joined: Wed Feb 27, 2013 12:23 am

Re: Targeting Macro Issues MT 1.4.5.4

Post by semada »

That code works great! Thanks Aliasmask!

One small issue though.

Image

The top of the input window says "Input values for Bob", but the token selected isnt Bob, its Ulrich.
I tried copying the macro to Bob's token, and it comes up with another tokens name.

Its a small issue and im sure my players and I can ignore it. Just wondering why its doing it. I cant see in your code where its even coming up at.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Targeting Macro Issues MT 1.4.5.4

Post by aliasmask »

You could do switchToken(source) just before the input() call.

semada
Kobold
Posts: 7
Joined: Wed Feb 27, 2013 12:23 am

Re: Targeting Macro Issues MT 1.4.5.4

Post by semada »

That worked!

Final thing. I am trying to use the following code to output the name of the target, but its appending the asset value along with the name.

Code: Select all

[h: targetName = listGet(inputList,Target)]
Ulrich skewers [r: targetname] with his mighty lance!
Ulrich skewers Andromeda asset://9936caa4cd903487d37203a3a6c8d9c8-30 with his mighty lance!

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Targeting Macro Issues MT 1.4.5.4

Post by aliasmask »

Well, that's another thing. You have to save the names of each one separately to line up with the inputList.

I would change the relevant code to this:

Code: Select all

<!-- generate the list for the input and sort by name -->
[H: inputList = ""]
[H: nameList = ""]
[H, foreach(tokenId,targets), code: {
   [H: switchToken(tokenId)]
   [H: image = getTokenImage()]
   [H: inputList = json.append(inputList,strformat("%{token.name} %{image}-30"))]
   [H: nameList = json.append(nameList,token.name)]
}]
[H: inputList = json.toList(json.sort(inputList,"a"))]
[H: nameList = json.sort(nameList,"a")]

<!-- INPUT -->
[H: abort(input(
   strformat("target|%{inputList}|Select Target|LIST|ICON=TRUE"),
	"feature2|None, EvilOutsiderDragonUndead, OtherEvil |Smite Evil|LIST|SELECT=0 VALUE=STRING",
	"haste|0|Hasted?|CHECK",
	"isPowerAttack|0|PowerAttack?|CHECK",
	"isCharging|0|Charging?|CHECK"
))]

[H: targetName = json.get(nameList,target)]
You could use regex to filter out the name from the inputList, but that's whole other can of worms.

semada
Kobold
Posts: 7
Joined: Wed Feb 27, 2013 12:23 am

Re: Targeting Macro Issues MT 1.4.5.4

Post by semada »

Bam that works perfectly now!

Post Reply

Return to “MapTool 1.4”