Can I make a script undoable?

I’ve written a script that runs through every pixel of the current sprite and adjusts its color. It would be nice if I could add this change to the undo queue, but it seems after running my script the undo queue is completely discarded. I even tried wrapping my method in an app.transaction(), but it is still not undoable. Can this be done?

I’ve only written one script, but it’s in a transaction and it’s undoable. Before I added the transaction, every step of the script was undoable on its own. So, if the undo queue gets deleted, it must be because of something you’re doing in the script.

The only thing I’m doing that changes the picture is Image::drawPixel(), so I presume that the undo mechanism doesn’t catch that.

Ah, you are correct. In the docs:

Warning : This method doesn’t create undo information, you should clone the image and then patch it to get proper undo/redo information.

2 Likes

That worked. Thanks!

hi, can i have a stupid question? how do you use transaction? in the documentation there is example:

app.transaction(
  function()
    ...
  end)

however anytime i try that i get a syntax error, on the other hand if i wrap only call to the function, it has no effect, so i have undo for each action

I have code identical to that in my scripts and they run just fine, and the transaction works correctly. Are you sure the syntax error wasn’t somewhere within your function? Make sure you didn’t accidentally leave off an end somewhere when copy+pasting your script inside the transaction.

okay, i made a few tests and now i’m even more confused :smiley:

app.transaction(
	function () 
		local var = 1 
		print("var: ", var) 
	end 
)

works, but

local var = 1 
fName(var)

app.transaction(
	function fName(var) 
		print("var: ", var) 
	end 
)

doesn’t.
it throws: test.lua:6: ‘(’ expected near ‘fName’
however i need functions with names to be able to call them.

interestingly, when i do this:

local var = 1 

function fName(var) 
	app.transaction(
		print("var: ", var) 
	)
end 

fName(var)

it’s fine. BUT, if i do this:

local var = 1 

function fName(var) 
	app.transaction(
		if var == 1 then  
			print("var: ", var) 
		else 
			print("lol") 
		end
	)
end 

fName(var)

i get: test.lua:6: unexpected symbol near ‘if’
there’s something important about this i’m still not getting… :expressionless:

In that very last example, you’re getting an error because app.transaction wants a function as a parameter (that’s why there are the parentheses, it’s a parameter list!), but you’re passing in code rather than a variable. You need to wrap the contents of the transaction in an anonymous function.

1 Like

oh, i see. i thought that function name() is technically same as function(). it felt strange to nest one function in another function. thank you very much!

If you want to avoid this nested function nonsense, then just set your variables outside of the transaction, and use them in the transaction. They’re still accessible from there, so there’s no reason to pass them in.

local var = 1

app.transaction(
	function () 
		print("var: ", var)
	end 
)

it’s good to know, however i need to call functions from dialog:button onclick and from other functions as well, is there a way to do that without names? i mean, i think i can live with

function name() 
   app.transaction( 
      function() 
         ... 
      end) 
end

as long as it works.