Hi everyone, I was hoping someone could help me with the following. I have a button that saves a screenshot PNG to the Downloads folder when you press it.

What I want is for the button to disappear after it is pressed, so the screenshot does not include the two menu buttons called “%SaveReport” and “%BackMainMenu”.

The code for the save button is listed below:

`

func _on_SaveReport_pressed():

$"%SaveReport".visible = false

$"%BackMainMenu".visible = false

print("I've disabled the buttons")
print("That means the screenshot SHOULD be button free")

take_screenshot()

$"%SaveReport".visible = true
$"%BackMainMenu".visible = true

`

As you can see, it calls the take_screenshot() function which is listed above:

`

func take_screenshot(): image = get_viewport().get_texture().get_image()

if OS.get_name() == "Web" or OS.has_feature('JavaScript'):
	print("We're on the web")
	# We're on the web		

	image.clear_mipmaps()

	var buffer = image.save_png_to_buffer()
	JavaScriptBridge.download_buffer(buffer, fileName)

if OS.get_name() != "Web" or !OS.has_feature('JavaScript'):
	# We're not on the web
	print("We're not on the web")
	
	var docs = OS.get_environment("HOME") + "/Documents"
	
	var title = str(docs + "/results",global_ints.observed_person_name, global_ints.observation_minutes,".png")
	
	print(title)
	
	var _saveimage = image.save_png(title)
	
	if OS.get_name() != "OSX":
		print("We're not on MacOS")
		var _openfolder = OS.shell_open(docs)
	
	if OS.get_name() == "OSX":
		print("We're on MacOS")
		
		var _openfolder = OS.shell_open("file://" + docs)

`

The code works. The screenshot is taken and it’s saved to the Downloads folder and MacOS/Windows/Linux open up the Downloads folder straight after.

For the life of me, I can’t figure out why the Back & Screenshot buttons ( “%SaveReport” and “%BackMainMenu”) that I turn invisible BEFORE I call take_screenshot() end up being in the screenshot. Every single time.

Anyone have any ideas?

Thank you!

  • bruce965@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    1 year ago

    You will have to “await” the next frame after disabling the buttons, then take the screenshot and re-enable the buttons.

    Otherwise you might be able to get the same effect by putting the buttons on a separate layer and excluding it from the screenshot (not sure if it’s actually possible this way, but I strongly suppose so).

  • HobbesHK@startrek.websiteOP
    link
    fedilink
    English
    arrow-up
    2
    ·
    1 year ago

    I took away the 2 .visible = true commands at the end, just to see if they were the culprit. They are not. It appears that the get_viewport().get_texture().get_image() command gets called before the interface elements are turned off, even though the order of code would make me think otherwise.

    • PoolloverNathan@programming.dev
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      You did turn off the interface elements, but too late for the viewport image to reflect that (as it was taken at the end of last frame). Like @bruce965@lemmy.ml said, you should use await get_tree().idle_frame to wait until the image up to date, then get and save it.

      • HobbesHK@startrek.websiteOP
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 year ago

        await get_tree().idle_frame

        Thank you for this! I just tried it out but unfortunately Godot throws an error on await get_tree().idle_frame : Invalid get index 'idle_frame' (on base: 'SceneTree').

        Could it be because I’m running in application mode, which only refreshes the screen if there’s an update?

        As an alternative, I’ve put the code to turn things off into its own function:

        func turn_off():
        	$"%SaveReport".visible = false
        	$"%BackMainMenu".visible = false
        

        I’ve then tried an await turn_off()

        The code runs, but doesn’t do anything and the screenshot still gets saved with the buttons visible.

        I’m trying both await functions just before the take_screenshot() function like so:

        	await turn_off()
        	await get_tree().idle_frame
        	
        	take_screenshot()
        

        Am I missing something very obvious here? Any help would be much appreciated!

        • HobbesHK@startrek.websiteOP
          link
          fedilink
          English
          arrow-up
          1
          ·
          1 year ago

          Managed to fix it by using await get_tree().process_frame instead. It seems that idle_frame appears to no longer exist in Godot 4.1?

          So my full code for triggering the screenshot function is:

          func _on_SaveReport_pressed():
          	await get_tree().process_frame
          	$"%SaveReport".visible = false
          	$"%BackMainMenu".visible = false
          	await get_tree().process_frame
          	
          	take_screenshot()
          
          	$"%SaveReport".visible = true
          	$"%BackMainMenu".visible = true
          

          For some reason, I have to await before I turn the interface elements off, and after I’ve turned them off. It now works a treat for my app. Thank you for your assistance!