copyToken questions

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

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

copyToken questions

Post by xavram »

So I'm generating a new token like so...

Code: Select all

[h : updates = json.set("", "Name", "XXX", "useDistance", 1, "x", getTokenX(1,selectedToken), "y", getTokenY(1,selectedToken), "facing", getTokenFacing(selectedToken))]

[h: copyToken("Mines", 1, "Resources", updates)]
The new token is created as expected but I'm having a couple of issues with the copyToken function.

1. It does not return the id or name of the created token and I need to do further work on the token that I can't via the "updates" json. Is there some way to determine the id/name of the token created so that I can access it to set further properties?

2. This works just fine when the "selectedToken" is the same size as the token that I'm copying (in this case, the "Mines" token from the "Resources" map). The new token is created directly under the selected Token. But, if they're not the same size, the things are "off". I know they would be off if the sizes were totally different but my selected token is 75x150 and the "Mines" token is 75x75. I expected the created token to be directly under the front half of the selected token. But its off by like 15 pixels. Any ideas on why this would be? I've double checked the sizes and they are sized as stated.

Thanks!

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

Re: copyToken questions

Post by aliasmask »

copyToken returns the id as a part of the function, so...

Code: Select all

[H: id = copyToken(...)]
You may want to refer to my createToken() function for setting properties.

http://forums.rptools.net/viewtopic.php ... 90#p227490

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

Oh cool, documentation on wiki doesn't say that it returns the Id...excellent.

Any ideas on the positioning issue?

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

Odd...look at this...

Code: Select all

		[h, MACRO("CreateDropped_New@Lib:Math"):droppedArgs]
		[h : newId = macro.return]
		[h, MACRO("CreateDropped_SetProperties@Lib:Math"):newId]
In "CreateDropped_New", I can see that the new token is being created...

Code: Select all

[h: newId = copyToken("Spikes", 1, "Resources", updates)]
	[broadcast(newId)]
[h : marco.return = newId]
...but when I try to use that "newId" for the "CreateDropped_SetProperties", its blank. What gives?

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

Okay, never mind the positioning, I have to break out the math to get that to work (figured out the issue, just not the solution...yet...heh).

But< i still don't understand why I can't use the ID of the newly created token. I get that I can't use it within the macro that created it, but as I showed, the id isn't even getting returned from the macro.

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

Okay, final post on this frustrating issue...then I gotta put this aside until tomorrow before I break something...

Code: Select all

[h : arguments = macro.args]

[h : selectedToken = json.get(arguments, "Shooter")]
[h : weapon = json.get(arguments, "Weapon")]

[h : moveThisMuch = 155]

[h : updates = json.set("", "useDistance", 1, "x", getTokenX(1,selectedToken), "y", getTokenY(1,selectedToken), "facing", getTokenFacing(selectedToken))]

[h: newId = copyToken("Spikes", 1, "Resources", updates)]

[h: link = macroLinkText("CreateDropped_SetProperties@Lib:Math", "all", newId)]
[h: execLink(link)]
Then, the "CreateDropped_SetProperties" marcro...

Code: Select all

[h : id = macro.args]
[h : setProperty("TireDamage", "2d6", id)]
This runs fine, no errors...but, when I check the properties for the newly created token, the "TireDamage" property is still set to the default value, 0.

Any ideas?

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

Re: copyToken questions

Post by aliasmask »

Yeah, you can't really set properties on a newly created token. That's why I use my hack to do it. Here's the relevant part of code:

Code: Select all

<!-- args contains all the props I want to set in the form of a json object -->
[H: props = json.get(args,"props")]
[H: propsSet = ""]
<!-- loop through each item creating macro code in the string which sets the property. Uses decode and encode to avoid bad characters -->
[H, foreach(prop,props): propsSet = json.append(propsSet,strformat('[H: setProperty("%{prop}",decode("%s"))]',encode(json.get(props,prop))))]
[H: name = json.get(updates,"name")]
[H: isLib = if(substring(trim(lower(name)),0,4) == "lib:",1,0)]
[H: propsSet = json.toList(propsSet,"")]
<!-- we are going to add the props to set to the name property which will also run code. This is the hacky part -->
[H: name = name+propsSet]
[H: updates = json.set(updates,"name",name)]

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: copyToken questions

Post by wolph42 »

point in case is that if you copy token A to A* then everything you change of A* (outside of the copyToken function) will be reset back to A.
The trick I normally use is to
1. make all necessary changes to A (required for A*)
2. copyToken A to A*
optionally 3. set A back to its original 'settings'. One possible trick for this latter thing (should it be required is to copyToken A to A** (with same name, so keep track of IDs of A and A*) then change A then copyToken A to A** and then delete A. (Which leaves A* which has the same name as A and the original settings)

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

Man, this seems like this is much harder than it should be....

Alias, I don't see where you actually copy or set the properties for the new token in your code snippet. I don't even see a "CopyToken" call in that snippet!

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

Re: copyToken questions

Post by aliasmask »

xavram wrote:Man, this seems like this is much harder than it should be....

Alias, I don't see where you actually copy or set the properties for the new token in your code snippet. I don't even see a "CopyToken" call in that snippet!
It's just a snippet, the full code is in my other post with a link to another forum post.

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

Gotcha, will check it out.

Does anyone know WHEN you can actually set properties on tokens created with copyToken? Is it a timing thing or just have to be fully out of the scope of the calling macro?

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: copyToken questions

Post by wolph42 »

as said you canchange the original and then copy it. the alternative is copy it then have an interupt: Wiki: input() and then continue with updating the properties. Those are the only two ways. You can probably find the pause macro (which uses the input) somewhere in the wiki, else you can use the bot which has that function implemented. That way you can create an interupt as follows:

Code: Select all

copytoken...
[message="This interuption is required for updating the token"]
[pause("message")]
update the token properties

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

Change the original and then copy it...can you do a "setProperty" call on a token that's not on the map you're currently on?

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: copyToken questions

Post by wolph42 »

no you can't but you can switchmap, change property and switch back
or move token to current map, change it and move it back.
the first will cause a little flicker on screen, the latter wont

xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Re: copyToken questions

Post by xavram »

I will try, "move necessary token from Resource map to current map, set properties on it, copy it to new token, then move original token back to Resource map"...do those steps seem right?

Post Reply

Return to “Macros”