Create Tokens

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
Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Create Tokens

Post by Sydious »

Is it possible to create tokens from a macro?

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

Re: Create Tokens

Post by aliasmask »

Sydious wrote:Is it possible to create tokens from a macro?
See Wiki: copyToken(), but in order to create a token, you have to model it on a base token. Not all properties of a token can be updated with this method, so the closer the base token is to your final token, the better.

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: Create Tokens

Post by Sydious »

So now that I have progressed through this quest called Maptool, I have used copyToken a bit now. As stated above, updating the properties on the token using copyToken is limited.

I am wondering if the following is normal behavior, or am I doing something wrong.

I have a macro that copies a token, then fires an updater macro.
The second macro uses setProperty on the token but nothing gets updated. If I run the macro manually, it updates the token, but if I call the macro from the first macro, it doesn't update.

Is it a timing issue by calling the macro? or could it be something else I have done?

User avatar
jfrazierjr
Deity
Posts: 5176
Joined: Tue Sep 11, 2007 7:31 pm

Re: Create Tokens

Post by jfrazierjr »

Sydious wrote:So now that I have progressed through this quest called Maptool, I have used copyToken a bit now. As stated above, updating the properties on the token using copyToken is limited.

I am wondering if the following is normal behavior, or am I doing something wrong.

I have a macro that copies a token, then fires an updater macro.
The second macro uses setProperty on the token but nothing gets updated. If I run the macro manually, it updates the token, but if I call the macro from the first macro, it doesn't update.

Is it a timing issue by calling the macro? or could it be something else I have done?
Yes.. go back to AM's previous post. Also go and read the first paragraph of Wiki: copyToken() very carefully.
I save all my Campaign Files to DropBox. Not only can I access a campaign file from pretty much any OS that will run Maptool(Win,OSX, linux), but each file is versioned, so if something goes crazy wild, I can always roll back to a previous version of the same file.

Get your Dropbox 2GB via my referral link, and as a bonus, I get an extra 250 MB of space. Even if you don't don't use my link, I still enthusiastically recommend Dropbox..

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: Create Tokens

Post by Sydious »

Ok. I was hoping that fireing the second macro would allow for updatingt since it wasn't "in" the macro but a second macro. Wishful thinking I guess.

User avatar
jfrazierjr
Deity
Posts: 5176
Joined: Tue Sep 11, 2007 7:31 pm

Re: Create Tokens

Post by jfrazierjr »

Sydious wrote:Ok. I was hoping that fireing the second macro would allow for updatingt since it wasn't "in" the macro but a second macro. Wishful thinking I guess.
Yea, but it's in the same macro "chain". A -> B -> C STILL means that the macro A is running and execution is "IN" Macro A no matter where down the "chain" the macro doing the editing is located.
I save all my Campaign Files to DropBox. Not only can I access a campaign file from pretty much any OS that will run Maptool(Win,OSX, linux), but each file is versioned, so if something goes crazy wild, I can always roll back to a previous version of the same file.

Get your Dropbox 2GB via my referral link, and as a bonus, I get an extra 250 MB of space. Even if you don't don't use my link, I still enthusiastically recommend Dropbox..

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

Re: Create Tokens

Post by aliasmask »

I'm actually writing a macro called createToken for my own purposes and it should be done sometime today. Here's what I'm working on.

Code: Select all

