TIP: getImage()

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
lmarkus001
Great Wyrm
Posts: 1867
Joined: Sat Mar 29, 2008 12:30 am
Location: Layfayette Hill, PA

TIP: getImage()

Post by lmarkus001 »

Anyone that has messed with Wiki: getImage() knows that it has some great weaknesses:
  • If not found it fails ungracefully aborting any running macro
  • If 2 tokens share the same name, it fails ungracefully aborting any running macro
  • It does not work on IDs
My workaround is this alternate getImage(param1,opt). It returns an empty string if the token is not found or if duplicate token names exist, and it allows for a token ID and size parameter.

In onCampaignLoad define the new getImage function (which replaces the old!):

Code: Select all

[H: defineFunction("getImage", "udfGetImage@LIBRARYTOKENNAME",1,1)]   
Then the macro itself is

udfGetImage

Code: Select all

<!--
//  Parameter count checking
// If 1 parameter treat it as a token name
// If there are more than 1 parameters, treat the first as a token ID and the second as a size parameter
// If the size parameter is 0, do not specify the size

[H: udfGetImage.acount = argCount() ]

[H, IF( udfGetImage.acount >= 1): udfGetImage.arg0 = arg(0); udfGetImage.arg0 = "" ]
[H, IF( udfGetImage.acount >= 2): udfGetImage.arg1 = arg(1); udfGetImage.arg1 = "" ]

[H: udfisID = if( udfGetImage.acount >= 2, 1, 0) ]

// Returns an empty string if image not found OR if duplicate token names exist
[H: udfGetImage.return = ""]

[ IF( ! udfisID ), CODE: {
   [ udfGetImage.return = oldFunction( udfGetImage.arg0 ) ]
};{}]

[ IF( udfisID &&  udfGetImage.arg1 == 0), CODE: {
   [ TOKEN( udfGetImage.arg0 ): udfGetImage.return = getTokenImage() ]
};{}]
[ IF( udfisID &&  udfGetImage.arg1 != 0), CODE: {
   [ TOKEN( udfGetImage.arg0 ):  udfGetImage.return = getTokenImage( udfGetImage.arg1 ) ]
};{}]

[H: macro.return = udfGetImage.return ]
-->  

User avatar
aliasmask
RPTools Team
Posts: 9031
Joined: Tue Nov 10, 2009 6:11 pm
Location: California

Re: TIP: getImage()

Post by aliasmask »

If not found it fails ungracefully aborting any running macro
Actually, it returns an empty string in b89 which I think was a fix from b87. It also grabs the first one found if there are duplicate names with different images. I do wish IDs were added as a valid parameter though. I think the origin of getImage was supposed to only be for image:tokens which is why a name and not id is needed. But it evolved at some point to take any image.

I would perhaps use findToken which takes an id or name (not in wiki) to see if token is on current map.

This is untested, but I believe should work for b87 as well:

Code: Select all

<!-- util.getImage(name/id,size) -->

[H, if(argCount()): nameId = arg(0); nameId = ""]
[H, if(argCount() >= 2): size = arg(1); size = ""]
[H: image = ""]

<!-- see if arg 1 is an id or name -->
[H: isId = if(length(nameId) == 32 && length(replace(nameId,"[^A-E0-9]","")) == 32,1,0)]
[H: isName = if(! isId && nameId != "",1,0)]

<!-- if arg 1 is a name, just getImage -->
[H, if(isName), code: {
   [H: image = getImage(nameId)]
};{
   <!-- if id, verify its on current map and use getTokenImage -->
   [H, token(nameId), if(isId && findToken(nameId) != ""): image = getTokenImage()]
}]

<!-- tack on the size if applicable -->
[H, if(isNumber(size) && image != ""): image = image + "-" + size]

[H: macro.return = image] 
edit: On second thought, I probably wouldn't replace getImage with this because of all the overhead. I would just make it a normal udf like util.getImage() or something like that and use it if there was a question or the option to take an Id and/or size for a parameter. The only change to code would be to change oldFunction to getImage (which I just did).

Post Reply

Return to “Macros”