Macro Writing Exercise: Create an image table database

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
aliasmask
Deity
Posts: 8667
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Macro Writing Exercise: Create an image table database

Post by aliasmask »

This is something that I'm going to do, but I thought it would be fun for new macro writers to test their skills. It shouldn't take too long to do and is straight forward.

Project Details:
  • Create a lib token on an empty map to hold your functions.
  • Drop tokens that you wish to create or add to an image table/database.
  • The token names will be the reference you use to access the image in the table, so they must be unique.
  • Index 0 will hold a json array of all the token names in the table.
  • Macro will ask for table name or use a default name.
  • Tables will be invisible to players but accessible.
  • Default Table imageId will be that of the lib token.
  • Table will be created if it doesn't exist.
  • Token names will be unique and overwrite existing image.
  • Images can be added with each run of the macro.
  • Have an option to clear the table first.
What I assume will be the basic process:
  • Get user input for table name and table options.
  • Get all token names from current map excluding the lib token (I think getTokens() does this already)
  • Loop through list adding each image to table.
  • If name exists, update correct index.
  • Update index 0 with the image list.
Index of image doesn't matter nor will refactoring the table so images have an new index because they are referenced by name. But there shouldn't be any gaps in the indexing.

Finally, write a macro that will get an imageId by it's name from the table. You can use my starter lib token for the basis of your lib token.

Feel free to add more features if you think it will be handy. For example, I was thinking of adding only selected tokens to image table.

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: Macro Writing Exercise: Create an image table database

Post by Jagged »

I shall follow this with baited breath, as I don't think the table macros have had much of a test/workout yet :shock:

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

Re: Macro Writing Exercise: Create an image table database

Post by aliasmask »

One reason I want to do this is because I'm making some fancy menus and I have a lot of image buttons. I also know I'll likely want to add more images later and change existing ones. Referencing by the image name is much more intuitive in the code rather than asking for tableimage("buttons",23) over, getButtonImage("Two Weapon Fighting").

And as with most things I like to make them generic enough to work with other projects.

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

Re: Macro Writing Exercise: Create an image table database

Post by aliasmask »

@jagged

I've noticed a couple of things with the table functions. First, setTableEntry in the wiki says this:
setTableEntry(tableName, roll, result)
roll - Whether or not the table can be seen by players in the Table Window, true(1) or false(0).
roll description is incorrect. Here's the addTableEntry()
addTableEntry(tableName,rangeStart,rangeEnd,result,imageId)
tableName - A string containing the name of the Table.
rangeStart - An integer indicating the lowest value that will return this row.
rangeEnd - An integer indicating the highest value that will return this row.
result - A string containing the result returned by the table() function.
imageId - Optional and is the asset id of an image that will be returned by the tableImage() function.
setTableEntry should include start and end range rather than roll and should also have the option for an imageId.

I haven't tested all the table functions, but I'm going to guess its the same for all of them that deal with imageId. The typical format of an imageId is "asset://...." where ... is a 32 character string, but AddTableEntry only takes the 32 character string, no "asset://" other MT functions return and take the full asset string.

edit: Wiki seems to be wrong for setTableImage as well. I assumed it would set the image for table in tables frame, but description say it sets it for an entry. I assume it should be setTableImage(tableName,roll,assetId). Is/will there be a way to just set the table image other than when creating it? Actually, I don't think it's really necessary anyway.

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

Re: Macro Writing Exercise: Create an image table database

Post by aliasmask »

Well, done with the lib token. You drop it on your map, run onCampaignLoad (only once) and drag "Image DB" macro to campaign window. Then you can select the tokens to add images to the db. Option to add all or just the selected. I added 32 images and it took 3 to 5 seconds to process, so be aware if you're going to add lots of images.

Here are the basic macros. I did make these in to UDFs using my Starter Lib Token. You can download my lib token below with all of the macros.
CODE

Code: Select all

@@ @<b>Image DB</b>
@[email protected] fontColor=black ; autoExecute=true ; fontSize=11pt ; sortBy= ; color=pink ; playerEditable=false ; applyToSelected=false ; group=Campaign Window ; tooltip= ; minWidth=90 ; 
[H: createImageDB()]

!!
@@ @createImageDB
@[email protected] fontColor=black ; autoExecute=true ; fontSize=11pt ; sortBy= ; color=aqua ; playerEditable=false ; applyToSelected=false ; group=Image DB ; tooltip= ; minWidth=94 ; 
<!-- createImageDB()

This function will prompt the user for a table name, create it and add images from current map in to it indexing the images at the 0 index.
-->

