Caught in a loop...

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
xavram
Dragon
Posts: 891
Joined: Tue Apr 20, 2010 8:22 pm

Caught in a loop...

Post by xavram »

I have a project I'm working on that involves a "onChangeSelection" and I've done something stupid in my code that's resulting in an endless loop, freezing Maptool completely.

The problem is, I can't MODIFY the macro to fix this because the moment I click on the library token, it goes into this loop and freezes!

Is there any way to stop this, so I can actually modify the macro that's causing the issue?

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

Re: Caught in a loop...

Post by aliasmask »

Oh onSelect. You need to have an open frame that's running the code to run that handler, so close that window.

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

Re: Caught in a loop...

Post by wolph42 »

and next time make sure that your lib is of a certain property type and make a check to NOT run onchangeselction for that prop type.

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

Re: Caught in a loop...

Post by aliasmask »

I made the same mistake in my original reply, but onChangeSelection doesn't care about prop types. It just runs when a token is selected or deselected or clicked. It doesn't use the stat sheet.

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

Re: Caught in a loop...

Post by xavram »

Got it fixed, thanks! I do need to add in a check for property type, good call.

That had me sweating there for a minute...:)

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

Re: Caught in a loop...

Post by aliasmask »

I actually have some generic code that handles instances of selecting a token, double selecting a token, deselecting and multi-select. I went ahead and added some comments to help translate.

Code: Select all

@@ @onTokenSelect
[H: eventId = macro.args]

<!-- property saved on players personal lib token, but you can do this with the new table functions -->
[H: lastEventId = am.play.getPlayerProperty("selectEventId")]

<!-- onChangeSelect executes multiple times. This stops the duplication -->
[H: matchingEvent = if(eventId == lastEventId,1,0)]
[H, if(matchingEvent): abort(0)]

<!-- get current selection and saved selection on lib token -->
[H: selectedTokens = getSelected("json")]
[H, if(json.isEmpty(selectedTokens)): selectedTokens = "[]"]
[H, if(json.type(selectedTokens) == "UNKNOWN"): selectedTokens = json.fromList(selectedTokens)]
[H: selectedList = am.play.getPlayerProperty("selectedTokenList")]
[H, if(json.isEmpty(selectedList)): selectedList = "[]"]

<!-- difference in selection - added tokens to selection -->
[H: selectChange = json.difference(selectedTokens,selectedList)]
<!-- difference in deselection - removed tokens from selection -->
[H: deselectChange = json.difference(selectedList,selectedTokens)]
<!-- onChangeSelect executed but no change in selection means a token was reselected -->
[H: eventProcessed = json.equals(selectedTokens,selectedList)]
<!-- only one token has been selected -->
[H: singleSelect = if(json.length(selectedTokens) == 1,1,0)]

[H, if(! eventProcessed), code: {
   <!-- There was a change in selected tokens -->
   [H: am.play.setPlayerProperty("selectedTokenList",selectedTokens)]
   [H, if(! json.isEmpty(deselectChange)), code: {
      <!-- deselection event processing -->
      [H: link = macroLinkText("multiDeselectChange@"+getMacroLocation(),"none",deselectChange)]
      [H: execLink(link,1)]
   };{}]
   [H, if(! json.isEmpty(selectChange)), code: {
      <!-- new selection event processing -->
      [H: link = macroLinkText("multiSelectChange@"+getMacroLocation(),"none",selectChange)]
      [H: execLink(link,1)]
   };{}]
   [H: am.play.deferFunction("am.play.openSelectFrame")]
   [H: am.play.setPlayerProperty("selectEventId",eventId)]
};{
   [H, if(singleSelect), code: {
      <!-- double click/select event processing -->
      [H: broadcast(strformat("<b>Double Click: </b>%s",getName(json.get(selectedTokens,0))))]
      [H: am.play.deferFunction("am.play.openSelectFrame")]
      [H: am.play.setPlayerProperty("selectEventId",eventId)]
   };{}]
}]

!! 

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

Re: Caught in a loop...

Post by xavram »

Good stuff, I'll have to see if I can incorporate some of this.

Post Reply

Return to “MapTool”