Help with script to change button icon based on a var


I am trying to get an icon for a button to changed based on whether a var is set or not. I can get it to work if @toggle is enabled for the var, but if @toggle is disabled (which is what I want) then the icon switching stops working as well.

Here is the working button code. If you swap the comments (//) on the toggle lines then the icon switching stops working. If there is a way to have the icon changing with toggle disabled that would be great!

<?xml version="1.0"?>
<button 3dborders="no" backcol="none" display="icon" icon_size="large" textcol="none">
	<function type="normal">
		<instruction>@toggle:if $lst:vViewPin</instruction>
		<instruction />
		<instruction>//if var exists</instruction>
		<instruction>@set lst:vViewPin</instruction>
		<instruction />
		<instruction>//if var does not exist</instruction>
		<instruction>@set lst:vViewPin=on</instruction>

Many thanks,

It works ok for me just as it is.

Hi Jon,

Is there a way to do it without the blue toggle background though?


Ah, I see what you mean. The problem is that the button isn't being redrawn without the @toggle directive in there, which means even though the variable changes the appearance doesn't update. You can use @toggle:update to force the redraw to happen manually:

@set lst:vViewPin
@set lst:vViewPin=on

Thanks, Jon. That's perfect!

Using Jon's code, I note that the icon toggle works on a fixed toolbar but not on a floating toolbar.

Regards, AB

lst: variables are scoped to a Lister, and a floating toolbar doesn't have a Lister.