[H: abort(input("dbName|image.lib|Table Name of Image Library|TEXT",
   "addOption|Add All,Add Selected|Add Option|LIST"
))]

[H: setLibProperty("imageDBName",dbName,getMacroLocation())]

[H: allTables = getTableNames("json")]
[H, if(! json.contains(allTables,dbName)), code: {
   [H: createTable(dbName,0,1,noAsset(getImage(getMacroLocation())))]
   [H: indexList = "[]"]
   [H: listSize = 0]
   [H: addTableEntry(dbName,0,0,indexList)]
};{
   [H: indexList = table(dbName,0)]
   [H: listSize = json.length(indexList)]
}]

[H, if(addOption): tokenIds = getSelected("json"); tokenIds = getTokens("json")]

[H, foreach(tokenId,tokenIds), code: {
   [H: switchToken(tokenId)]
   [H: imageAsset = getTokenImage()]
   [H, if(json.contains(indexList,token.name)), code: {
      [H: index = json.indexOf(indexList,token.name) +1]
      [H: oldImageAsset = tableImage(dbName,index)]
      [H, if(oldImageAsset != imageAsset): deleteTableEntry(dbName,index)]
      [H, if(oldImageAsset != imageAsset): addTableEntry(dbName,index,index,token.name,noAsset(imageAsset))]
   };{
      [H: listSize = listSize + 1]
      [H: index = listSize]
      [H: indexList = json.append(indexList,token.name)]
      [H: addTableEntry(dbName,index,index,token.name,noAsset(imageAsset))]
   }]
}]

[H: setTableEntry(dbName,0,indexList)]

!!
@@ @getImageDB
@[email protected] fontColor=black ; autoExecute=true ; fontSize=11pt ; sortBy= ; color=aqua ; playerEditable=false ; applyToSelected=false ; group=Image DB ; tooltip= ; minWidth=94 ; 
[H: imageName = arg(0)]
[H: tableName = "image.lib"]
[H: index = json.indexOf(table(tableName,0),imageName) +1]
[H, if(index): macro.return = tableImage(tableName,index); macro.return = ""]

!!
@@ @noAsset
@[email protected] fontColor=black ; autoExecute=true ; fontSize=11pt ; sortBy= ; color=aqua ; playerEditable=false ; applyToSelected=false ; group=Image DB ; tooltip= ; minWidth=94 ; 
[H: macro.return = replace(arg(0),"asset://","")]

!!
amsave: image database
Attachments
lib_imageDB.rptok
(60.99 KiB) Downloaded 15 times

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: Macro Writing Exercise: Create an image table database

Post by Jagged »

Yeah, SetTableEntry in the wiki looks completely wrong.

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: Macro Writing Exercise: Create an image table database

Post by Jagged »

aliasmask wrote:
Sat Mar 02, 2019 9:12 am
setTableEntry should include start and end range rather than roll and should also have the option for an imageId.
If setTableEntry needed the start and end range, you would need to know significant more about the table than necessary to update the return value.
If the table has a range of 1-6 and you pass values 1-4, should you update the table?
Logically you only need to pass one value from within the range to accurately select the correct row, and removing the need for a start and end range allows you to modify a table you know little about.

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: Macro Writing Exercise: Create an image table database

Post by Jagged »

I have submitted a pull request which I think will make it into the next build to make the handling of assets more consistent.
So you will be able to do this:

Code: Select all

[r: asset1 = getImage("Dragon")]
[r: asset2 = getImage("Eagle")]
[r: setTableEntry("tableOne", 4, 1, asset1)]
[r: setTableEntry("tableOne", 8, 12, asset2)]

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

Re: Macro Writing Exercise: Create an image table database

Post by aliasmask »

Yeah, that seems fine to me just having the 1 reference rather than the range. createTable has the same issue with the assetId where it only takes the 32 character string.

There's also setTableImage which sounds like setTableEntry but just for the image. I'm thinking this should be for the image of the table in the tables frame.

User avatar
Jagged
Great Wyrm
Posts: 1306
Joined: Mon Sep 15, 2008 9:27 am
Location: Bristol, UK

Re: Macro Writing Exercise: Create an image table database

Post by Jagged »

aliasmask wrote:
Mon Mar 04, 2019 3:32 pm
There's also setTableImage which sounds like setTableEntry but just for the image. I'm thinking this should be for the image of the table in the tables frame.
Yes, you are correct. Tested this last night and updated the wiki accordingly. :)


PS: My Pull Request to change these to work with both asset IDs and asset URL has been merged, so this will definitely be in the 1.5.0 release.

Post Reply

Return to “Macros”