[H: '<!-- List of Arguments Possible createToken(updates): id
   name - Name of token
   map - map name to move token to 
   target - name of token on current map OR x,y coords
   offset - x,y coord offset (always relative to target, uses useDistance metric)
   useDistance - 0 (grid x,y), 1 (gridless x,y)
   props - json of props to be set on token created
   states - json array of states to be set on token created
   layer - layer for token to be created on
   facing - facing of token
   size - size of token
   label - label of token
   gmName - gm name of token
   tokenImage, portraitImage, handoutImage - token images
-->']
 
There may be timing and debugging issues to deal with, so I'm not sure when it'll be done, but I'm pretty confident it'll be today. This macro will also require another fixed token to copy from and I'm using the image:Unknown token on a libraries map. I'm making this a part of my lib:players token so look for that release when it's out. But I can post the code before that if you wish to use it before the release. For now, I'm just having it create one token. I'll mess around with multiple tokens later.

Since I'm still writing it, I'm open to suggestions.

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

Re: Create Tokens

Post by aliasmask »

Here's my current code, which you'll see requires more functions other than this one, but perhaps you can glean the info you want... or just wait for the lib:players release. It's getting close to v1.0.

Code: Select all

@@ @createToken
[H: args = arg(0)]
[H: sourceToken = "image:Unknown"]
[H: sourceMap = getLibProperty("play.libMap",getMacroLocation())]
[H: currentMap = getCurrentMapName()]
[H: targetMap = json.get(args,"map")]
[H, if(json.isEmpty(targetMap)): targetMap = currentMap]
[H: '<!-- List of Arguments Possible createToken(updates): id
   name - Name of token
   * type - token type for properties
   * snap - snap to grid (default 1, yes)
   map - map name to move token to 
   target - name of token on current map OR x,y coords
   offset - x,y coord offset (always relative to target, uses useDistance metric)
   useDistance - 0 (grid x,y), 1 (gridless x,y)
   props - json of props to be set on token created
   states - json array of states to be set on token created
   layer - layer for token to be created on
   facing - facing of token
   size - size of token
   label - label of token
   gmName - gm name of token
   tokenImage, portraitImage, handoutImage - token images
-->']

[H: updateVars = json.append("","name","label","gmName","facing","size","tokenImage","portraitImage","handoutImage","layer")]
[H: updates = "{}"]
[H, foreach(varName,updateVars), code: {
   [H: value = json.get(args,varName)]
   [H, if(! json.isEmpty(value)): updates = json.set(updates,varName,value)]
}]
[H: useDistance = json.get(args,"useDistance")]
[H, if(json.isEmpty(useDistance)): useDistance = 0]
[H: updates = json.set(updates,"useDistance",useDistance)]
[H: target = json.get(args,"target")]
[H, if(json.isEmpty(target)), code: {
   [H: x = 0]
   [H: y = 0]
};{ 
   [H, if(json.type(target) == "OBJECT"), code: {
      [H: x = json.get(target,"x")]
      [H: y = json.get(target,"y")]
   };{
      [H: x = getTokenX(useDistance,target)]
      [H: y = getTokenY(useDistance,target)]
   }]
}]
[H: offset = json.get(args,"offset")]
[H, if(! json.isEmpty(offset)), code: {
   [H: x = x + json.get(offset,"x")]
   [H: y = y + json.get(offset,"y")]
};{}]
[H: updates = json.set(updates,"x",x,"y",y)]
[H, if(currentMap != sourceMap): setCurrentMap(sourceMap)]
[H: props = json.get(args,"props")]
[H: propsSet = ""]
[H, foreach(prop,props): propsSet = json.append(propsSet,strformat('[H: setProperty("%{prop}",decode("%s"))]',encode(json.get(props,prop))))]
[H: updates = json.set(updates,"name",json.get(updates,"name")+json.toList(propsSet,""))]
[H: states = json.get(args,"states")]
[H, foreach(state,states): setState(state,1,sourceToken)]

[H: id = copyToken(sourceToken,1,"",updates)]

[H: setAllStates(0,sourceToken)]
[H, if(currentMap != sourceMap), code: {
   [H: setCurrentMap(currentMap)]
   [H: am.play.deferFunction("moveTokenFromMap",json.append("",id,sourceMap,x,y))]
};{}]
[H, if(currentMap != targetMap): am.play.deferFunction("moveTokenToMap",json.append("",id,sourceMap,x,y))]
[H: macro.return = id]

!! 

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: Create Tokens

Post by Sydious »

Lots to absorb......

One question: What makes this diffrent then normal macros updating? Is it because it is a UDF and that subverts the timeing issue somehow? (Maybe it's my amature status as a programer that makes me not understand...)

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

Re: Create Tokens

Post by aliasmask »

When creating the token, you run in to timing issues for certain things. So, if you change the original token then copy it, you don't have that problem. Also, I haven't verified that all my code actually works the way I want it to. But the deferFunction does work with other stuff. Basically, it runs after the current chain of events, pauses 1 second then does it's stuff.

I've noticed a need to add "owners" to my list of things I wish to set. I was trying to set it with the props, but I think this is one of those things that is overwritten by copyToken, so I'll have to defer it and cross my fingers and hope that works. I'm going to see if I can do most of all the changes as a post creation event, but I may have to change the original, copy and change original back to get it to work.

Sydious
Giant
Posts: 155
Joined: Sun Aug 21, 2011 2:27 am

Re: Create Tokens

Post by Sydious »

That makes sense.

Post Reply

Return to “Macros”